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
- bookmarks: List[Tuple[datetime.datetime, str, str, str]]
List of tuples of Timestamp, URL, Title, Folder.
- field_map: Dict[str, Dict[str, Any]]
Dictionary which maps fetch_type to the respective variables and formatting fields.
- 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 asoutput_format
- Parameters
output_format (
str
) – One the formats in csv, json, jsonl- Return type
str
- histories: List[Tuple[datetime.datetime, str]]
List of tuples of Timestamp & URL
- 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'], ... [datetime(2020, 1, 1), 'https://google.com/imghp?hl=EN'], ... [datetime(2020, 1, 1), 'https://example.com'], ... ] ... 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' ] ], 'google.com': [ [ datetime.datetime(2020, 1, 1, 0, 0), 'https://google.com' ], [ datetime.datetime(2020, 1, 1, 0, 0), 'https://google.com/imghp?hl=EN' ] ] })
- Return type
DefaultDict
[Any
,List
[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'], ... [datetime(2020, 1, 1), 'https://example.com'], ... ] ... obj = generic.Outputs('history') ... obj.histories = entries ... print(obj.to_csv()) Timestamp,URL 2020-01-01 00:00:00,https://google.com 2020-01-01 00:00:00,https://example.com
- 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'], ... [datetime(2020, 1, 1), 'https://example.com'], ... ] ... obj = generic.Outputs() ... obj.entries = entries ... print(obj.to_json(True)) {"Timestamp": "2020-01-01T00:00:00", "URL": "https://google.com"} {"Timestamp": "2020-01-01T00:00:00", "URL": "https://example.com"} >>> print(obj.to_json()) { "history": [ { "Timestamp": "2020-01-01T00:00:00", "URL": "https://google.com" }, { "Timestamp": "2020-01-01T00:00:00", "URL": "https://example.com" } ] }