revisions set by The Compiler

This commit is contained in:
Josefson Fraga 2017-11-17 02:38:56 -03:00
parent 92f9a8503e
commit 96599b9684

95
scripts/hist_importer.py Normal file → Executable file
View File

@ -1,8 +1,11 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: # vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# This file is part of qutebrowser. # Copyright 2017 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
# Copyright 2014-2017 Josefson Souza <josefson.br@gmail.com>
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify # qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or # the Free Software Foundation, either version 3 of the License, or
@ -17,60 +20,60 @@
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. # along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
'''Tool to import browser history data from other browsers. Although, safari """Tool to import browser history from other browsers."""
support is still on the way.'''
import argparse import argparse
import sqlite3 import sqlite3
import sys import sys
import os
def parser(): def parse():
"""Parse command line arguments.""" """Parse command line arguments."""
description = 'This program is meant to extract browser history from your'\ description = ("This program is meant to extract browser history from your"
'previous browser and import them into qutebrowser.' "previous browser and import them into qutebrowser.")
epilog = 'Databases:\n\tQute: Is named "history.sqlite" and can be found '\ epilog = ("Databases:\n\tQutebrowser: Is named 'history.sqlite' and can be"
'at your --basedir. In order to find where your basedir is you '\ " found at your --basedir. In order to find where your basedir"
'can run ":open qute:version" inside qutebrowser.'\ " is you can run ':open qute:version' inside qutebrowser."
'\n\tFirerox: Is named "places.sqlite", and can be found at your'\ "\n\tFirerox: Is named 'places.sqlite', and can be found at your"
'system\'s profile folder. Check this link for where it is locat'\ "system\"s profile folder. Check this link for where it is locat"
'ed: http://kb.mozillazine.org/Profile_folder'\ "ed: http://kb.mozillazine.org/Profile_folder"
'\n\tChrome: Is named "History", and can be found at the respec'\ "\n\tChrome: Is named 'History', and can be found at the respec"
'tive User Data Directory. Check this link for where it is locat'\ "tive User Data Directory. Check this link for where it is locat"
'ed: https://chromium.googlesource.com/chromium/src/+/master/'\ "ed: https://chromium.googlesource.com/chromium/src/+/master/"
'docs/user_data_dir.md\n\n'\ "docs/user_data_dir.md\n\n"
'Example: $this_script.py -b firefox -s /Firefox/Profile/places.'\ "Example: hist_importer.py -b firefox -s /Firefox/Profile/"
'sqlite -d /qutebrowser/data/history.sqlite' "places.sqlite -d /qutebrowser/data/history.sqlite")
parsed = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description=description, epilog=epilog, description=description, epilog=epilog,
formatter_class=argparse.RawTextHelpFormatter formatter_class=argparse.RawTextHelpFormatter
) )
parsed.add_argument('-b', '--browser', dest='browser', required=True, parser.add_argument('-b', '--browser', dest='browser', required=True,
type=str, help='Browsers: {firefox, chrome, safari}') type=str, help='Browsers: {firefox, chrome}')
parsed.add_argument('-s', '--source', dest='source', required=True, parser.add_argument('-s', '--source', dest='source', required=True,
type=str, help='Source: fullpath to the sqlite data' type=str, help='Source: Full path to the sqlite data'
'base file from the source browser.') 'base file from the source browser.')
parsed.add_argument('-d', '--dest', dest='dest', required=True, type=str, parser.add_argument('-d', '--dest', dest='dest', required=True, type=str,
help='Destination: The fullpath to the qutebrowser ' help='Destination: The full path to the qutebrowser '
'sqlite database') 'sqlite database')
return parsed.parse_args() return parser.parse_args()
def open_db(data_base): def open_db(data_base):
"""Open connection with database.""" """Open connection with database."""
try: if os.path.isfile(data_base):
conn = sqlite3.connect(data_base) conn = sqlite3.connect(data_base)
return conn return conn
except Exception as any_e: else:
print('Error: {}'.format(any_e)) raise sys.exit('\nDataBaseNotFound: There was some error trying to to'
raise('Error: There was some error trying to to connect with the [{}]' ' connect with the [{}] database. Verify if the'
'database. Verify if the filepath is correct or is being used.'. ' filepath is correct or is being used.'
format(data_base)) .format(data_base))
def extract(source, query): def extract(source, query):
"""Performs extraction of (datetime,url,title) from source.""" """Extracts (datetime,url,title) from source database."""
try: try:
conn = open_db(source) conn = open_db(source)
cursor = conn.cursor() cursor = conn.cursor()
@ -78,18 +81,17 @@ def extract(source, query):
history = cursor.fetchall() history = cursor.fetchall()
conn.close() conn.close()
return history return history
except Exception as any_e: except sqlite3.OperationalError as op_e:
print('Error: {}'.format(any_e)) print('\nCould not perform queries into the source database: {}'
print(type(source)) '\nBrowser version is not supported as it have a different sql'
raise('Error: There was some error trying to to connect with the [{}]' ' schema.'.format(op_e))
'database. Verify if the filepath is correct or is being used.'.
format(str(source)))
def clean(history): def clean(history):
"""Receives a list of records:(datetime,url,title). And clean all records """Clean up records from source database.
in place, that has a NULL/None datetime attribute. Otherwise Qutebrowser Receives a list of records:(datetime,url,title). And clean all records
will throw errors. Also, will add a 4rth attribute of '0' for the redirect in place, that has a NULL/None datetime attribute. Otherwise qutebrowser
will throw errors. Also, will add a 4th attribute of '0' for the redirect
field in history.sqlite in qutebrowser.""" field in history.sqlite in qutebrowser."""
nulls = [record for record in history if record[0] is None] nulls = [record for record in history if record[0] is None]
for null_datetime in nulls: for null_datetime in nulls:
@ -101,7 +103,8 @@ def clean(history):
def insert_qb(history, dest): def insert_qb(history, dest):
"""Given a list of records in history and a dest db, insert all records in """Insert history into dest database
Given a list of records in history and a dest db, insert all records in
the dest db.""" the dest db."""
conn = open_db(dest) conn = open_db(dest)
cursor = conn.cursor() cursor = conn.cursor()
@ -116,7 +119,7 @@ def insert_qb(history, dest):
def main(): def main():
"""Main control flux of the script.""" """Main control flux of the script."""
args = parser() args = parse()
browser = args.browser.lower() browser = args.browser.lower()
source, dest = args.source, args.dest source, dest = args.source, args.dest
query = { query = {
@ -124,15 +127,11 @@ def main():
'from moz_places', 'from moz_places',
'chrome': 'select url,title,last_visit_time/10000000 as date ' 'chrome': 'select url,title,last_visit_time/10000000 as date '
'from urls', 'from urls',
'safari': None
} }
if browser not in query: if browser not in query:
sys.exit('Sorry, the selected browser: "{}" is not supported.'.format( sys.exit('Sorry, the selected browser: "{}" is not supported.'.format(
browser)) browser))
else: else:
if browser == 'safari':
print('Sorry, currently we do not support this browser.')
sys.exit(1)
history = extract(source, query[browser]) history = extract(source, query[browser])
history = clean(history) history = clean(history)
insert_qb(history, dest) insert_qb(history, dest)