Outputs Class

class browser_history.generic.Outputs(fetch_type)

Bases: object

A generic class to encapsulate history and bookmark outputs and to easily convert them to JSON, CSV or other formats.

Parameters:

fetch_type – string argument to select history output or bookmarks output

_get_data()

Return the list of histories or bookmarks (depending on fetch_type).

_get_fields()

Return names of the fields of the data.

_valid_fetch_types = ('history', 'bookmarks')
bookmarks: List[Tuple[datetime, str, str, str]]

List of tuples of Timestamp, URL, Title, Folder.

property field_map: Dict[str, Any]

[Deprecated] This was not meant for public usage and will be removed soon.

Use _get_data and _get_fields if you really need this.

format_map: Dict[str, Callable]

Dictionary which maps output formats to their respective functions.

formatted(output_format='csv')

Returns history or bookmarks as a str formatted as output_format

Parameters:

output_format (str) – One the formats in csv, json, jsonl

Return type:

str

histories: List[Tuple[datetime, str, str]]

List of tuples of Timestamp, URL, Title.

save(filename, output_format='infer')

Saves history or bookmarks to a file. Infers the type from the given filename extension. If the type could not be inferred, it defaults to csv.

Parameters:
  • filename – the name of the file.

  • output_format – (optional)One the formats in csv, json, jsonl. If not given, it will automatically be inferd from the file’s extension

sort_domain()

Returns the history/bookamarks sorted according to the domain-name.

Examples:

>>> from datetime import datetime
... from browser_history import generic
... entries = [
...     (datetime(2020, 1, 1), 'https://google.com', 'Google'),
...     (
...         datetime(2020, 1, 1),
...         "https://google.com/imghp?hl=EN",
...         "Google Images",
...     ),
...     (datetime(2020, 1, 1), 'https://example.com', 'Example'),
... ]
... obj = generic.Outputs('history')
... obj.histories = entries
... obj.sort_domain()
defaultdict(<class 'list'>, {
    'example.com': [
        [
            datetime.datetime(2020, 1, 1, 0, 0),
            'https://example.com',
            'Example'
        ]
    ],
    'google.com': [
         [
            datetime.datetime(2020, 1, 1, 0, 0),
            'https://google.com',
            'Google'
         ],
         [
            datetime.datetime(2020, 1, 1, 0, 0),
            'https://google.com/imghp?hl=EN',
            'Google Images'
        ]
:rtype: :sphinx_autodoc_typehints_type:`\:py\:class\:\`\~typing.DefaultDict\`\\ \\\[\:py\:data\:\`\~typing.Any\`\, \:py\:class\:\`\~typing.List\`\\ \\\[\:py\:data\:\`\~typing.Any\`\]\]`

]

})

to_csv()

Return history or bookmarks formatted as a comma separated string with the first row having the fields names

Return type:

str

Returns:

string with the output in CSV format

Examples:

>>> from datetime import datetime
... from browser_history import generic
... entries = [
...     [datetime(2020, 1, 1), 'https://google.com', 'Google'],
...     [datetime(2020, 1, 1), 'https://example.com', 'Example Domain'],
... ]
... obj = generic.Outputs('history')
... obj.histories = entries
... print(obj.to_csv())
Timestamp,URL
2020-01-01 00:00:00,https://google.com,Google
2020-01-01 00:00:00,https://example.com,Example Domain
to_json(json_lines=False)

Return history or bookmarks formatted as a JSON or JSON Lines format names. If json_lines flag is True convert to JSON Lines format, otherwise convert it to Plain JSON format.

Parameters:

json_lines (bool) – flag to specify if the json_string should be JSON Lines.

Return type:

str

Returns:

string with the output in JSON/JSONL format

Examples:

>>> from datetime import datetime
... from browser_history import generic
... entries = [
...     [datetime(2020, 1, 1), 'https://google.com', 'Google'],
...     [datetime(2020, 1, 1), 'https://example.com', 'Example Domain'],
... ]
... obj = generic.Outputs()
... obj.entries = entries
... print(obj.to_json(True))
{
    "Timestamp": "2020-01-01T00:00:00",
    "URL": "https://google.com",
    "Title": "Google",
}
{
    "Timestamp": "2020-01-01T00:00:00",
    "URL": "https://example.com",
    "Title": "Example Domain",
}
>>> print(obj.to_json())
{
    "history": [
        {
            "Timestamp": "2020-01-01T00:00:00",
            "URL": "https://google.com",
            "Title", "Google"
        },
        {
            "Timestamp": "2020-01-01T00:00:00",
            "URL": "https://example.com",
            "Title": "Example Domain"
        }
    ]
}