diff --git a/blasta/__main__.py b/blasta/__main__.py index a22f789..3408c4f 100644 --- a/blasta/__main__.py +++ b/blasta/__main__.py @@ -12,10 +12,12 @@ TODO """ import argparse +from blasta.config import Share from blasta.http.instance import HttpInstance -from blasta.sqlite import SQLite +from blasta.database.sqlite import DatabaseSQLite import json import logging +import os from os.path import getsize, exists import sys import time @@ -32,8 +34,10 @@ except: def main(): - if not exists('main.sqlite') or not getsize('main.sqlite'): - SQLite.instantiate_database('main.sqlite') + directory_data = Share.get_directory() + db_file = os.path.join(directory_data, 'main.sqlite') + if not exists(db_file) or not getsize(db_file): + DatabaseSQLite.instantiate_database(db_file) accounts = {} sessions = {} http_instance = HttpInstance(accounts, sessions) @@ -41,7 +45,6 @@ def main(): app = main() -# FIXME if __name__ == 'blasta.__main__': parser = argparse.ArgumentParser( prog='blasta', @@ -52,7 +55,7 @@ if __name__ == 'blasta.__main__': parser.add_argument('-p', '--port', help='port number', dest='port') parser.add_argument('-o', '--open', help='open an html browser', action='store_const', const=True, dest='open') args = parser.parse_args() - port = args.port if args.port else 8000 + port = args.port or 8000 uvicorn.run(app, host='localhost', port=port) if args.open: # TODO Check first time diff --git a/blasta/assets/graphic/blasta.ico b/blasta/assets/graphic/blasta.ico new file mode 100644 index 0000000..81548ff Binary files /dev/null and b/blasta/assets/graphic/blasta.ico differ diff --git a/blasta/sqlite.py b/blasta/database/sqlite.py similarity index 94% rename from blasta/sqlite.py rename to blasta/database/sqlite.py index eef77ae..4d2233c 100644 --- a/blasta/sqlite.py +++ b/blasta/database/sqlite.py @@ -8,7 +8,7 @@ import time DBLOCK = Lock() -class SQLite: +class DatabaseSQLite: def instantiate_database(db_file): # db_dir = get_default_data_directory() @@ -17,8 +17,8 @@ class SQLite: # if not os.path.isdir(db_dir + "/sqlite"): # os.mkdir(db_dir + "/sqlite") # db_file = os.path.join(db_dir, "sqlite", r"{}.db".format(jid_file)) - SQLite.create_tables(db_file) - SQLite.add_statistics(db_file) + DatabaseSQLite.create_tables(db_file) + DatabaseSQLite.add_statistics(db_file) return db_file #from slixfeed.log import Logger @@ -75,7 +75,7 @@ class SQLite: function_name = sys._getframe().f_code.co_name # logger.debug('{}: db_file: {}' # .format(function_name, db_file)) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: sql_table_main_entries = ( """ CREATE TABLE IF NOT EXISTS main_entries ( @@ -510,7 +510,7 @@ class SQLite: ('tags'); """ ) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() try: cur.execute(sql) @@ -519,16 +519,16 @@ class SQLite: async def associate_entries_tags_jids(db_file, entry): async with DBLOCK: - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() jid = entry['jid'] url_hash = entry['url_hash'] - entry_id = SQLite.get_entry_id_by_url_hash(db_file, url_hash) - jid_id = SQLite.get_jid_id_by_jid(db_file, jid) + entry_id = DatabaseSQLite.get_entry_id_by_url_hash(db_file, url_hash) + jid_id = DatabaseSQLite.get_jid_id_by_jid(db_file, jid) if entry_id: for tag in entry['tags']: - tag_id = SQLite.get_tag_id_by_tag(db_file, tag) - cet_id = SQLite.get_combination_id_by_entry_id_tag_id_jid_id(db_file, entry_id, tag_id, jid_id) + tag_id = DatabaseSQLite.get_tag_id_by_tag(db_file, tag) + cet_id = DatabaseSQLite.get_combination_id_by_entry_id_tag_id_jid_id(db_file, entry_id, tag_id, jid_id) if not cet_id: sql = ( """ @@ -565,7 +565,7 @@ class SQLite: # logger.debug('{}: db_file: {}' # .format(function_name, db_file)) async with DBLOCK: - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() for entry in entries: tags = entry['tags'] @@ -575,7 +575,7 @@ class SQLite: # INSERT OR IGNORE INTO main_tags(tag) VALUES (?); # """ # ) - if not SQLite.get_tag_id_by_tag(db_file, tag): + if not DatabaseSQLite.get_tag_id_by_tag(db_file, tag): sql = ( """ INSERT INTO main_tags(tag) VALUES(?); @@ -607,7 +607,7 @@ class SQLite: # .format(function_name, db_file)) async with DBLOCK: - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() for entry in entries: @@ -621,7 +621,7 @@ class SQLite: # instances = entry['instances'] # Import entries - jid_id = SQLite.get_jid_id_by_jid(db_file, jid) + jid_id = DatabaseSQLite.get_jid_id_by_jid(db_file, jid) sql = ( """ INSERT @@ -672,7 +672,7 @@ class SQLite: ) par = (jid, ) async with DBLOCK: - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() try: cur.execute(sql, par) @@ -705,7 +705,7 @@ class SQLite: WHERE type = "entries"; """ ) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql).fetchone() return result[0] if result and len(result) == 1 else result @@ -745,7 +745,7 @@ class SQLite: "tag_id": tag_id, "jid_id": jid_id } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchone() return result[0] if result and len(result) == 1 else result @@ -783,7 +783,7 @@ class SQLite: """ ) async with DBLOCK: - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: for tag in tags: par = { "url_hash": url_hash, @@ -820,7 +820,7 @@ class SQLite: """ ) par = (tag,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchone() # return result[0] if result else None, None @@ -857,7 +857,7 @@ class SQLite: """ ) par = (url_hash,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -891,7 +891,7 @@ class SQLite: """ ) par = (entry_id,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -926,7 +926,7 @@ class SQLite: """ ) par = (entry_id,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -962,7 +962,7 @@ class SQLite: """ ) par = (url_hash,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -994,7 +994,7 @@ class SQLite: """ ) par = (tag,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchone() return result[0] if result and len(result) == 1 else result @@ -1026,7 +1026,7 @@ class SQLite: """ ) par = (url_hash,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchone() return result[0] if result and len(result) == 1 else result @@ -1058,7 +1058,7 @@ class SQLite: """ ) par = (url_hash,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchone() return result[0] if result and len(result) == 1 else result @@ -1090,7 +1090,7 @@ class SQLite: """ ) par = (url_hash,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchone() return result[0] if result and len(result) == 1 else result @@ -1125,7 +1125,7 @@ class SQLite: """ ) par = (index_first,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -1160,7 +1160,7 @@ class SQLite: """ ) par = (index_first,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -1195,7 +1195,7 @@ class SQLite: """ ) par = (index_first,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -1236,7 +1236,7 @@ class SQLite: "query": f'%{query}%', "index_first": index_first } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -1272,7 +1272,7 @@ class SQLite: par = { "query": f'%{query}%', } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchone() return result[0] if result and len(result) == 1 else result @@ -1319,7 +1319,7 @@ class SQLite: "tag": tag, "index_first": index_first } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -1360,7 +1360,7 @@ class SQLite: "jid": jid, "tag": tag } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchone() return result[0] if result and len(result) == 1 else result @@ -1406,7 +1406,7 @@ class SQLite: "query": f'%{query}%', "index_first": index_first } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -1446,7 +1446,7 @@ class SQLite: "jid": jid, "query": f'%{query}%' } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchone() return result[0] if result and len(result) == 1 else result @@ -1489,7 +1489,7 @@ class SQLite: "jid": jid, "index_first": index_first } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -1526,7 +1526,7 @@ class SQLite: par = { "jid": jid } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchone() return result[0] if result and len(result) == 1 else result @@ -1560,7 +1560,7 @@ class SQLite: """ ) par = (tag,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchone() return result[0] if result and len(result) == 1 else result @@ -1602,7 +1602,7 @@ class SQLite: "tag": tag, "index_first": index_first } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -1644,7 +1644,7 @@ class SQLite: "tag": tag, "index_first": index_first } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -1686,7 +1686,7 @@ class SQLite: "tag": tag, "index_first": index_first } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -1716,7 +1716,7 @@ class SQLite: LIMIT 30; """ ) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql).fetchall() return result @@ -1757,7 +1757,7 @@ class SQLite: """ ) par = (index_first,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -1804,7 +1804,7 @@ class SQLite: "tag": tag, "index_first": index_first } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -1851,7 +1851,7 @@ class SQLite: "tag": tag, "index_first": index_first } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -1898,7 +1898,7 @@ class SQLite: "tag": tag, "index_first": index_first } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -1939,7 +1939,7 @@ class SQLite: """ ) par = (index_first,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -1980,7 +1980,7 @@ class SQLite: """ ) par = (index_first,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -2027,7 +2027,7 @@ class SQLite: "query": f'%{query}%', "index_first": index_first } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -2079,7 +2079,7 @@ class SQLite: "tag": tag, "index_first": index_first } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -2131,7 +2131,7 @@ class SQLite: "query": f'%{query}%', "index_first": index_first } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -2178,7 +2178,7 @@ class SQLite: "jid": jid, "index_first": index_first } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -2213,7 +2213,7 @@ class SQLite: ORDER BY tag ASC; """ ) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql).fetchall() return result @@ -2251,7 +2251,7 @@ class SQLite: par = { "jid": jid } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -2290,7 +2290,7 @@ class SQLite: par = { "jid": jid } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -2331,7 +2331,7 @@ class SQLite: "jid": jid, "url_hash": url_hash } - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchone() return result[0] if result and len(result) == 1 else result @@ -2374,7 +2374,7 @@ class SQLite: "url_hash": url_hash } async with DBLOCK: - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() cur.execute(sql, par) @@ -2412,7 +2412,7 @@ class SQLite: """ ) par = (entry_id,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchall() return result @@ -2444,7 +2444,7 @@ class SQLite: """ ) par = (jid,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchone() return result[0] if result and len(result) == 1 else result @@ -2476,7 +2476,7 @@ class SQLite: """ ) par = (jid_id,) - with SQLite.create_connection(db_file) as conn: + with DatabaseSQLite.create_connection(db_file) as conn: cur = conn.cursor() result = cur.execute(sql, par).fetchone() return result[0] if result and len(result) == 1 else result diff --git a/blasta/helpers/utilities.py b/blasta/helpers/utilities.py deleted file mode 100644 index 53f6054..0000000 --- a/blasta/helpers/utilities.py +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -from datetime import datetime -import hashlib - -class Utilities: - - def convert_iso8601_to_readable(timestamp): - old_date_format = datetime.fromisoformat(timestamp.replace("Z", "+00:00")) - new_date_format = old_date_format.strftime("%B %d, %Y") - return new_date_format - - def hash_url_to_md5(url): - url_encoded = url.encode() - url_hashed = hashlib.md5(url_encoded) - url_digest = url_hashed.hexdigest() - return url_digest - - def is_jid_matches_to_session(accounts, sessions, request): - jabber_id = request.cookies.get('jabber_id') - session_key = request.cookies.get('session_key') - if (jabber_id and - jabber_id in accounts and - jabber_id in sessions and - session_key == sessions[jabber_id]): - return jabber_id diff --git a/blasta/http/instance.py b/blasta/http/instance.py index dff01da..370a545 100644 --- a/blasta/http/instance.py +++ b/blasta/http/instance.py @@ -3,10 +3,13 @@ import asyncio from blasta.config import Cache, Settings, Share -from blasta.helpers.data import Data -from blasta.helpers.utilities import Utilities -from blasta.sqlite import SQLite -from blasta.xml.syndication import Syndication +from blasta.database.sqlite import DatabaseSQLite +from blasta.utilities.cryptography import UtilitiesCryptography +from blasta.utilities.data import UtilitiesData +from blasta.utilities.date import UtilitiesDate +from blasta.utilities.http import UtilitiesHttp +from blasta.utilities.syndication import UtilitiesSyndication +from blasta.xmpp.form import DataForm from blasta.xmpp.instance import XmppInstance from blasta.xmpp.pubsub import XmppPubsub from datetime import datetime @@ -34,6 +37,8 @@ class HttpInstance: directory_data = Share.get_directory() directory_data_graphic = os.path.join(directory_data, 'graphic') + directory_data_graphic_ico = os.path.join(directory_data_graphic, 'blasta.ico') + directory_data_graphic_svg = os.path.join(directory_data_graphic, 'blasta.svg') directory_data_script = os.path.join(directory_data, 'script') directory_data_stylesheet = os.path.join(directory_data, 'stylesheet') directory_data_template = os.path.join(directory_data, 'template') @@ -52,7 +57,7 @@ class HttpInstance: directory_settings = Settings.get_directory() filename_settings = os.path.join(directory_settings, 'settings.toml') - data = Data.open_file_toml(filename_settings) + data = UtilitiesData.open_file_toml(filename_settings) contacts = data['contacts'] contact_email = contacts['email'] @@ -124,7 +129,7 @@ class HttpInstance: if param_hash: return RedirectResponse(url='/url/' + param_hash) if param_url: - url_hash = Utilities.hash_url_to_md5(param_url) + url_hash = UtilitiesCryptography.hash_url_to_md5(param_url) return RedirectResponse(url='/url/' + url_hash) response = await call_next(request) @@ -180,14 +185,14 @@ class HttpInstance: @self.app.exception_handler(401) def not_authorized_exception_handler(request: Request, exc: HTTPException): - #jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + #jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) message = 'Not Authorized.' description = 'Not Authorized (401)' return result_post(request, description, message) @self.app.exception_handler(403) def access_denied_exception_handler(request: Request, exc: HTTPException): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) message = 'Blasta system message » Access denied.' description = 'Access denied (403)' path = 'error' @@ -195,7 +200,7 @@ class HttpInstance: @self.app.exception_handler(404) def not_found_exception_handler(request: Request, exc: HTTPException): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) message = 'Blasta system message » Not Found.' description = 'Not found (404)' path = 'error' @@ -203,7 +208,7 @@ class HttpInstance: @self.app.exception_handler(405) def not_allowed_exception_handler(request: Request, exc: HTTPException): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) message = 'Blasta system message » Method Not Allowed.' description = 'Not allowed (405)' path = 'error' @@ -211,7 +216,7 @@ class HttpInstance: @self.app.exception_handler(500) def internal_error_exception_handler(request: Request, exc: HTTPException): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) message = 'Blasta system message » Internal Server Error.' description = 'Internal error (500)' path = 'error' @@ -220,7 +225,7 @@ class HttpInstance: # TODO @self.app.get('/admin') def admin_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) authorized = None if authorized: template_file = 'connect.xhtml' @@ -235,7 +240,7 @@ class HttpInstance: @self.app.get('/connect') def connect_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) if jabber_id: response = RedirectResponse(url='/jid/' + jabber_id) else: @@ -249,7 +254,7 @@ class HttpInstance: @self.app.get('/contact') def contact_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'contact.xhtml' template_dict = { 'contact_email' : contact_email, @@ -279,11 +284,11 @@ class HttpInstance: @self.app.get('/favicon.ico', include_in_schema=False) def favicon_get(): - return FileResponse('graphic/blasta.ico') + return FileResponse(directory_data_graphic_ico) @self.app.get('/help') def help_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'help.xhtml' template_dict = { 'request' : request, @@ -295,7 +300,7 @@ class HttpInstance: @self.app.get('/help/about') def help_about_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'about.xhtml' template_dict = { 'request' : request, @@ -307,7 +312,7 @@ class HttpInstance: @self.app.get('/help/about/folksonomy') def help_about_folksonomies_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'folksonomy.xhtml' template_dict = { 'request' : request, @@ -319,7 +324,7 @@ class HttpInstance: @self.app.get('/help/about/ideas') def help_about_ideas_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) protocol = request.url.scheme hostname = request.url.hostname + ':' + str(request.url.port) origin = protocol + '://' + hostname @@ -335,7 +340,7 @@ class HttpInstance: @self.app.get('/help/about/philosophy') def help_about_philosophy_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'philosophy.xhtml' template_dict = { 'request' : request, @@ -347,7 +352,7 @@ class HttpInstance: @self.app.get('/help/about/projects') def help_about_projects_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'projects.xhtml' template_dict = { 'request' : request, @@ -359,7 +364,7 @@ class HttpInstance: @self.app.get('/help/about/software') def help_about_software_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'software.xhtml' template_dict = { 'request' : request, @@ -371,7 +376,7 @@ class HttpInstance: @self.app.get('/help/about/thanks') def help_about_thanks_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'thanks.xhtml' template_dict = { 'request' : request, @@ -383,7 +388,7 @@ class HttpInstance: @self.app.get('/help/about/xmpp') def help_about_xmpp_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'xmpp.xhtml' template_dict = { 'request' : request, @@ -395,7 +400,7 @@ class HttpInstance: @self.app.get('/help/about/xmpp/atomsub') def help_about_xmpp_atomsub_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'atomsub.xhtml' template_dict = { 'request' : request, @@ -407,7 +412,7 @@ class HttpInstance: @self.app.get('/help/about/xmpp/libervia') def help_about_xmpp_libervia_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'libervia.xhtml' template_dict = { 'request' : request, @@ -419,7 +424,7 @@ class HttpInstance: @self.app.get('/help/about/xmpp/movim') def help_about_xmpp_movim_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'movim.xhtml' template_dict = { 'request' : request, @@ -432,8 +437,8 @@ class HttpInstance: @self.app.get('/help/about/xmpp/pubsub') def help_about_xmpp_pubsub_get(request: Request): date_now_iso = datetime.now().isoformat() - date_now_readable = Utilities.convert_iso8601_to_readable(date_now_iso) - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + date_now_readable = UtilitiesDate.convert_iso8601_to_readable(date_now_iso) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'pubsub.xhtml' template_dict = { 'request' : request, @@ -447,7 +452,7 @@ class HttpInstance: @self.app.get('/help/feeds') def help_about_feeds_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'feeds.xhtml' template_dict = { 'request' : request, @@ -459,7 +464,7 @@ class HttpInstance: @self.app.get('/help/questions') def help_questions_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'questions.xhtml' template_dict = { 'request' : request, @@ -474,7 +479,7 @@ class HttpInstance: hostname = request.url.hostname + ':' + str(request.url.port) protocol = request.url.scheme origin = protocol + '://' + hostname - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'syndication.xhtml' template_dict = { 'request' : request, @@ -488,7 +493,7 @@ class HttpInstance: @self.app.get('/help/utilities') def help_utilities_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) hostname = request.url.hostname protocol = request.url.scheme hostname = request.url.netloc @@ -509,11 +514,12 @@ class HttpInstance: async def jid_get(request: Request, response : Response): node_type = 'public' path = 'jid' - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) if jabber_id: xmpp_instance = accounts[jabber_id] node_id = nodes[node_type]['name'] - result, reason = await Data.update_cache_and_database(directory_cache, xmpp_instance, jabber_id, node_type, node_id) + result, reason = await UtilitiesData.update_cache_and_database( + db_file, directory_cache, xmpp_instance, jabber_id, node_type, node_id) if result == 'error': message = 'XMPP system message » {}.'.format(reason) description = 'IQ Error' @@ -565,7 +571,7 @@ class HttpInstance: page_prev = page - 1 index_first = (page - 1)*10 index_last = index_first+10 - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) if jabber_id == jid or node_type in ('private', 'read'): xmpp_instance = accounts[jabber_id] # NOTE You need something other than an iterator (XEP-0059). @@ -577,12 +583,12 @@ class HttpInstance: # See function "cache_items_and_tags_filter". if param_query: query = param_query - entries_cache = Data.open_file_toml(filename_items) + entries_cache = UtilitiesData.open_file_toml(filename_items) entries_cache_node = entries_cache[node_type] filename_cache = os.path.join(directory_cache, 'data', jid + '_query.toml') - Data.cache_items_and_tags_search(directory_cache, entries_cache_node, jid, query) + UtilitiesData.cache_items_and_tags_search(directory_cache, entries_cache_node, jid, query) if os.path.exists(filename_cache) and os.path.getsize(filename_cache): - data = Data.open_file_toml(filename_cache) + data = UtilitiesData.open_file_toml(filename_cache) item_ids_all = data['item_ids'] related_tags = data['tags'] if len(item_ids_all) <= index_last: @@ -597,7 +603,7 @@ class HttpInstance: if entry['url_hash'] == item_id: entries.append(entry) for entry in entries: - entry['published_mod'] = Utilities.convert_iso8601_to_readable(entry['published']) + entry['published_mod'] = UtilitiesDate.convert_iso8601_to_readable(entry['published']) entry['tags'] = entry['tags'][:5] description = 'Your {} bookmarks with "{}"'.format(node_type, query) message = 'Listing {} bookmarks {} - {} out of {}.'.format(node_type, index_first+1, index_last, len(item_ids_all)) @@ -611,12 +617,12 @@ class HttpInstance: tags_list = param_tags.split('+') if len(tags_list) == 1: tag = param_tags - entries_cache = Data.open_file_toml(filename_items) + entries_cache = UtilitiesData.open_file_toml(filename_items) entries_cache_node = entries_cache[node_type] filename_cache = os.path.join(directory_cache, 'data', jid, tag + '.toml') - Data.cache_items_and_tags_filter(directory_cache, entries_cache_node, jid, tag) + UtilitiesData.cache_items_and_tags_filter(directory_cache, entries_cache_node, jid, tag) if os.path.exists(filename_cache) and os.path.getsize(filename_cache): - data = Data.open_file_toml(filename_cache) + data = UtilitiesData.open_file_toml(filename_cache) item_ids_all = data['item_ids'] related_tags = data['tags'] if len(item_ids_all) <= index_last: @@ -631,7 +637,7 @@ class HttpInstance: if entry['url_hash'] == item_id: entries.append(entry) for entry in entries: - entry['published_mod'] = Utilities.convert_iso8601_to_readable(entry['published']) + entry['published_mod'] = UtilitiesDate.convert_iso8601_to_readable(entry['published']) entry['tags'] = entry['tags'][:5] description = 'Your {} bookmarks tagged with "{}"'.format(node_type, tag) message = 'Listing {} bookmarks {} - {} out of {}.'.format(node_type, index_first+1, index_last, len(item_ids_all)) @@ -646,13 +652,13 @@ class HttpInstance: # if not param_tags and not param_tld and not param_filetype and not param_protocol and not param_url and not param_hash: else: name = jabber_id.split('@')[0] - entries_cache = Data.open_file_toml(filename_items) + entries_cache = UtilitiesData.open_file_toml(filename_items) entries_cache_node = entries_cache[node_type] filename_cache = os.path.join(directory_cache, 'data', jabber_id + '.toml') #if len(entries_cache_node) and not os.path.exists(filename_cache): - Data.cache_items_and_tags(directory_cache, entries_cache_node, jabber_id) + UtilitiesData.cache_items_and_tags(directory_cache, entries_cache_node, jabber_id) if os.path.exists(filename_cache) and os.path.getsize(filename_cache): - data = Data.open_file_toml(filename_cache) + data = UtilitiesData.open_file_toml(filename_cache) item_ids_all = data['item_ids'] related_tags = data['tags'] if len(item_ids_all) <= index_last: @@ -667,7 +673,7 @@ class HttpInstance: if entry['url_hash'] == item_id: entries.append(entry) for entry in entries: - entry['published_mod'] = Utilities.convert_iso8601_to_readable(entry['published']) + entry['published_mod'] = UtilitiesDate.convert_iso8601_to_readable(entry['published']) entry['tags'] = entry['tags'][:5] description = 'Your {} bookmarks'.format(node_type) message = 'Listing {} bookmarks {} - {} out of {}.'.format(node_type, index_first+1, index_last, len(item_ids_all)) @@ -686,21 +692,21 @@ class HttpInstance: tags_dict = {} if param_query: description = 'Bookmarks from {} with "{}"'.format(jid, param_query) - entries_database = SQLite.get_entries_by_jid_and_query(db_file, jid, param_query, index_first) - entries_count = SQLite.get_entries_count_by_jid_and_query(db_file, jid, param_query) - for tag, instances in SQLite.get_30_tags_by_jid_and_query(db_file, jid, param_query, index_first): + entries_database = DatabaseSQLite.get_entries_by_jid_and_query(db_file, jid, param_query, index_first) + entries_count = DatabaseSQLite.get_entries_count_by_jid_and_query(db_file, jid, param_query) + for tag, instances in DatabaseSQLite.get_30_tags_by_jid_and_query(db_file, jid, param_query, index_first): tags_dict[tag] = instances elif param_tags: description = 'Bookmarks from {} tagged with "{}"'.format(jid, param_tags) - entries_database = SQLite.get_entries_by_jid_and_tag(db_file, jid, param_tags, index_first) - entries_count = SQLite.get_entries_count_by_jid_and_tag(db_file, jid, param_tags) - for tag, instances in SQLite.get_30_tags_by_jid_and_tag(db_file, jid, param_tags, index_first): + entries_database = DatabaseSQLite.get_entries_by_jid_and_tag(db_file, jid, param_tags, index_first) + entries_count = DatabaseSQLite.get_entries_count_by_jid_and_tag(db_file, jid, param_tags) + for tag, instances in DatabaseSQLite.get_30_tags_by_jid_and_tag(db_file, jid, param_tags, index_first): tags_dict[tag] = instances else: description = 'Bookmarks from {}'.format(jid) - entries_database = SQLite.get_entries_by_jid(db_file, jid, index_first) - entries_count = SQLite.get_entries_count_by_jid(db_file, jid) - for tag, instances in SQLite.get_30_tags_by_jid(db_file, jid, index_first): + entries_database = DatabaseSQLite.get_entries_by_jid(db_file, jid, index_first) + entries_count = DatabaseSQLite.get_entries_count_by_jid(db_file, jid) + for tag, instances in DatabaseSQLite.get_30_tags_by_jid(db_file, jid, index_first): tags_dict[tag] = instances if not entries_database: #message = 'Blasta system message » Error: No entries were found.' @@ -712,11 +718,11 @@ class HttpInstance: entries = [] for entry in entries_database: tags_sorted = [] - for tag in SQLite.get_tags_by_entry_id(db_file, entry[0]): + for tag in DatabaseSQLite.get_tags_by_entry_id(db_file, entry[0]): tags_sorted.append(tag[0]) - entry_jid = SQLite.get_jid_by_jid_id(db_file, entry[5]) - url_hash = Utilities.hash_url_to_md5(entry[2]) - instances = SQLite.get_entry_instances_by_url_hash(db_file, url_hash) + entry_jid = DatabaseSQLite.get_jid_by_jid_id(db_file, entry[5]) + url_hash = UtilitiesCryptography.hash_url_to_md5(entry[2]) + instances = DatabaseSQLite.get_entry_instances_by_url_hash(db_file, url_hash) entries.append( {'title' : entry[3], 'link' : entry[2], @@ -731,7 +737,7 @@ class HttpInstance: for entry in entries: try: date_iso = entry['published'] - date_wrt = Utilities.convert_iso8601_to_readable(date_iso) + date_wrt = UtilitiesDate.convert_iso8601_to_readable(date_iso) entry['published_mod'] = date_wrt except: print('ERROR: Probably due to an attempt to convert a non ISO 8601.') @@ -770,10 +776,10 @@ class HttpInstance: item_ids_selection.append(item_id) iq = await XmppPubsub.get_node_items(xmpp_instance, jid, node_id_public, item_ids_selection) - entries = Data.extract_iq_items_extra(iq, jid) + entries = UtilitiesData.extract_iq_items_extra(db_file, iq, jid) if entries: for entry in entries: - entry['published_mod'] = Utilities.convert_iso8601_to_readable(entry['published']) + entry['published_mod'] = UtilitiesDate.convert_iso8601_to_readable(entry['published']) message = 'Listing bookmarks {} - {} out of {}.'.format(index_first+1, index_last, len(item_ids_all)) description = 'Bookmarks from {}'.format(jid) else: @@ -838,7 +844,7 @@ class HttpInstance: message = 'Blasta system message » Please connect with your XMPP account to view this directory.' path = 'error' return result_post(request, jabber_id, description, message, path) - if not entries: raise HTTPException(status_code=404, detail='No entries were found') + #if not entries: raise HTTPException(status_code=404, detail='No entries were found') template_dict = { 'request': request, 'description': description, @@ -871,7 +877,7 @@ class HttpInstance: @self.app.get('/blasta.svg') def logo_get(): - return FileResponse('graphic/blasta.svg') + return FileResponse(directory_data_graphic_svg) @self.app.get('/', response_class=HTMLResponse) @self.app.get('/new', response_class=HTMLResponse) @@ -895,7 +901,7 @@ class HttpInstance: return response async def root_main_get(request: Request, response : Response, page_type=None): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) node_id = path = syndicate = page_type param_filetype = request.query_params.get('filetype', '') or None param_mode = request.query_params.get('mode', '') or None @@ -919,44 +925,44 @@ class HttpInstance: page_prev = page - 1 index_first = (page - 1)*10 if param_tags or param_tld or param_filetype or param_protocol: - entries_count = SQLite.get_entries_count_by_tag(db_file, param_tags) + entries_count = DatabaseSQLite.get_entries_count_by_tag(db_file, param_tags) match page_type: case 'new': description = 'New bookmarks tagged with "{}"'.format(param_tags) - entries_database = SQLite.get_entries_new_by_tag(db_file, param_tags, index_first) - tags_of_entries = SQLite.get_30_tags_by_entries_new_by_tag(db_file, param_tags, index_first) + entries_database = DatabaseSQLite.get_entries_new_by_tag(db_file, param_tags, index_first) + tags_of_entries = DatabaseSQLite.get_30_tags_by_entries_new_by_tag(db_file, param_tags, index_first) case 'popular': description = 'Popular bookmarks tagged with "{}"'.format(param_tags) # 'Most popular' - entries_database = SQLite.get_entries_popular_by_tag(db_file, param_tags, index_first) - tags_of_entries = SQLite.get_30_tags_by_entries_popular_by_tag(db_file, param_tags, index_first) + entries_database = DatabaseSQLite.get_entries_popular_by_tag(db_file, param_tags, index_first) + tags_of_entries = DatabaseSQLite.get_30_tags_by_entries_popular_by_tag(db_file, param_tags, index_first) case 'recent': description = 'Recent bookmarks tagged with "{}"'.format(param_tags) - entries_database = SQLite.get_entries_recent_by_tag(db_file, param_tags, index_first) - tags_of_entries = SQLite.get_30_tags_by_entries_recent_by_tag(db_file, param_tags, index_first) + entries_database = DatabaseSQLite.get_entries_recent_by_tag(db_file, param_tags, index_first) + tags_of_entries = DatabaseSQLite.get_30_tags_by_entries_recent_by_tag(db_file, param_tags, index_first) # TODO case 'query': else: match page_type: case 'new': description = 'New bookmarks' - entries_database = SQLite.get_entries_new(db_file, index_first) - tags_of_entries = SQLite.get_30_tags_by_entries_new(db_file, index_first) - entries_count = SQLite.get_entries_count(db_file) + entries_database = DatabaseSQLite.get_entries_new(db_file, index_first) + tags_of_entries = DatabaseSQLite.get_30_tags_by_entries_new(db_file, index_first) + entries_count = DatabaseSQLite.get_entries_count(db_file) case 'popular': description = 'Popular bookmarks' # 'Most popular' - entries_database = SQLite.get_entries_popular(db_file, index_first) - tags_of_entries = SQLite.get_30_tags_by_entries_popular(db_file, index_first) - entries_count = SQLite.get_entries_count(db_file) + entries_database = DatabaseSQLite.get_entries_popular(db_file, index_first) + tags_of_entries = DatabaseSQLite.get_30_tags_by_entries_popular(db_file, index_first) + entries_count = DatabaseSQLite.get_entries_count(db_file) case 'query': node_id = syndicate = 'new' description = 'Posted bookmarks with "{}"'.format(param_query) - entries_database = SQLite.get_entries_by_query(db_file, param_query, index_first) - tags_of_entries = SQLite.get_30_tags_by_entries_by_query_recent(db_file, param_query, index_first) - entries_count = SQLite.get_entries_count_by_query(db_file, param_query) + entries_database = DatabaseSQLite.get_entries_by_query(db_file, param_query, index_first) + tags_of_entries = DatabaseSQLite.get_30_tags_by_entries_by_query_recent(db_file, param_query, index_first) + entries_count = DatabaseSQLite.get_entries_count_by_query(db_file, param_query) case 'recent': description = 'Recent bookmarks' - entries_database = SQLite.get_entries_recent(db_file, index_first) - tags_of_entries = SQLite.get_30_tags_by_entries_recent(db_file, index_first) - entries_count = SQLite.get_entries_count(db_file) + entries_database = DatabaseSQLite.get_entries_recent(db_file, index_first) + tags_of_entries = DatabaseSQLite.get_30_tags_by_entries_recent(db_file, index_first) + entries_count = DatabaseSQLite.get_entries_count(db_file) if not entries_database: #message = 'Blasta system message » Error: No entries were found.' #description = 'No results' @@ -964,17 +970,17 @@ class HttpInstance: #return result_post(request, jabber_id, description, message, path) raise HTTPException(status_code=404, detail='No entries were found') tags_dict = {} - #for tag, instances in SQLite.get_tags_30(db_file): + #for tag, instances in DatabaseSQLite.get_tags_30(db_file): for tag, instances in tags_of_entries: tags_dict[tag] = instances entries = [] for entry in entries_database: tags_sorted = [] - for tag in SQLite.get_tags_by_entry_id(db_file, entry[0]): + for tag in DatabaseSQLite.get_tags_by_entry_id(db_file, entry[0]): tags_sorted.append(tag[0]) - jid = SQLite.get_jid_by_jid_id(db_file, entry[5]) - url_hash = Utilities.hash_url_to_md5(entry[2]) - instances = SQLite.get_entry_instances_by_url_hash(db_file, url_hash) + jid = DatabaseSQLite.get_jid_by_jid_id(db_file, entry[5]) + url_hash = UtilitiesCryptography.hash_url_to_md5(entry[2]) + instances = DatabaseSQLite.get_entry_instances_by_url_hash(db_file, url_hash) entries.append( {'title' : entry[3], 'link' : entry[2], @@ -989,7 +995,7 @@ class HttpInstance: for entry in entries: try: date_iso = entry['published'] - date_wrt = Utilities.convert_iso8601_to_readable(date_iso) + date_wrt = UtilitiesDate.convert_iso8601_to_readable(date_iso) entry['published_mod'] = date_wrt except: print('ERROR: Probably due to an attempt to convert a non ISO 8601.') @@ -1044,7 +1050,7 @@ class HttpInstance: # TODO Return to code /tag and / (root) once SQLite database is ready. @self.app.get('/tag/{tag}') async def tag_tag_get(request: Request, tag): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) node_id = 'tag:{}'.format(tag) syndicate = '?tag={}'.format(tag) path = 'tag' @@ -1052,7 +1058,7 @@ class HttpInstance: # tags ("category") of viewer to override the tags of Blasta # TODO If URL exist in visitor's bookmarks, display its properties # (summary, tags title etc.) before data of others. -# if Utilities.is_jid_matches_to_session(accounts, sessions, request): +# if UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request): page = request.query_params.get('page', '') or None if page: try: @@ -1092,7 +1098,7 @@ class HttpInstance: response: Response, jabber_id: str = Form(...), password: str = Form(...)): - if not Utilities.is_jid_matches_to_session(accounts, sessions, request): + if not UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request): # Store a variable in the request's state request.app.state.jabber_id = jabber_id session_key = str(random.random()) @@ -1132,7 +1138,8 @@ class HttpInstance: #configuration_form = await xmpp_instance['xep_0060'].get_node_config(jabber_id, properties['name']) #print(configuration_form) node_id = nodes['public']['name'] - result, reason = await Data.update_cache_and_database(directory_cache, xmpp_instance, jabber_id, 'public', node_id) + result, reason = await UtilitiesData.update_cache_and_database( + db_file, directory_cache, xmpp_instance, jabber_id, 'public', node_id) if result == 'error': message = 'XMPP system message » {}.'.format(reason) description = 'IQ Error' @@ -1168,7 +1175,7 @@ class HttpInstance: async def message_post(request: Request, jid: str = Form(...), body: str = Form(...)): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) if jabber_id: #headline = 'This is a message from Blasta' xmpp_instance = accounts[jabber_id] @@ -1185,7 +1192,7 @@ class HttpInstance: @self.app.get('/now') def now_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'now.xhtml' template_dict = { 'request' : request, @@ -1200,11 +1207,12 @@ class HttpInstance: async def private_get(request: Request, response : Response): node_type = 'private' path = 'private' - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) if jabber_id: xmpp_instance = accounts[jabber_id] node_id = nodes[node_type]['name'] - result, reason = await Data.update_cache_and_database(directory_cache, xmpp_instance, jabber_id, node_type, node_id) + result, reason = await UtilitiesData.update_cache_and_database( + db_file, directory_cache, xmpp_instance, jabber_id, node_type, node_id) if result == 'error': message = 'Blasta system message » {}.'.format(reason) description = 'Directory "private" appears to be empty' @@ -1221,7 +1229,7 @@ class HttpInstance: @self.app.get('/profile') async def profile_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) if jabber_id: xmpp_instance = accounts[jabber_id] if not await XmppPubsub.is_node_exist(xmpp_instance, 'xmpp:blasta:configuration:0'): @@ -1263,18 +1271,18 @@ class HttpInstance: async def profile_post(request: Request, routine: str = Form(None), enroll: str = Form(None)): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) if jabber_id: xmpp_instance = accounts[jabber_id] if routine: message = 'The routine directory has been set to {}'.format(routine) - payload = Xml.create_setting_entry(xmpp_instance, 'routine', routine) + payload = DataForm.create_setting_entry(xmpp_instance, 'routine', routine) iq = await XmppPubsub.publish_node_item( # NOTE Consider "configurations" as item ID (see Movim) xmpp_instance, jabber_id, 'xmpp:blasta:configuration:0', 'routine', payload) if enroll: if enroll == '1': message = 'Your database is shared with the Blasta system' else: message = 'Your database is excluded from the Blasta system' - payload = Xml.create_setting_entry(xmpp_instance, 'enroll', enroll) + payload = DataForm.create_setting_entry(xmpp_instance, 'enroll', enroll) iq = await XmppPubsub.publish_node_item( xmpp_instance, jabber_id, 'xmpp:blasta:configuration:0', 'enrollment', payload) description = 'Setting has been saved' @@ -1298,13 +1306,13 @@ class HttpInstance: @self.app.get('/profile/export/{node_type}/{filetype}') async def profile_export_get(request: Request, node_type, filetype): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) if jabber_id: xmpp_instance = accounts[jabber_id] node_id = nodes[node_type]['name'] iq = await XmppPubsub.get_node_items(xmpp_instance, jabber_id, node_id) if isinstance(iq, Iq): - entries = Data.extract_iq_items(iq, jabber_id) + entries = UtilitiesData.extract_iq_items(iq, jabber_id) # TODO Append a bookmark or bookmarks of Blasta if entries: filename = os.path.join(directory_cache, 'export', jabber_id + '_' + node_type + '.' + filetype) @@ -1314,11 +1322,11 @@ class HttpInstance: #filename = 'export_{}/{}.{}'.format(node_type, jabber_id, filetype) match filetype: case 'json': - Data.save_to_json(filename, entries) + UtilitiesData.save_to_json(filename, entries) case 'toml': # NOTE Should the dict be named with 'entries' or 'private'/'public'/'read'? data = {'entries' : entries} - Data.save_to_toml(filename, data) + UtilitiesData.save_to_toml(filename, data) response = FileResponse(filename) else: message = 'Blasta system message » Error: No active session.' @@ -1334,7 +1342,7 @@ class HttpInstance: merge: str = Form(None), node: str = Form(...), override: str = Form(None)): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) if jabber_id: xmpp_instance = accounts[jabber_id] if file: @@ -1370,8 +1378,8 @@ class HttpInstance: for entry_type in entries: for entry in entries[entry_type]: - url_hash = item_id = Utilities.hash_url_to_md5(entry['link']) - instances = SQLite.get_entry_instances_by_url_hash(db_file, url_hash) + url_hash = item_id = UtilitiesCryptography.hash_url_to_md5(entry['link']) + instances = DatabaseSQLite.get_entry_instances_by_url_hash(db_file, url_hash) entry_new = { 'title' : entry['title'], 'link' : entry['link'], @@ -1408,11 +1416,11 @@ class HttpInstance: @self.app.get('/save') async def save_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) if jabber_id: xmpp_instance = accounts[jabber_id] param_url = request.query_params.get('url', '') - url_hash = Utilities.hash_url_to_md5(param_url) + url_hash = UtilitiesCryptography.hash_url_to_md5(param_url) for node_type in nodes: node_id = nodes[node_type]['name'] iq = await XmppPubsub.get_node_item(xmpp_instance, jabber_id, node_id, url_hash) @@ -1470,10 +1478,10 @@ class HttpInstance: tags: str = Form(''), title: str = Form(...), url: str = Form(...)): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) if jabber_id: xmpp_instance = accounts[jabber_id] - url_hash = Utilities.hash_url_to_md5(url) + url_hash = UtilitiesCryptography.hash_url_to_md5(url) for node_type in nodes: node_id = nodes[node_type]['name'] iq = await XmppPubsub.get_node_item( @@ -1513,11 +1521,12 @@ class HttpInstance: async def read_get(request: Request, response : Response): node_type = 'read' path = 'read' - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) if jabber_id: xmpp_instance = accounts[jabber_id] node_id = nodes[node_type]['name'] - result, reason = await Data.update_cache_and_database(directory_cache, xmpp_instance, jabber_id, node_type, node_id) + result, reason = await UtilitiesData.update_cache_and_database( + db_file, directory_cache, xmpp_instance, jabber_id, node_type, node_id) if result == 'error': message = 'Blasta system message » {}.'.format(reason) description = 'Directory "read" appears to be empty' @@ -1547,7 +1556,7 @@ class HttpInstance: @self.app.get('/register') def register_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) template_file = 'register.xhtml' template_dict = { 'request' : request, @@ -1568,7 +1577,7 @@ class HttpInstance: @self.app.get('/search/all') async def search_all_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) description = 'Search for public bookmarks' form_action = '/query' input_id = input_name = label_for = 'q' @@ -1598,7 +1607,7 @@ class HttpInstance: @self.app.get('/search/jid/{jid}') async def search_jid_get(request: Request, jid): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) if jabber_id: if jabber_id == jid: description = 'Search your own bookmarks' @@ -1636,7 +1645,7 @@ class HttpInstance: @self.app.get('/search/url') async def search_url_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) description = 'Search for a bookmark' form_action = None # This is not relevant due to function middleware. Maybe / or /url. input_id = input_name = label_for = 'url' @@ -1666,8 +1675,8 @@ class HttpInstance: @self.app.get('/tag') def tag_get(request: Request): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) - tag_list = SQLite.get_tags_500(db_file) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) + tag_list = DatabaseSQLite.get_tags_500(db_file) message = 'Common 500 tags sorted by name and sized by commonality.' description = 'Common tags' template_file = 'tag.xhtml' @@ -1684,11 +1693,11 @@ class HttpInstance: @self.app.get('/tag/{jid}') def tag_get_jid(request: Request, jid): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) # NOTE Consider retrieval of tags from cache file. # This is relevant to private and read nodes. #if jabber_id == jid or node_type in ('private', 'read'): - tag_list = SQLite.get_500_tags_by_jid_sorted_by_name(db_file, jid) + tag_list = DatabaseSQLite.get_500_tags_by_jid_sorted_by_name(db_file, jid) message = 'Common 500 tags sorted by name and sized by commonality.' description = 'Common tags of {}'.format(jid) template_file = 'tag.xhtml' @@ -1711,7 +1720,7 @@ class HttpInstance: @self.app.get('/url/{url_hash}') async def url_hash_get(request: Request, url_hash): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) node_id = 'hash:{}'.format(url_hash) param_hash = url_hash syndicate = path = 'url' @@ -1743,19 +1752,19 @@ class HttpInstance: entry = Syndication.extract_items(item_payload, limit=True) if entry: #url_hash = iq_item['id'] - url_hash = Utilities.hash_url_to_md5(entry['link']) + url_hash = UtilitiesCryptography.hash_url_to_md5(entry['link']) # TODO Add a check: if iq_item['id'] == url_hash: - instances = SQLite.get_entry_instances_by_url_hash(db_file, url_hash) + instances = DatabaseSQLite.get_entry_instances_by_url_hash(db_file, url_hash) entry['instances'] = instances entry['jid'] = jabber_id name = jabber_id.split('@')[0] entry['name'] = name entry['url_hash'] = url_hash - entry['published_mod'] = Utilities.convert_iso8601_to_readable(entry['published']) + entry['published_mod'] = UtilitiesDate.convert_iso8601_to_readable(entry['published']) #entry['tags'] = entry['tags'][:5] entries.append(entry) tags_list = {} - tags_and_instances = SQLite.get_tags_and_instances_by_url_hash(db_file, url_hash) + tags_and_instances = DatabaseSQLite.get_tags_and_instances_by_url_hash(db_file, url_hash) for tag, tag_instances in tags_and_instances: tags_list[tag] = tag_instances else: # NOTE Is it possible to activate this else statement? Consider removal. # https://fastapi.tiangolo.com/tutorial/handling-errors/ @@ -1766,22 +1775,22 @@ class HttpInstance: return result_post(request, jabber_id, description, message, path) return response else: - entry = SQLite.get_entry_by_url_hash(db_file, url_hash) + entry = DatabaseSQLite.get_entry_by_url_hash(db_file, url_hash) tags_sorted = [] if entry: - for tag in SQLite.get_tags_by_entry_id(db_file, entry[0]): + for tag in DatabaseSQLite.get_tags_by_entry_id(db_file, entry[0]): tags_sorted.append(tag[0]) tags_list = {} - tags_and_instances = SQLite.get_tags_and_instances_by_entry_id(db_file, entry[0]) + tags_and_instances = DatabaseSQLite.get_tags_and_instances_by_entry_id(db_file, entry[0]) for tag, tag_instances in tags_and_instances: tags_list[tag] = tag_instances - jid = SQLite.get_jid_by_jid_id(db_file, entry[5]) - instances = SQLite.get_entry_instances_by_url_hash(db_file, url_hash) + jid = DatabaseSQLite.get_jid_by_jid_id(db_file, entry[5]) + instances = DatabaseSQLite.get_entry_instances_by_url_hash(db_file, url_hash) entries.append( {'title' : entry[3], 'link' : entry[2], 'summary' : entry[4], 'published' : entry[6], - 'published_mod' : Utilities.convert_iso8601_to_readable(entry[6]), + 'published_mod' : UtilitiesDate.convert_iso8601_to_readable(entry[6]), 'updated' : entry[7], 'tags' : tags_sorted, 'url_hash' : url_hash, @@ -1802,22 +1811,22 @@ class HttpInstance: return result_post(request, jabber_id, description, message, path) return response else: - entry = SQLite.get_entry_by_url_hash(db_file, url_hash) + entry = DatabaseSQLite.get_entry_by_url_hash(db_file, url_hash) if entry: tags_sorted = [] - for tag in SQLite.get_tags_by_entry_id(db_file, entry[0]): + for tag in DatabaseSQLite.get_tags_by_entry_id(db_file, entry[0]): tags_sorted.append(tag[0]) tags_list = {} - tags_and_instances = SQLite.get_tags_and_instances_by_entry_id(db_file, entry[0]) + tags_and_instances = DatabaseSQLite.get_tags_and_instances_by_entry_id(db_file, entry[0]) for tag, tag_instances in tags_and_instances: tags_list[tag] = tag_instances - jid = SQLite.get_jid_by_jid_id(db_file, entry[5]) - instances = SQLite.get_entry_instances_by_url_hash(db_file, url_hash) + jid = DatabaseSQLite.get_jid_by_jid_id(db_file, entry[5]) + instances = DatabaseSQLite.get_entry_instances_by_url_hash(db_file, url_hash) entries.append( {'title' : entry[3], 'link' : entry[2], 'summary' : entry[4], 'published' : entry[6], - 'published_mod' : Utilities.convert_iso8601_to_readable(entry[6]), + 'published_mod' : UtilitiesDate.convert_iso8601_to_readable(entry[6]), 'updated' : entry[7], 'tags' : tags_sorted, 'url_hash' : url_hash, @@ -1837,7 +1846,7 @@ class HttpInstance: description = 'Discover new resources and see who shares them' template_file = 'people.xhtml' people_list = {} - jids_and_tags = SQLite.get_jids_and_tags_by_url_hash(db_file, url_hash) + jids_and_tags = DatabaseSQLite.get_jids_and_tags_by_url_hash(db_file, url_hash) for jid, tag in jids_and_tags: if jid in people_list and isinstance(people_list[jid], list): people_list[jid].append(tag) @@ -1885,12 +1894,12 @@ class HttpInstance: node_id = 'hash:{}'.format(url_hash) param_hash = url_hash syndicate = path = 'url' - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) if jabber_id: name = jabber_id.split('@')[0] - instances = SQLite.get_entry_instances_by_url_hash(db_file, url_hash) + instances = DatabaseSQLite.get_entry_instances_by_url_hash(db_file, url_hash) timestamp = datetime.now().isoformat() - tags_new = Data.organize_tags(tags) if tags else '' + tags_new = UtilitiesData.organize_tags(tags) if tags else '' entry = {'title' : title.strip(), 'link' : url.strip(), 'summary' : summary.strip() if summary else '', @@ -1918,21 +1927,21 @@ class HttpInstance: #iq = await XmppPubsub.publish_node_item_private( # xmpp_instance, node_id_private, url_hash, iq) await XmppPubsub.del_node_item(xmpp_instance, jabber_id, node_id_public, url_hash) - Data.remove_item_from_cache(directory_cache, jabber_id, 'public', url_hash) + UtilitiesData.remove_item_from_cache(directory_cache, jabber_id, 'public', url_hash) await XmppPubsub.del_node_item(xmpp_instance, jabber_id, node_id_read, url_hash) - Data.remove_item_from_cache(directory_cache, jabber_id, 'read', url_hash) + UtilitiesData.remove_item_from_cache(directory_cache, jabber_id, 'read', url_hash) case 'public': await XmppPubsub.del_node_item(xmpp_instance, jabber_id, node_id_private, url_hash) - Data.remove_item_from_cache(directory_cache, jabber_id, 'private', url_hash) + UtilitiesData.remove_item_from_cache(directory_cache, jabber_id, 'private', url_hash) await XmppPubsub.del_node_item(xmpp_instance, jabber_id, node_id_read, url_hash) - Data.remove_item_from_cache(directory_cache, jabber_id, 'read', url_hash) + UtilitiesData.remove_item_from_cache(directory_cache, jabber_id, 'read', url_hash) case 'read': #iq = await XmppPubsub.publish_node_item_private( # xmpp_instance, node_id_read, url_hash, iq) await XmppPubsub.del_node_item(xmpp_instance, jabber_id, node_id_public, url_hash) - Data.remove_item_from_cache(directory_cache, jabber_id, 'public', url_hash) + UtilitiesData.remove_item_from_cache(directory_cache, jabber_id, 'public', url_hash) await XmppPubsub.del_node_item(xmpp_instance, jabber_id, node_id_private, url_hash) - Data.remove_item_from_cache(directory_cache, jabber_id, 'private', url_hash) + UtilitiesData.remove_item_from_cache(directory_cache, jabber_id, 'private', url_hash) if isinstance(iq, str): description = 'Could not save bookmark' message = 'XMPP system message » {}.'.format(iq) @@ -1941,7 +1950,7 @@ class HttpInstance: #await iq.send(timeout=15) # Save changes to cache file entries_cache_filename = os.path.join(directory_cache, 'items', jabber_id + '.toml') - entries_cache = Data.open_file_toml(entries_cache_filename) + entries_cache = UtilitiesData.open_file_toml(entries_cache_filename) entries_cache_node = entries_cache[node] if node in entries_cache else [] entries_cache_mod = [] #for entry_cache in entries_cache_node: @@ -1960,7 +1969,7 @@ class HttpInstance: if not is_entry_modified: entries_cache_mod.append(entry) entries_cache[node] = entries_cache_mod entries_cache_data = entries_cache - Data.save_to_toml(entries_cache_filename, entries_cache_data) + UtilitiesData.save_to_toml(entries_cache_filename, entries_cache_data) # Save changes to database if node == 'public': tags_valid = [] @@ -1980,16 +1989,16 @@ class HttpInstance: # FIXME Variable tags_valid is not in use. # NOTE Variable tags_valid might not be needed. See function associate_entries_tags_jids. entry['tags'] = tags_valid - await SQLite.add_tags(db_file, [entry]) + await DatabaseSQLite.add_tags(db_file, [entry]) # Slow (high I/O) - entry_id = SQLite.get_entry_id_by_url_hash(db_file, url_hash) + entry_id = DatabaseSQLite.get_entry_id_by_url_hash(db_file, url_hash) if not entry_id: - await SQLite.add_new_entries(db_file, [entry]) # Is this line needed? - await SQLite.associate_entries_tags_jids(db_file, entry) - #elif not SQLite.is_jid_associated_with_url_hash(db_file, jabber_id, url_hash): - # await SQLite.associate_entries_tags_jids(db_file, entry) + await DatabaseSQLite.add_new_entries(db_file, [entry]) # Is this line needed? + await DatabaseSQLite.associate_entries_tags_jids(db_file, entry) + #elif not DatabaseSQLite.is_jid_associated_with_url_hash(db_file, jabber_id, url_hash): + # await DatabaseSQLite.associate_entries_tags_jids(db_file, entry) else: - await SQLite.associate_entries_tags_jids(db_file, entry) + await DatabaseSQLite.associate_entries_tags_jids(db_file, entry) print('tags_new') print(tags_new) print('tags_old') @@ -2000,10 +2009,10 @@ class HttpInstance: print(tags_invalid) print(url_hash) print(jabber_id) - await SQLite.delete_combination_row_by_url_hash_and_tag_and_jid(db_file, url_hash, tags_invalid, jabber_id) + await DatabaseSQLite.delete_combination_row_by_url_hash_and_tag_and_jid(db_file, url_hash, tags_invalid, jabber_id) # Entry for HTML - entry['published_mod'] = Utilities.convert_iso8601_to_readable(published) - entry['updated_mod'] = Utilities.convert_iso8601_to_readable(timestamp) + entry['published_mod'] = UtilitiesDate.convert_iso8601_to_readable(published) + entry['updated_mod'] = UtilitiesDate.convert_iso8601_to_readable(timestamp) entry['tags'] = entry['tags'][:5] entries = [entry] template_file = 'browse.xhtml' @@ -2032,7 +2041,7 @@ class HttpInstance: @self.app.get('/url/{url_hash}/confirm') async def url_hash_confirm_get(request: Request, url_hash): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) node_id = 'hash:{}'.format(url_hash) param_hash = url_hash syndicate = path = 'url' @@ -2062,13 +2071,13 @@ class HttpInstance: # TODO Add a check: if iq_item['id'] == url_hash: entries = [] entry = Syndication.extract_items(item_payload) - instances = SQLite.get_entry_instances_by_url_hash(db_file, url_hash) + instances = DatabaseSQLite.get_entry_instances_by_url_hash(db_file, url_hash) entry['instances'] = instances entry['jid'] = jabber_id name = jabber_id.split('@')[0] entry['name'] = name entry['url_hash'] = url_hash - entry['published_mod'] = Utilities.convert_iso8601_to_readable(entry['published']) + entry['published_mod'] = UtilitiesDate.convert_iso8601_to_readable(entry['published']) entries.append(entry) description = 'Confirm deletion of a bookmark' message = 'Details for bookmark {}'.format(entries[0]['link']) @@ -2104,7 +2113,7 @@ class HttpInstance: @self.app.get('/url/{url_hash}/delete') async def url_hash_delete_get(request: Request, url_hash): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) node_id = 'hash:{}'.format(url_hash) param_hash = url_hash syndicate = path = 'url' @@ -2134,13 +2143,13 @@ class HttpInstance: # TODO Add a check: if iq_item['id'] == url_hash: entries = [] entry = Syndication.extract_items(item_payload) - instances = SQLite.get_entry_instances_by_url_hash(db_file, url_hash) + instances = DatabaseSQLite.get_entry_instances_by_url_hash(db_file, url_hash) entry['instances'] = instances entry['jid'] = jabber_id name = jabber_id.split('@')[0] entry['name'] = name entry['url_hash'] = url_hash - entry['published_mod'] = Utilities.convert_iso8601_to_readable(entry['published']) + entry['published_mod'] = UtilitiesDate.convert_iso8601_to_readable(entry['published']) entries.append(entry) # Set a title @@ -2159,11 +2168,11 @@ class HttpInstance: await XmppPubsub.del_node_item(xmpp_instance, jabber_id, node_id, url_hash) # Remove the item association from database - await SQLite.delete_combination_row_by_jid_and_url_hash(db_file, url_hash, jabber_id) - #await SQLite.delete_combination_row_by_url_hash_and_tag_and_jid(db_file, url_hash, entry['tags'], jabber_id) + await DatabaseSQLite.delete_combination_row_by_jid_and_url_hash(db_file, url_hash, jabber_id) + #await DatabaseSQLite.delete_combination_row_by_url_hash_and_tag_and_jid(db_file, url_hash, entry['tags'], jabber_id) # Remove the item from cache - Data.remove_item_from_cache(directory_cache, jabber_id, node_type, url_hash) + UtilitiesData.remove_item_from_cache(directory_cache, jabber_id, node_type, url_hash) template_file = 'browse.xhtml' template_dict = { @@ -2199,7 +2208,7 @@ class HttpInstance: @self.app.get('/url/{url_hash}/edit') @self.app.post('/url/{url_hash}/edit') async def url_hash_edit_get(request: Request, url_hash): - jabber_id = Utilities.is_jid_matches_to_session(accounts, sessions, request) + jabber_id = UtilitiesHttp.is_jid_matches_to_session(accounts, sessions, request) # node_id = 'hash:{}'.format(url_hash) if len(url_hash) == 32: if jabber_id: @@ -2231,7 +2240,7 @@ class HttpInstance: path = 'edit' description = 'Edit an existing bookmark' entry = Syndication.extract_items(item_payload) - entry['instances'] = SQLite.get_entry_instances_by_url_hash(db_file, url_hash) + entry['instances'] = DatabaseSQLite.get_entry_instances_by_url_hash(db_file, url_hash) print(jabber_id) print(entry['tags']) else: @@ -2239,10 +2248,10 @@ class HttpInstance: # NOTE This seems to be the best to do, albeit, perhaps the pathname should be /save instead of /url/hash/edit. path = 'save' # 'add' description = 'Add a new bookmark' - result = SQLite.get_entry_by_url_hash(db_file, url_hash) + result = DatabaseSQLite.get_entry_by_url_hash(db_file, url_hash) tags_sorted = [] if result: - for tag in SQLite.get_tags_by_entry_id(db_file, result[0]): + for tag in DatabaseSQLite.get_tags_by_entry_id(db_file, result[0]): tags_sorted.append(tag[0]) entry = {'title' : result[3], 'link' : result[2], diff --git a/blasta/utilities/cryptography.py b/blasta/utilities/cryptography.py new file mode 100644 index 0000000..2312a87 --- /dev/null +++ b/blasta/utilities/cryptography.py @@ -0,0 +1,12 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import hashlib + +class UtilitiesCryptography: + + def hash_url_to_md5(url): + url_encoded = url.encode() + url_hashed = hashlib.md5(url_encoded) + url_digest = url_hashed.hexdigest() + return url_digest diff --git a/blasta/helpers/data.py b/blasta/utilities/data.py similarity index 95% rename from blasta/helpers/data.py rename to blasta/utilities/data.py index 61e9cfa..8bdc56c 100644 --- a/blasta/helpers/data.py +++ b/blasta/utilities/data.py @@ -1,9 +1,9 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -from blasta.helpers.utilities import Utilities -from blasta.sqlite import SQLite -from blasta.xml.syndication import Syndication +from blasta.database.sqlite import DatabaseSQLite +from blasta.utilities.cryptography import UtilitiesCryptography +from blasta.utilities.syndication import UtilitiesSyndication from blasta.xmpp.pubsub import XmppPubsub import os from slixmpp.stanza.iq import Iq @@ -14,7 +14,7 @@ try: except: import tomli as tomllib -class Data: +class UtilitiesData: def cache_items_and_tags_search(directory_cache, entries, jid, query): """Create a cache file of node items and tags.""" @@ -60,7 +60,7 @@ class Data: directory = os.path.join(directory_cache, 'data', jid) if not os.path.exists(directory): os.mkdir(directory) - filename = os.path.join(directory, tag) + filename = os.path.join(directory, tag + '.toml') # Add support for search query #filename = 'data/{}/query:{}.toml'.format(jid, query) #filename = 'data/{}/tag:{}.toml'.format(jid, tag) @@ -103,19 +103,18 @@ class Data: entries.reverse() return entries - def extract_iq_items_extra(iq, jabber_id, limit=None): + def extract_iq_items_extra(db_file, iq, jabber_id, limit=None): iq_items = iq['pubsub']['items'] entries = [] name = jabber_id.split('@')[0] for iq_item in iq_items: item_payload = iq_item['payload'] entry = Syndication.extract_items(item_payload, limit) - url_hash = Utilities.hash_url_to_md5(entry['link']) + url_hash = UtilitiesCryptography.hash_url_to_md5(entry['link']) iq_item_id = iq_item['id'] if iq_item_id != url_hash: logging.error('Item ID does not match MD5. id: {} hash: {}'.format(iq_item_id, url_hash)) logging.warn('Item ID does not match MD5. id: {} hash: {}'.format(iq_item_id, url_hash)) - db_file = 'main.sqlite' instances = SQLite.get_entry_instances_by_url_hash(db_file, url_hash) if entry: entry['instances'] = instances or 0 @@ -166,7 +165,8 @@ class Data: data_as_string = tomli_w.dumps(data) fn.write(data_as_string) - async def update_cache_and_database(directory_cache, xmpp_instance, jabber_id: str, node_type: str, node_id: str): + async def update_cache_and_database( + db_file, directory_cache, xmpp_instance, jabber_id: str, node_type: str, node_id: str): # Download identifiers of node items. iq = await XmppPubsub.get_node_item_ids(xmpp_instance, jabber_id, node_id) if isinstance(iq, Iq): @@ -196,7 +196,6 @@ class Data: entries_cache = Data.open_file_toml(filename_items) if not node_type in entries_cache: return ['error', 'Directory "{}" is empty'. format(node_type)] entries_cache_node = entries_cache[node_type] - db_file = 'main.sqlite' # Check whether items still exist on node for entry in entries_cache_node: diff --git a/blasta/utilities/date.py b/blasta/utilities/date.py new file mode 100644 index 0000000..2f21eed --- /dev/null +++ b/blasta/utilities/date.py @@ -0,0 +1,11 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +from datetime import datetime + +class UtilitiesDate: + + def convert_iso8601_to_readable(timestamp): + old_date_format = datetime.fromisoformat(timestamp.replace("Z", "+00:00")) + new_date_format = old_date_format.strftime("%B %d, %Y") + return new_date_format diff --git a/blasta/utilities/http.py b/blasta/utilities/http.py new file mode 100644 index 0000000..2f35cec --- /dev/null +++ b/blasta/utilities/http.py @@ -0,0 +1,13 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +class UtilitiesHttp: + + def is_jid_matches_to_session(accounts, sessions, request): + jabber_id = request.cookies.get('jabber_id') + session_key = request.cookies.get('session_key') + if (jabber_id and + jabber_id in accounts and + jabber_id in sessions and + session_key == sessions[jabber_id]): + return jabber_id diff --git a/blasta/xml/syndication.py b/blasta/utilities/syndication.py similarity index 99% rename from blasta/xml/syndication.py rename to blasta/utilities/syndication.py index fcd48fe..25638aa 100644 --- a/blasta/xml/syndication.py +++ b/blasta/utilities/syndication.py @@ -3,7 +3,7 @@ import xml.etree.ElementTree as ET -class Syndication: +class UtilitiesSyndication: def create_rfc4287_entry(feed_entry): node_entry = ET.Element('entry') diff --git a/blasta/helpers/xml.py b/blasta/xmpp/form.py similarity index 82% rename from blasta/helpers/xml.py rename to blasta/xmpp/form.py index 4d66663..3aa4ba7 100644 --- a/blasta/helpers/xml.py +++ b/blasta/xmpp/form.py @@ -1,13 +1,12 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -class Xml: +class DataForm: def create_setting_entry(xmpp_instance, key : str, value : str): form = xmpp_instance['xep_0004'].make_form('form', 'Settings') form['type'] = 'result' - form.add_field(var=key, - value=value) + form.add_field(var=key, value=value) return form # def create_setting_entry(value : str): diff --git a/pyproject.toml b/pyproject.toml index fc27fff..30e9b6e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,9 @@ classifiers = [ "Topic :: Office/Business :: News/Diary", ] keywords = [ + "annotation", "atom", + "bibliography", "bookmark", "collaboration", "gemini", @@ -37,11 +39,13 @@ keywords = [ dependencies = [ "fastapi", + "jinja2", "lxml", "python-dateutil", "python-multipart", "slixmpp", "tomli", # Python 3.10 + "tomli-w", "uvicorn", ]