Skip to content

Commit

Permalink
separate db and appdata functions
Browse files Browse the repository at this point in the history
  • Loading branch information
svandragt committed Mar 17, 2020
1 parent 845569a commit 8dbe7d6
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 88 deletions.
2 changes: 1 addition & 1 deletion fafi/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.0'
__version__ = "0.1.5"
58 changes: 58 additions & 0 deletions fafi/appdata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import appdirs
import os
import shutil
import tempfile

# fafi
import db


def bookmarks_path():
return appdirs.user_data_dir("Firefox")


def get_bookmarks_db():

# set the path of firefox folder with databases
bm_path = bookmarks_path()

# recursively walk tha path
for root, dirs, files in os.walk(bm_path + "/Profiles/"):
for name in files:
if name == "places.sqlite":
db_path = str(root + os.sep + name).strip()
print("Indexing: ", db_path)
return db_path
return None


def db_path():
data_dir = appdirs.user_data_dir("fafi")
if not os.path.exists(data_dir):
os.makedirs(data_dir)
db_path = data_dir + "/data.sqlite"
print("Using: " + db_path)
return db_path


# get bookmarks from firefox sqlite database file and print all
def select_bookmarks(cursor):
bookmarks_query = """
SELECT DISTINCT
url, moz_places.title from moz_places
JOIN
moz_bookmarks on moz_bookmarks.fk=moz_places.id
WHERE
moz_places.url like 'http%'
ORDER BY
dateAdded desc
"""
db.execute_query(cursor, bookmarks_query)
return cursor


def create_temporary_copy(path):
temp_dir = tempfile.gettempdir()
temp_path = os.path.join(temp_dir, "temp_file_name")
shutil.copy2(path, temp_path)
return temp_path
36 changes: 36 additions & 0 deletions fafi/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sqlite3


def create_connection(db_file):
""" create a database connection to the SQLite database
specified by db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
return conn
except sqlite3.Error as e:
print(e)

return conn


def create_table(conn, table_sql):
""" create a table from the table_sql statement
:param conn: Connection object
:param table_sql: a CREATE TABLE statement
:return:
"""
c = conn.cursor()
c.execute(table_sql)
conn.commit()


# execute a query on sqlite cursor
def execute_query(cursor, query):
try:
cursor.execute(query)
except Exception as error:
print(str(error) + "\n " + query)
100 changes: 14 additions & 86 deletions fafi/fafi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,15 @@

from contextlib import closing
from subprocess import call
import appdirs
import argparse
import click
import hashlib
import newspaper
import os
import shutil
import sqlite3
import tempfile


fafi = None

# execute a query on sqlite cursor
def execute_query(cursor, query):
try:
cursor.execute(query)
except Exception as error:
print(str(error) + "\n " + query)


# get bookmarks from firefox sqlite database file and print all
def select_bookmarks(cursor):
bookmarks_query = """
SELECT DISTINCT
url, moz_places.title from moz_places
JOIN
moz_bookmarks on moz_bookmarks.fk=moz_places.id
WHERE
moz_places.url like 'http%'
ORDER BY
dateAdded desc
"""
execute_query(cursor, bookmarks_query)
return cursor


def create_temporary_copy(path):
temp_dir = tempfile.gettempdir()
temp_path = os.path.join(temp_dir, "temp_file_name")
shutil.copy2(path, temp_path)
return temp_path


def get_bookmarks_path():
# set the path of firefox folder with databases
bookmarks_path = appdirs.user_data_dir("Firefox")

for root, dirs, files in os.walk(bookmarks_path + "/Profiles/"):
for name in files:
if name == "places.sqlite":
print("Indexing: ", root + os.sep + name)
return root + os.sep + name
return None


def create_connection(db_file):
""" create a database connection to the SQLite database
specified by db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
return conn
except sqlite3.Error as e:
print(e)

return conn


def create_table(conn, table_sql):
""" create a table from the table_sql statement
:param conn: Connection object
:param table_sql: a CREATE TABLE statement
:return:
"""
c = conn.cursor()
c.execute(table_sql)
conn.commit()
# fafi
import appdata
import db


def index_site(conn, row, verbose):
Expand Down Expand Up @@ -125,19 +53,18 @@ def cli():
)
@click.option("-v", "--verbose", is_flag=True, help="Enables verbose mode")
def do_index(verbose, stop_when_exists):
path = get_bookmarks_path()
bm_db = appdata.get_bookmarks_db()
exists = 0
if path:
temp_path = create_temporary_copy(path)
if bm_db:
temp_path = appdata.create_temporary_copy(bm_db)

with create_connection(temp_path) as places:
with db.create_connection(temp_path) as places:
with closing(places.cursor()) as ff_cursor:
ff_cursor = select_bookmarks(ff_cursor)
ff_cursor = appdata.select_bookmarks(ff_cursor)

if not os.path.exists("./data"):
os.makedirs("./data")
with create_connection("./data/fafi.sqlite") as fafi:
create_table(
db_path = appdata.db_path()
with db.create_connection(db_path) as fafi:
db.create_table(
fafi,
"CREATE VIRTUAL TABLE IF NOT EXISTS sites USING FTS5(url, text)",
)
Expand All @@ -160,8 +87,9 @@ def do_index(verbose, stop_when_exists):
)
def do_search(query, max_results):
print("Searching for:", query)
if os.path.exists("./data/fafi.sqlite"):
with create_connection("./data/fafi.sqlite") as fafi:
db_path = appdata.db_path()
if os.path.exists(db_path):
with db.create_connection(db_path) as fafi:
cursor = fafi.execute(
"""SELECT
url,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "fafi"
version = "0.1.4"
version = "0.1.5"
description = "CLI for indexing Firefox bookmarks."
authors = ["Sander van Dragt <sander@vandragt.com>"]
license = "MIT"
Expand Down

0 comments on commit 8dbe7d6

Please sign in to comment.