2023-07-16 17:23:44 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2023-11-13 14:45:10 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
TODO
|
|
|
|
|
2024-01-10 21:06:56 +01:00
|
|
|
1) Function to open connection (receive db_file).
|
|
|
|
Function to close connection.
|
|
|
|
All other functions to receive cursor.
|
2023-11-13 14:45:10 +01:00
|
|
|
|
2024-01-10 21:06:56 +01:00
|
|
|
2) Merge function add_metadata into function import_feeds.
|
2023-11-13 14:45:10 +01:00
|
|
|
"""
|
|
|
|
|
2023-12-04 15:41:02 +01:00
|
|
|
from asyncio import Lock
|
2023-07-16 17:23:44 +02:00
|
|
|
from datetime import date
|
2024-01-04 13:38:22 +01:00
|
|
|
import logging
|
2023-12-28 15:50:23 +01:00
|
|
|
import slixfeed.config as config
|
|
|
|
# from slixfeed.data import join_url
|
2023-12-29 01:45:01 +01:00
|
|
|
from slixfeed.datetime import (
|
|
|
|
current_time,
|
|
|
|
rfc2822_to_iso8601
|
|
|
|
)
|
2024-01-06 23:03:08 +01:00
|
|
|
from sqlite3 import connect, Error, IntegrityError
|
2024-01-02 19:11:36 +01:00
|
|
|
from slixfeed.url import join_url
|
2023-07-16 17:23:44 +02:00
|
|
|
|
2023-09-29 13:49:24 +02:00
|
|
|
# from eliot import start_action, to_file
|
2023-11-13 14:45:10 +01:00
|
|
|
# # with start_action(action_type="list_feeds()", db=db_file):
|
2023-09-29 13:49:24 +02:00
|
|
|
# # with start_action(action_type="last_entries()", num=num):
|
2023-11-13 14:45:10 +01:00
|
|
|
# # with start_action(action_type="get_feeds()"):
|
2023-09-29 13:49:24 +02:00
|
|
|
# # with start_action(action_type="remove_entry()", source=source):
|
|
|
|
# # with start_action(action_type="search_entries()", query=query):
|
|
|
|
# # with start_action(action_type="check_entry()", link=link):
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
# aiosqlite
|
2023-12-04 15:41:02 +01:00
|
|
|
DBLOCK = Lock()
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
CURSORS = {}
|
|
|
|
|
|
|
|
def create_connection(db_file):
|
|
|
|
"""
|
|
|
|
Create a database connection to the SQLite database
|
2023-10-04 14:37:31 +02:00
|
|
|
specified by db_file.
|
2023-11-13 14:45:10 +01:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
conn : object
|
|
|
|
Connection object or None.
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2023-09-29 13:49:24 +02:00
|
|
|
conn = None
|
|
|
|
try:
|
2023-12-04 15:41:02 +01:00
|
|
|
conn = connect(db_file)
|
2024-01-06 23:03:08 +01:00
|
|
|
conn.execute("PRAGMA foreign_keys = ON")
|
2023-07-16 17:23:44 +02:00
|
|
|
return conn
|
2023-09-29 13:49:24 +02:00
|
|
|
except Error as e:
|
|
|
|
print(e)
|
|
|
|
return conn
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def create_tables(db_file):
|
2023-10-04 14:37:31 +02:00
|
|
|
"""
|
|
|
|
Create SQLite tables.
|
2023-11-02 06:14:01 +01:00
|
|
|
|
2023-11-13 14:45:10 +01:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
2023-10-04 14:37:31 +02:00
|
|
|
"""
|
2023-09-29 13:49:24 +02:00
|
|
|
with create_connection(db_file) as conn:
|
2024-01-06 23:03:08 +01:00
|
|
|
feeds_table_sql = (
|
|
|
|
"""
|
|
|
|
CREATE TABLE IF NOT EXISTS feeds (
|
|
|
|
id INTEGER NOT NULL,
|
|
|
|
name TEXT,
|
|
|
|
url TEXT NOT NULL UNIQUE,
|
|
|
|
PRIMARY KEY ("id")
|
|
|
|
);
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
properties_table_sql = (
|
|
|
|
"""
|
|
|
|
CREATE TABLE IF NOT EXISTS properties (
|
|
|
|
id INTEGER NOT NULL,
|
2024-01-10 21:06:56 +01:00
|
|
|
feed_id INTEGER NOT NULL UNIQUE,
|
2024-01-06 23:03:08 +01:00
|
|
|
type TEXT,
|
|
|
|
encoding TEXT,
|
|
|
|
language TEXT,
|
|
|
|
entries INTEGER,
|
|
|
|
FOREIGN KEY ("feed_id") REFERENCES "feeds" ("id")
|
|
|
|
ON UPDATE CASCADE
|
|
|
|
ON DELETE CASCADE,
|
|
|
|
PRIMARY KEY (id)
|
|
|
|
);
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
status_table_sql = (
|
|
|
|
"""
|
|
|
|
CREATE TABLE IF NOT EXISTS status (
|
|
|
|
id INTEGER NOT NULL,
|
2024-01-10 21:06:56 +01:00
|
|
|
feed_id INTEGER NOT NULL UNIQUE,
|
2024-01-06 23:03:08 +01:00
|
|
|
enabled INTEGER NOT NULL DEFAULT 1,
|
|
|
|
updated TEXT,
|
|
|
|
scanned TEXT,
|
|
|
|
renewed TEXT,
|
|
|
|
status_code INTEGER,
|
|
|
|
valid INTEGER,
|
|
|
|
filter INTEGER NOT NULL DEFAULT 1,
|
2024-01-10 21:06:56 +01:00
|
|
|
priority INTEGER,
|
2024-01-06 23:03:08 +01:00
|
|
|
FOREIGN KEY ("feed_id") REFERENCES "feeds" ("id")
|
|
|
|
ON UPDATE CASCADE
|
|
|
|
ON DELETE CASCADE,
|
|
|
|
PRIMARY KEY ("id")
|
|
|
|
);
|
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
)
|
2024-01-07 10:57:54 +01:00
|
|
|
# TODO
|
|
|
|
# Consider parameter unique:
|
|
|
|
# entry_id TEXT NOT NULL UNIQUE,
|
|
|
|
# Will eliminate function:
|
|
|
|
# check_entry_exist
|
2023-11-13 14:45:10 +01:00
|
|
|
entries_table_sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
CREATE TABLE IF NOT EXISTS entries (
|
|
|
|
id INTEGER NOT NULL,
|
|
|
|
title TEXT NOT NULL,
|
|
|
|
link TEXT NOT NULL,
|
2024-01-13 18:17:43 +01:00
|
|
|
enclosure TEXT,
|
2024-01-06 23:03:08 +01:00
|
|
|
entry_id TEXT NOT NULL,
|
|
|
|
feed_id INTEGER NOT NULL,
|
|
|
|
timestamp TEXT,
|
|
|
|
read INTEGER NOT NULL DEFAULT 0,
|
|
|
|
FOREIGN KEY ("feed_id") REFERENCES "feeds" ("id")
|
|
|
|
ON UPDATE CASCADE
|
|
|
|
ON DELETE CASCADE,
|
|
|
|
PRIMARY KEY ("id")
|
|
|
|
);
|
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
)
|
|
|
|
archive_table_sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
CREATE TABLE IF NOT EXISTS archive (
|
|
|
|
id INTEGER NOT NULL,
|
|
|
|
title TEXT NOT NULL,
|
|
|
|
link TEXT NOT NULL,
|
2024-01-13 18:17:43 +01:00
|
|
|
enclosure TEXT,
|
2024-01-06 23:03:08 +01:00
|
|
|
entry_id TEXT NOT NULL,
|
|
|
|
feed_id INTEGER NOT NULL,
|
|
|
|
timestamp TEXT,
|
|
|
|
read INTEGER NOT NULL DEFAULT 0,
|
|
|
|
FOREIGN KEY ("feed_id") REFERENCES "feeds" ("id")
|
|
|
|
ON UPDATE CASCADE
|
|
|
|
ON DELETE CASCADE,
|
|
|
|
PRIMARY KEY ("id")
|
|
|
|
);
|
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
)
|
|
|
|
# statistics_table_sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
# """
|
|
|
|
# CREATE TABLE IF NOT EXISTS statistics (
|
|
|
|
# id INTEGER NOT NULL,
|
|
|
|
# title TEXT NOT NULL,
|
|
|
|
# number INTEGER,
|
|
|
|
# PRIMARY KEY ("id")
|
|
|
|
# );
|
|
|
|
# """
|
2023-11-13 14:45:10 +01:00
|
|
|
# )
|
|
|
|
settings_table_sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
CREATE TABLE IF NOT EXISTS settings (
|
|
|
|
id INTEGER NOT NULL,
|
|
|
|
key TEXT NOT NULL,
|
|
|
|
value INTEGER,
|
|
|
|
PRIMARY KEY ("id")
|
|
|
|
);
|
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
)
|
2023-12-11 10:04:45 +01:00
|
|
|
filters_table_sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
CREATE TABLE IF NOT EXISTS filters (
|
|
|
|
id INTEGER NOT NULL,
|
|
|
|
key TEXT NOT NULL,
|
|
|
|
value TEXT,
|
|
|
|
PRIMARY KEY ("id")
|
|
|
|
);
|
|
|
|
"""
|
2023-12-11 10:04:45 +01:00
|
|
|
)
|
2023-11-02 06:14:01 +01:00
|
|
|
cur = conn.cursor()
|
|
|
|
# cur = get_cursor(db_file)
|
|
|
|
cur.execute(feeds_table_sql)
|
2024-01-06 23:03:08 +01:00
|
|
|
cur.execute(status_table_sql)
|
|
|
|
cur.execute(properties_table_sql)
|
2023-11-02 06:14:01 +01:00
|
|
|
cur.execute(entries_table_sql)
|
2023-11-13 14:45:10 +01:00
|
|
|
cur.execute(archive_table_sql)
|
2023-11-02 06:14:01 +01:00
|
|
|
# cur.execute(statistics_table_sql)
|
|
|
|
cur.execute(settings_table_sql)
|
2023-12-11 10:04:45 +01:00
|
|
|
cur.execute(filters_table_sql)
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_cursor(db_file):
|
|
|
|
"""
|
|
|
|
Allocate a cursor to connection per database.
|
2023-11-02 06:14:01 +01:00
|
|
|
|
2023-11-13 14:45:10 +01:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
CURSORS[db_file] : object
|
|
|
|
Cursor.
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2023-09-29 13:49:24 +02:00
|
|
|
if db_file in CURSORS:
|
2023-07-16 17:23:44 +02:00
|
|
|
return CURSORS[db_file]
|
2023-09-29 13:49:24 +02:00
|
|
|
else:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
CURSORS[db_file] = cur
|
|
|
|
return CURSORS[db_file]
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
|
2024-01-06 23:03:08 +01:00
|
|
|
async def import_feeds(db_file, feeds):
|
|
|
|
"""
|
|
|
|
Insert a new feed into the feeds table.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
feeds : list
|
|
|
|
Set of feeds (Title and URL).
|
|
|
|
"""
|
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
for feed in feeds:
|
|
|
|
url = feed[0]
|
|
|
|
title = feed[1]
|
|
|
|
feed = (
|
|
|
|
title, url
|
|
|
|
)
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
INSERT
|
|
|
|
INTO feeds(
|
|
|
|
name, url)
|
|
|
|
VALUES(
|
|
|
|
?, ?)
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
cur.execute(sql, feed)
|
|
|
|
except IntegrityError as e:
|
|
|
|
logging.warning("Skipping: " + url)
|
|
|
|
logging.error(e)
|
|
|
|
|
|
|
|
|
2024-01-10 21:06:56 +01:00
|
|
|
async def add_metadata(db_file):
|
|
|
|
"""
|
|
|
|
Insert a new feed into the feeds table.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
"""
|
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
SELECT id
|
|
|
|
FROM feeds
|
|
|
|
ORDER BY id ASC
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
ixs = cur.execute(sql).fetchall()
|
|
|
|
for ix in ixs:
|
|
|
|
feed_id = ix[0]
|
|
|
|
insert_feed_status(cur, feed_id)
|
|
|
|
insert_feed_properties(cur, feed_id)
|
|
|
|
|
|
|
|
|
|
|
|
def insert_feed_status(cur, feed_id):
|
|
|
|
"""
|
|
|
|
Set feed status.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
cur : object
|
|
|
|
Cursor object.
|
|
|
|
"""
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
INSERT
|
|
|
|
INTO status(
|
|
|
|
feed_id)
|
|
|
|
VALUES(
|
|
|
|
?)
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
cur.execute(sql, (feed_id,))
|
|
|
|
except IntegrityError as e:
|
|
|
|
logging.warning(
|
|
|
|
"Skipping feed_id {} for table status".format(feed_id))
|
|
|
|
logging.error(e)
|
|
|
|
|
|
|
|
|
|
|
|
def insert_feed_properties(cur, feed_id):
|
|
|
|
"""
|
|
|
|
Set feed properties.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
cur : object
|
|
|
|
Cursor object.
|
|
|
|
"""
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
INSERT
|
|
|
|
INTO properties(
|
|
|
|
feed_id)
|
|
|
|
VALUES(
|
|
|
|
?)
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
cur.execute(sql, (feed_id,))
|
|
|
|
except IntegrityError as e:
|
|
|
|
logging.warning(
|
|
|
|
"Skipping feed_id {} for table properties".format(feed_id))
|
|
|
|
logging.error(e)
|
|
|
|
|
|
|
|
|
2024-01-06 23:03:08 +01:00
|
|
|
async def insert_feed(
|
|
|
|
db_file, url, title=None, entries=None, version=None,
|
|
|
|
encoding=None, language=None, status_code=None, updated=None):
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2023-12-04 15:41:02 +01:00
|
|
|
Insert a new feed into the feeds table.
|
2023-11-02 06:14:01 +01:00
|
|
|
|
2023-11-13 14:45:10 +01:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
url : str
|
|
|
|
URL.
|
|
|
|
title : str, optional
|
2024-01-06 23:03:08 +01:00
|
|
|
Feed title. The default is None.
|
|
|
|
entries : int, optional
|
|
|
|
Number of entries. The default is None.
|
|
|
|
version : str, optional
|
|
|
|
Type of feed. The default is None.
|
|
|
|
encoding : str, optional
|
|
|
|
Encoding of feed. The default is None.
|
|
|
|
language : str, optional
|
|
|
|
Language code of feed. The default is None.
|
2023-11-13 14:45:10 +01:00
|
|
|
status : str, optional
|
|
|
|
HTTP status code. The default is None.
|
2024-01-06 23:03:08 +01:00
|
|
|
updated : ???, optional
|
|
|
|
Date feed was last updated. The default is None.
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2023-09-29 13:49:24 +02:00
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
2024-01-06 23:03:08 +01:00
|
|
|
feed = (
|
|
|
|
title, url
|
|
|
|
)
|
2023-11-13 14:45:10 +01:00
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
INSERT
|
|
|
|
INTO feeds(
|
|
|
|
name, url)
|
|
|
|
VALUES(
|
|
|
|
?, ?)
|
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
)
|
2023-09-29 13:49:24 +02:00
|
|
|
cur.execute(sql, feed)
|
2024-01-06 23:03:08 +01:00
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
SELECT id
|
|
|
|
FROM feeds
|
|
|
|
WHERE url = :url
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
feed_id = cur.execute(sql, (url,)).fetchone()[0]
|
|
|
|
status = (
|
|
|
|
feed_id, 1, updated, status_code, 1
|
|
|
|
)
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
INSERT
|
|
|
|
INTO status(
|
|
|
|
feed_id, enabled, updated, status_code, valid)
|
|
|
|
VALUES(
|
|
|
|
?, ?, ?, ?, ?)
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
cur.execute(sql, status)
|
|
|
|
properties = (
|
|
|
|
feed_id, entries, version, encoding, language
|
|
|
|
)
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
INSERT
|
|
|
|
INTO properties(
|
|
|
|
feed_id, entries, type, encoding, language)
|
|
|
|
VALUES(
|
|
|
|
?, ?, ?, ?, ?)
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
cur.execute(sql, properties)
|
|
|
|
|
|
|
|
|
2024-01-10 21:06:56 +01:00
|
|
|
async def insert_feed_(
|
|
|
|
db_file, url, title=None, entries=None, version=None,
|
|
|
|
encoding=None, language=None, status_code=None, updated=None):
|
|
|
|
"""
|
|
|
|
Insert a new feed into the feeds table.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
url : str
|
|
|
|
URL.
|
|
|
|
title : str, optional
|
|
|
|
Feed title. The default is None.
|
|
|
|
entries : int, optional
|
|
|
|
Number of entries. The default is None.
|
|
|
|
version : str, optional
|
|
|
|
Type of feed. The default is None.
|
|
|
|
encoding : str, optional
|
|
|
|
Encoding of feed. The default is None.
|
|
|
|
language : str, optional
|
|
|
|
Language code of feed. The default is None.
|
|
|
|
status : str, optional
|
|
|
|
HTTP status code. The default is None.
|
|
|
|
updated : ???, optional
|
|
|
|
Date feed was last updated. The default is None.
|
|
|
|
status : str, optional
|
|
|
|
HTTP status code. The default is None.
|
|
|
|
updated : ???, optional
|
|
|
|
Date feed was last updated. The default is None.
|
|
|
|
"""
|
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
feed = (
|
|
|
|
title, url
|
|
|
|
)
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
INSERT
|
|
|
|
INTO feeds(
|
|
|
|
name, url)
|
|
|
|
VALUES(
|
|
|
|
?, ?)
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
cur.execute(sql, feed)
|
|
|
|
feed_id = get_feed_id(cur, url)
|
|
|
|
insert_feed_properties(
|
|
|
|
cur, feed_id, entries=None,
|
|
|
|
version=None, encoding=None, language=None)
|
|
|
|
insert_feed_status(
|
|
|
|
cur, feed_id, status_code=None, updated=None)
|
|
|
|
|
|
|
|
|
2024-01-06 23:03:08 +01:00
|
|
|
async def remove_feed_by_url(db_file, url):
|
|
|
|
"""
|
|
|
|
Delete a feed by feed URL.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
url : str
|
|
|
|
URL of feed.
|
|
|
|
"""
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
async with DBLOCK:
|
|
|
|
cur = conn.cursor()
|
|
|
|
sql = (
|
|
|
|
"""
|
2024-01-13 18:17:43 +01:00
|
|
|
DELETE
|
|
|
|
FROM feeds
|
2024-01-06 23:03:08 +01:00
|
|
|
WHERE url = ?
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
cur.execute(sql, (url,))
|
2023-09-29 13:49:24 +02:00
|
|
|
|
2023-07-16 17:23:44 +02:00
|
|
|
|
2024-01-06 23:03:08 +01:00
|
|
|
async def remove_feed_by_index(db_file, ix):
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
Delete a feed by feed ID.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
ix : str
|
|
|
|
Index of feed.
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2023-09-29 13:49:24 +02:00
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
async with DBLOCK:
|
|
|
|
cur = conn.cursor()
|
2024-01-03 16:04:01 +01:00
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
SELECT url
|
|
|
|
FROM feeds
|
|
|
|
WHERE id = ?
|
|
|
|
"""
|
2024-01-03 16:04:01 +01:00
|
|
|
)
|
|
|
|
url = cur.execute(sql, (ix,)).fetchone()[0]
|
2024-01-06 23:03:08 +01:00
|
|
|
# # NOTE Should we move DBLOCK to this line? 2022-12-23
|
|
|
|
# sql = (
|
|
|
|
# "DELETE "
|
|
|
|
# "FROM entries "
|
|
|
|
# "WHERE feed_id = ?"
|
|
|
|
# )
|
|
|
|
# cur.execute(sql, (url,)) # Error? 2024-01-05
|
|
|
|
# sql = (
|
|
|
|
# "DELETE "
|
|
|
|
# "FROM archive "
|
|
|
|
# "WHERE feed_id = ?"
|
|
|
|
# )
|
|
|
|
# cur.execute(sql, (url,))
|
2024-01-03 16:04:01 +01:00
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
DELETE FROM feeds
|
|
|
|
WHERE id = ?
|
|
|
|
"""
|
2024-01-03 16:04:01 +01:00
|
|
|
)
|
|
|
|
cur.execute(sql, (ix,))
|
2024-01-06 23:03:08 +01:00
|
|
|
return url
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
|
2024-01-06 23:03:08 +01:00
|
|
|
async def get_feed_id_and_name(db_file, url):
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2024-01-04 02:16:24 +01:00
|
|
|
Get Id and Name of feed.
|
2023-10-04 14:37:31 +02:00
|
|
|
Check whether a feed exists.
|
|
|
|
Query for feeds by given url.
|
2023-11-02 06:14:01 +01:00
|
|
|
|
2023-11-13 14:45:10 +01:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
url : str
|
|
|
|
URL.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
result : list
|
|
|
|
List of ID and Name of feed.
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2024-01-13 18:17:43 +01:00
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
SELECT id, name
|
|
|
|
FROM feeds
|
|
|
|
WHERE url = ?
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = cur.execute(sql, (url,)).fetchone()
|
|
|
|
return result
|
2023-09-29 13:49:24 +02:00
|
|
|
|
|
|
|
|
2024-01-02 13:06:35 +01:00
|
|
|
async def get_number_of_items(db_file, table):
|
2023-09-29 13:49:24 +02:00
|
|
|
"""
|
2023-10-04 14:37:31 +02:00
|
|
|
Return number of entries or feeds.
|
2023-11-02 06:14:01 +01:00
|
|
|
|
2023-11-13 14:45:10 +01:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
table : str
|
|
|
|
"entries" or "feeds".
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
count : ?
|
|
|
|
Number of rows.
|
2023-09-29 13:49:24 +02:00
|
|
|
"""
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
2023-11-13 14:45:10 +01:00
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
SELECT count(id)
|
|
|
|
FROM {}
|
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
).format(table)
|
2023-11-02 06:14:01 +01:00
|
|
|
count = cur.execute(sql).fetchone()[0]
|
|
|
|
return count
|
|
|
|
|
|
|
|
|
2024-01-02 13:06:35 +01:00
|
|
|
async def get_number_of_feeds_active(db_file):
|
2023-11-02 06:14:01 +01:00
|
|
|
"""
|
|
|
|
Return number of active feeds.
|
|
|
|
|
2023-11-13 14:45:10 +01:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
count : ?
|
|
|
|
Number of rows.
|
2023-11-02 06:14:01 +01:00
|
|
|
"""
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
2023-11-13 14:45:10 +01:00
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
SELECT count(id)
|
|
|
|
FROM status
|
|
|
|
WHERE enabled = 1
|
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
)
|
2023-11-02 06:14:01 +01:00
|
|
|
count = cur.execute(sql).fetchone()[0]
|
2023-09-29 13:49:24 +02:00
|
|
|
return count
|
2023-10-04 14:37:31 +02:00
|
|
|
|
|
|
|
|
2024-01-02 13:06:35 +01:00
|
|
|
async def get_number_of_entries_unread(db_file):
|
2023-10-04 14:37:31 +02:00
|
|
|
"""
|
|
|
|
Return number of unread items.
|
2023-11-13 14:45:10 +01:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
count : ?
|
|
|
|
Number of rows.
|
2023-10-04 14:37:31 +02:00
|
|
|
"""
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
2023-11-13 14:45:10 +01:00
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
SELECT
|
|
|
|
(
|
|
|
|
SELECT count(id)
|
|
|
|
FROM entries
|
|
|
|
WHERE read = 0
|
|
|
|
) + (
|
|
|
|
SELECT count(id)
|
|
|
|
FROM archive
|
|
|
|
)
|
|
|
|
AS total_count
|
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
)
|
2023-11-02 06:14:01 +01:00
|
|
|
count = cur.execute(sql).fetchone()[0]
|
2023-10-04 14:37:31 +02:00
|
|
|
return count
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
|
2024-01-02 19:11:36 +01:00
|
|
|
async def get_unread_entries(db_file, num):
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
Extract information from unread entries.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
num : str, optional
|
|
|
|
Number. The default is None.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
2024-01-03 16:04:01 +01:00
|
|
|
results : ???
|
|
|
|
News items.
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2023-09-29 13:49:24 +02:00
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
2023-11-13 14:45:10 +01:00
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
2024-01-13 18:17:43 +01:00
|
|
|
SELECT id, title, link, enclosure, feed_id, timestamp
|
2024-01-06 23:03:08 +01:00
|
|
|
FROM entries
|
|
|
|
WHERE read = 0
|
|
|
|
UNION ALL
|
2024-01-13 18:17:43 +01:00
|
|
|
SELECT id, title, link, enclosure, feed_id, timestamp
|
2024-01-06 23:03:08 +01:00
|
|
|
FROM archive
|
|
|
|
ORDER BY timestamp
|
|
|
|
DESC LIMIT :num
|
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
)
|
2024-01-06 23:03:08 +01:00
|
|
|
results = cur.execute(sql, (num,)).fetchall()
|
2024-01-02 19:11:36 +01:00
|
|
|
return results
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
|
2024-01-10 21:06:56 +01:00
|
|
|
def get_feed_id(cur, url):
|
2024-01-07 10:57:54 +01:00
|
|
|
"""
|
|
|
|
Get index of given feed.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
cur : object
|
|
|
|
Cursor object.
|
|
|
|
url : str
|
|
|
|
URL.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
feed_id : str
|
|
|
|
Feed index.
|
|
|
|
"""
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
SELECT id
|
|
|
|
FROM feeds
|
|
|
|
WHERE url = :url
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
feed_id = cur.execute(sql, (url,)).fetchone()[0]
|
|
|
|
return feed_id
|
|
|
|
|
|
|
|
|
2024-01-04 13:38:22 +01:00
|
|
|
async def mark_entry_as_read(cur, ix):
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2023-11-29 16:32:35 +01:00
|
|
|
Set read status of entry as read.
|
2023-11-13 14:45:10 +01:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2023-11-29 16:32:35 +01:00
|
|
|
cur : object
|
|
|
|
Cursor object.
|
2023-11-13 14:45:10 +01:00
|
|
|
ix : str
|
|
|
|
Index of entry.
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
UPDATE entries
|
|
|
|
SET read = 1
|
|
|
|
WHERE id = ?
|
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
)
|
2023-09-29 13:49:24 +02:00
|
|
|
cur.execute(sql, (ix,))
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
|
2024-01-06 23:03:08 +01:00
|
|
|
async def mark_feed_as_read(db_file, url):
|
2023-11-29 16:32:35 +01:00
|
|
|
"""
|
2024-01-06 23:03:08 +01:00
|
|
|
Set read status of entries of given feed as read.
|
2023-11-29 16:32:35 +01:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
2024-01-06 23:03:08 +01:00
|
|
|
url : str
|
2023-11-29 16:32:35 +01:00
|
|
|
URL.
|
|
|
|
"""
|
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
UPDATE entries
|
|
|
|
SET read = 1
|
|
|
|
WHERE feed_id = ?
|
|
|
|
"""
|
2023-11-29 16:32:35 +01:00
|
|
|
)
|
2024-01-06 23:03:08 +01:00
|
|
|
cur.execute(sql, (url,))
|
2023-11-29 16:32:35 +01:00
|
|
|
|
|
|
|
|
2024-01-04 13:38:22 +01:00
|
|
|
async def delete_entry_by_id(db_file, ix):
|
|
|
|
"""
|
|
|
|
Delete entry by Id.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
ix : str
|
|
|
|
Index.
|
|
|
|
"""
|
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
DELETE
|
|
|
|
FROM entries
|
|
|
|
WHERE id = :ix
|
|
|
|
"""
|
2024-01-04 13:38:22 +01:00
|
|
|
)
|
|
|
|
cur.execute(sql, (ix,))
|
|
|
|
|
|
|
|
|
|
|
|
async def archive_entry(db_file, ix):
|
|
|
|
"""
|
|
|
|
Insert entry to archive and delete entry.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
ix : str
|
|
|
|
Index.
|
|
|
|
"""
|
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
INSERT
|
|
|
|
INTO archive
|
|
|
|
SELECT *
|
|
|
|
FROM entries
|
|
|
|
WHERE entries.id = :ix
|
|
|
|
"""
|
2024-01-04 13:38:22 +01:00
|
|
|
)
|
|
|
|
try:
|
|
|
|
cur.execute(sql, (ix,))
|
|
|
|
except:
|
|
|
|
print(
|
|
|
|
"ERROR DB insert from entries "
|
|
|
|
"into archive at index", ix
|
|
|
|
)
|
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
DELETE
|
|
|
|
FROM entries
|
|
|
|
WHERE id = :ix
|
|
|
|
"""
|
2024-01-04 13:38:22 +01:00
|
|
|
)
|
|
|
|
try:
|
|
|
|
cur.execute(sql, (ix,))
|
|
|
|
except:
|
|
|
|
print(
|
|
|
|
"ERROR DB deleting items from "
|
|
|
|
"table entries at index", ix
|
|
|
|
)
|
2024-01-09 16:53:19 +01:00
|
|
|
|
2024-01-04 13:38:22 +01:00
|
|
|
|
2024-01-06 23:03:08 +01:00
|
|
|
def get_feed_title(db_file, ix):
|
2024-01-02 19:11:36 +01:00
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
SELECT name
|
|
|
|
FROM feeds
|
|
|
|
WHERE id = :ix
|
|
|
|
"""
|
2024-01-02 19:11:36 +01:00
|
|
|
)
|
2024-01-06 23:03:08 +01:00
|
|
|
title = cur.execute(sql, (ix,)).fetchone()[0]
|
|
|
|
return title
|
2024-01-02 19:11:36 +01:00
|
|
|
|
|
|
|
|
2024-01-09 16:53:19 +01:00
|
|
|
def get_entry_url(db_file, ix):
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
2024-01-11 11:55:42 +01:00
|
|
|
sql = ( # TODO Handletable archive too
|
2024-01-09 16:53:19 +01:00
|
|
|
"""
|
|
|
|
SELECT link
|
|
|
|
FROM entries
|
|
|
|
WHERE id = :ix
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
url = cur.execute(sql, (ix,)).fetchone()[0]
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
2024-01-13 18:17:43 +01:00
|
|
|
def get_feed_url(db_file, feed_id):
|
2024-01-11 11:55:42 +01:00
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
SELECT url
|
|
|
|
FROM feeds
|
|
|
|
WHERE id = :feed_id
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
url = cur.execute(sql, (feed_id,)).fetchone()[0]
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
2024-01-02 19:11:36 +01:00
|
|
|
async def mark_as_read(db_file, ix):
|
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
# TODO While `async with DBLOCK` does work well from
|
|
|
|
# outside of functions, it would be better practice
|
|
|
|
# to place it within the functions.
|
|
|
|
# NOTE: We can use DBLOCK once for both
|
|
|
|
# functions, because, due to exclusive
|
|
|
|
# ID, only one can ever occur.
|
2024-01-04 13:38:22 +01:00
|
|
|
await mark_entry_as_read(cur, ix)
|
|
|
|
await delete_archived_entry(cur, ix)
|
|
|
|
|
2024-01-02 19:11:36 +01:00
|
|
|
|
2023-11-29 16:32:35 +01:00
|
|
|
async def mark_all_as_read(db_file):
|
|
|
|
"""
|
|
|
|
Set read status of all entries as read.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
"""
|
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
UPDATE entries
|
|
|
|
SET read = 1
|
|
|
|
"""
|
2023-11-29 16:32:35 +01:00
|
|
|
)
|
|
|
|
cur.execute(sql)
|
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
DELETE
|
|
|
|
FROM archive
|
|
|
|
"""
|
2023-11-29 16:32:35 +01:00
|
|
|
)
|
|
|
|
cur.execute(sql)
|
|
|
|
|
|
|
|
|
2024-01-04 13:38:22 +01:00
|
|
|
async def delete_archived_entry(cur, ix):
|
2023-11-15 15:00:49 +01:00
|
|
|
"""
|
|
|
|
Delete entry from table archive.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
ix : str
|
|
|
|
Index of entry.
|
|
|
|
"""
|
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
DELETE FROM archive
|
|
|
|
WHERE id = ?
|
|
|
|
"""
|
2023-11-15 15:00:49 +01:00
|
|
|
)
|
|
|
|
cur.execute(sql, (ix,))
|
|
|
|
|
|
|
|
|
2023-10-04 14:37:31 +02:00
|
|
|
async def statistics(db_file):
|
|
|
|
"""
|
|
|
|
Return table statistics.
|
2023-11-02 06:14:01 +01:00
|
|
|
|
2023-11-13 14:45:10 +01:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
2024-01-02 19:11:36 +01:00
|
|
|
values : list
|
|
|
|
List of values.
|
2023-10-04 14:37:31 +02:00
|
|
|
"""
|
2024-01-02 12:42:41 +01:00
|
|
|
values = []
|
2024-01-02 13:06:35 +01:00
|
|
|
values.extend([await get_number_of_entries_unread(db_file)])
|
|
|
|
entries = await get_number_of_items(db_file, 'entries')
|
|
|
|
archive = await get_number_of_items(db_file, 'archive')
|
2024-01-02 12:42:41 +01:00
|
|
|
values.extend([entries + archive])
|
2024-01-02 13:06:35 +01:00
|
|
|
values.extend([await get_number_of_feeds_active(db_file)])
|
|
|
|
values.extend([await get_number_of_items(db_file, 'feeds')])
|
2023-11-02 06:14:01 +01:00
|
|
|
# msg = """You have {} unread news items out of {} from {} news sources.
|
|
|
|
# """.format(unread_entries, entries, feeds)
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
2024-01-02 12:42:41 +01:00
|
|
|
for key in ["archive", "interval",
|
|
|
|
"quantum", "enabled"]:
|
2023-11-13 14:45:10 +01:00
|
|
|
sql = (
|
|
|
|
"SELECT value "
|
|
|
|
"FROM settings "
|
|
|
|
"WHERE key = ?"
|
|
|
|
)
|
2023-12-18 16:29:32 +01:00
|
|
|
try:
|
2024-01-02 12:42:41 +01:00
|
|
|
value = cur.execute(sql, (key,)).fetchone()[0]
|
2023-12-18 16:29:32 +01:00
|
|
|
except:
|
|
|
|
print("Error for key:", key)
|
2024-01-06 23:03:08 +01:00
|
|
|
value = "Default"
|
2024-01-02 12:42:41 +01:00
|
|
|
values.extend([value])
|
|
|
|
return values
|
2023-10-04 14:37:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def update_statistics(cur):
|
|
|
|
"""
|
|
|
|
Update table statistics.
|
2023-11-13 14:45:10 +01:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
cur : object
|
|
|
|
Cursor object.
|
2023-10-04 14:37:31 +02:00
|
|
|
"""
|
|
|
|
stat_dict = {}
|
2024-01-02 13:06:35 +01:00
|
|
|
stat_dict["feeds"] = await get_number_of_items(cur, 'feeds')
|
|
|
|
stat_dict["entries"] = await get_number_of_items(cur, 'entries')
|
|
|
|
stat_dict["unread"] = await get_number_of_entries_unread(cur=cur)
|
2023-10-04 14:37:31 +02:00
|
|
|
for i in stat_dict:
|
2023-11-13 14:45:10 +01:00
|
|
|
sql = (
|
|
|
|
"SELECT id "
|
|
|
|
"FROM statistics "
|
|
|
|
"WHERE title = ?"
|
|
|
|
)
|
2023-10-04 14:37:31 +02:00
|
|
|
cur.execute(sql, (i,))
|
|
|
|
if cur.fetchone():
|
2023-11-13 14:45:10 +01:00
|
|
|
sql = (
|
|
|
|
"UPDATE statistics "
|
|
|
|
"SET number = :num "
|
|
|
|
"WHERE title = :title"
|
|
|
|
)
|
|
|
|
cur.execute(sql, {
|
|
|
|
"title": i,
|
|
|
|
"num": stat_dict[i]
|
|
|
|
})
|
2023-10-04 14:37:31 +02:00
|
|
|
else:
|
2023-11-13 14:45:10 +01:00
|
|
|
sql = (
|
|
|
|
"SELECT count(id) "
|
|
|
|
"FROM statistics"
|
|
|
|
)
|
2023-11-02 06:14:01 +01:00
|
|
|
count = cur.execute(sql).fetchone()[0]
|
2023-10-04 14:37:31 +02:00
|
|
|
ix = count + 1
|
2023-11-13 14:45:10 +01:00
|
|
|
sql = (
|
|
|
|
"INSERT INTO statistics "
|
|
|
|
"VALUES(?,?,?)"
|
|
|
|
)
|
2023-10-04 14:37:31 +02:00
|
|
|
cur.execute(sql, (ix, i, stat_dict[i]))
|
|
|
|
|
|
|
|
|
2024-01-03 16:04:01 +01:00
|
|
|
async def set_enabled_status(db_file, ix, status):
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2024-01-03 16:04:01 +01:00
|
|
|
Set status of feed to enabled or not enabled (i.e. disabled).
|
2023-11-13 14:45:10 +01:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
ix : str
|
|
|
|
Index of entry.
|
2024-01-03 16:04:01 +01:00
|
|
|
status : int
|
|
|
|
0 or 1.
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2023-09-29 13:49:24 +02:00
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
2024-01-03 16:04:01 +01:00
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
2024-01-10 21:06:56 +01:00
|
|
|
UPDATE status
|
2024-01-06 23:03:08 +01:00
|
|
|
SET enabled = :status
|
2024-01-10 21:06:56 +01:00
|
|
|
WHERE feed_id = :id
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
2024-01-03 16:04:01 +01:00
|
|
|
)
|
|
|
|
cur.execute(sql, {
|
|
|
|
"status": status,
|
|
|
|
"id": ix
|
|
|
|
})
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
TODO
|
|
|
|
|
|
|
|
Investigate what causes date to be int 0
|
|
|
|
|
|
|
|
NOTE
|
|
|
|
|
|
|
|
When time functions of slixfeed.timedate
|
|
|
|
were async, there were errors of coroutines
|
|
|
|
|
|
|
|
"""
|
2024-01-07 10:57:54 +01:00
|
|
|
async def add_entry(
|
|
|
|
db_file, title, link, entry_id, url, date, read_status):
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2024-01-06 23:03:08 +01:00
|
|
|
Add a new entry row into the entries table.
|
2023-11-02 06:14:01 +01:00
|
|
|
|
2023-11-13 14:45:10 +01:00
|
|
|
Parameters
|
|
|
|
----------
|
2024-01-06 23:03:08 +01:00
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
2024-01-07 10:57:54 +01:00
|
|
|
title : str
|
|
|
|
Title.
|
|
|
|
link : str
|
|
|
|
Link.
|
|
|
|
entry_id : str
|
|
|
|
Entry index.
|
|
|
|
url : str
|
|
|
|
URL.
|
|
|
|
date : str
|
|
|
|
Date.
|
|
|
|
read_status : str
|
|
|
|
0 or 1.
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2024-01-06 23:03:08 +01:00
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
2024-01-10 21:06:56 +01:00
|
|
|
feed_id = get_feed_id(cur, url)
|
2024-01-06 23:03:08 +01:00
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
INSERT
|
|
|
|
INTO entries(
|
|
|
|
title, link, entry_id, feed_id, timestamp, read)
|
|
|
|
VALUES(
|
|
|
|
:title, :link, :entry_id, :feed_id, :timestamp, :read)
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
cur.execute(sql, {
|
|
|
|
"title": title,
|
|
|
|
"link": link,
|
|
|
|
"entry_id": entry_id,
|
|
|
|
"feed_id": feed_id,
|
|
|
|
"timestamp": date,
|
|
|
|
"read": read_status
|
|
|
|
})
|
|
|
|
# try:
|
|
|
|
# cur.execute(sql, entry)
|
|
|
|
# except:
|
|
|
|
# # None
|
|
|
|
# print("Unknown error for sqlite.add_entry")
|
|
|
|
# print(entry)
|
|
|
|
# #
|
|
|
|
# # print(current_time(), "COROUTINE OBJECT NOW")
|
|
|
|
# # for i in entry:
|
|
|
|
# # print(type(i))
|
|
|
|
# # print(i)
|
|
|
|
# # print(type(entry))
|
|
|
|
# # print(entry)
|
|
|
|
# # print(current_time(), "COROUTINE OBJECT NOW")
|
|
|
|
# # breakpoint()
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
|
2024-01-10 21:06:56 +01:00
|
|
|
async def add_entries_and_update_timestamp(db_file, new_entries):
|
|
|
|
"""
|
|
|
|
Add new entries.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
new_entries : list
|
|
|
|
Set of entries as dict.
|
|
|
|
"""
|
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
feeds = []
|
|
|
|
for entry in new_entries:
|
|
|
|
url = entry["url"]
|
|
|
|
feed_id = get_feed_id(cur, url)
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
INSERT
|
|
|
|
INTO entries(
|
2024-01-13 18:17:43 +01:00
|
|
|
title, link, enclosure, entry_id, feed_id, timestamp, read)
|
2024-01-10 21:06:56 +01:00
|
|
|
VALUES(
|
2024-01-13 18:17:43 +01:00
|
|
|
:title, :link, :enclosure, :entry_id, :feed_id, :timestamp, :read)
|
2024-01-10 21:06:56 +01:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
cur.execute(sql, {
|
|
|
|
"title": entry["title"],
|
|
|
|
"link": entry["link"],
|
2024-01-13 18:17:43 +01:00
|
|
|
"enclosure": entry["enclosure"],
|
2024-01-10 21:06:56 +01:00
|
|
|
"entry_id": entry["entry_id"],
|
|
|
|
"feed_id": feed_id,
|
|
|
|
"timestamp": entry["date"],
|
|
|
|
"read": entry["read_status"]
|
|
|
|
})
|
|
|
|
if url not in feeds:
|
|
|
|
feeds.extend([url])
|
|
|
|
for feed in feeds:
|
|
|
|
url = feed
|
|
|
|
feed_id = get_feed_id(cur, url)
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
UPDATE status
|
|
|
|
SET renewed = :today
|
|
|
|
WHERE feed_id = :feed_id
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
cur.execute(sql, {
|
|
|
|
"today": date.today(),
|
|
|
|
"feed_id": feed_id
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2024-01-06 23:03:08 +01:00
|
|
|
async def set_date(db_file, url):
|
2023-10-04 14:37:31 +02:00
|
|
|
"""
|
2024-01-06 23:03:08 +01:00
|
|
|
Set renewed date of given feed.
|
2023-11-13 14:45:10 +01:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
2024-01-06 23:03:08 +01:00
|
|
|
url : str
|
|
|
|
URL.
|
2023-10-04 14:37:31 +02:00
|
|
|
"""
|
2023-07-16 17:23:44 +02:00
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
2024-01-10 21:06:56 +01:00
|
|
|
feed_id = get_feed_id(cur, url)
|
2024-01-06 23:03:08 +01:00
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
UPDATE status
|
|
|
|
SET renewed = :today
|
|
|
|
WHERE feed_id = :feed_id
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
# cur = conn.cursor()
|
|
|
|
cur.execute(sql, {
|
|
|
|
"today": date.today(),
|
|
|
|
"feed_id": feed_id
|
|
|
|
})
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
|
2024-01-06 23:03:08 +01:00
|
|
|
async def update_feed_status(db_file, url, status_code):
|
2023-10-04 14:37:31 +02:00
|
|
|
"""
|
2024-01-06 23:03:08 +01:00
|
|
|
Set status_code of feed_id in table status.
|
2023-11-13 14:45:10 +01:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
2024-01-06 23:03:08 +01:00
|
|
|
url : str
|
2023-11-13 14:45:10 +01:00
|
|
|
Feed URL.
|
|
|
|
status : str
|
|
|
|
Status ID or message.
|
2023-10-04 14:37:31 +02:00
|
|
|
"""
|
2023-07-16 17:23:44 +02:00
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
2024-01-10 21:06:56 +01:00
|
|
|
feed_id = get_feed_id(cur, url)
|
2024-01-06 23:03:08 +01:00
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
UPDATE status
|
|
|
|
SET status_code = :status_code, scanned = :scanned
|
|
|
|
WHERE feed_id = :feed_id
|
|
|
|
"""
|
|
|
|
)
|
2023-11-13 14:45:10 +01:00
|
|
|
cur.execute(sql, {
|
2024-01-07 10:57:54 +01:00
|
|
|
"status_code": status_code,
|
|
|
|
"scanned": date.today(),
|
|
|
|
"feed_id": feed_id
|
2023-11-13 14:45:10 +01:00
|
|
|
})
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
|
2024-01-06 23:03:08 +01:00
|
|
|
async def update_feed_validity(db_file, url, valid):
|
2023-10-04 14:37:31 +02:00
|
|
|
"""
|
2024-01-06 23:03:08 +01:00
|
|
|
Set validity status of feed_id in table status.
|
2023-11-13 14:45:10 +01:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
2024-01-06 23:03:08 +01:00
|
|
|
url : str
|
2023-11-13 14:45:10 +01:00
|
|
|
Feed URL.
|
|
|
|
valid : boolean
|
|
|
|
0 or 1.
|
2023-10-04 14:37:31 +02:00
|
|
|
"""
|
2023-07-16 17:23:44 +02:00
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
2024-01-10 21:06:56 +01:00
|
|
|
feed_id = get_feed_id(cur, url)
|
2024-01-06 23:03:08 +01:00
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
UPDATE status
|
|
|
|
SET valid = :valid
|
|
|
|
WHERE feed_id = :feed_id
|
|
|
|
"""
|
|
|
|
)
|
2023-11-13 14:45:10 +01:00
|
|
|
cur.execute(sql, {
|
2024-01-06 23:03:08 +01:00
|
|
|
"valid": valid,
|
|
|
|
"feed_id": feed_id
|
2023-11-13 14:45:10 +01:00
|
|
|
})
|
2023-07-16 17:23:44 +02:00
|
|
|
|
2023-12-01 14:22:03 +01:00
|
|
|
|
2024-01-06 23:03:08 +01:00
|
|
|
async def update_feed_properties(db_file, url, entries, updated):
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2024-01-06 23:03:08 +01:00
|
|
|
Update properties of url in table feeds.
|
2023-11-13 14:45:10 +01:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2024-01-06 23:03:08 +01:00
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
url : str
|
|
|
|
Feed URL.
|
|
|
|
entries : int
|
|
|
|
Number of entries.
|
|
|
|
updated : ???
|
|
|
|
Date feed was last updated.
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2024-01-06 23:03:08 +01:00
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
2024-01-10 21:06:56 +01:00
|
|
|
feed_id = get_feed_id(cur, url)
|
2024-01-06 23:03:08 +01:00
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
UPDATE properties
|
|
|
|
SET entries = :entries
|
|
|
|
WHERE feed_id = :feed_id
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
cur.execute(sql, {
|
|
|
|
"entries" : entries,
|
|
|
|
"feed_id": feed_id
|
|
|
|
})
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
|
2024-01-04 13:38:22 +01:00
|
|
|
async def maintain_archive(db_file, limit):
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2023-12-08 12:32:01 +01:00
|
|
|
Maintain list of archived entries equal to specified number of items.
|
2023-11-13 14:45:10 +01:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
2024-01-06 23:03:08 +01:00
|
|
|
limit : str
|
|
|
|
Number of maximum entries to store.
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2024-01-04 13:38:22 +01:00
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
SELECT count(id)
|
|
|
|
FROM archive
|
|
|
|
"""
|
2024-01-04 13:38:22 +01:00
|
|
|
)
|
|
|
|
count = cur.execute(sql).fetchone()[0]
|
|
|
|
# FIXME Upon first time joining to a groupchat
|
|
|
|
# and then adding a URL, variable "limit"
|
|
|
|
# becomes a string in one of the iterations.
|
|
|
|
# if isinstance(limit,str):
|
|
|
|
# print("STOP")
|
|
|
|
# breakpoint()
|
|
|
|
difference = count - int(limit)
|
|
|
|
if difference > 0:
|
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
DELETE FROM archive
|
|
|
|
WHERE id
|
2024-01-13 18:17:43 +01:00
|
|
|
IN (
|
|
|
|
SELECT id
|
|
|
|
FROM archive
|
|
|
|
ORDER BY timestamp ASC
|
|
|
|
LIMIT :difference
|
|
|
|
)
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
2024-01-04 13:38:22 +01:00
|
|
|
)
|
|
|
|
cur.execute(sql, {
|
|
|
|
"difference": difference
|
|
|
|
})
|
2023-11-13 14:45:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
# TODO Move entries that don't exist into table archive.
|
|
|
|
# NOTE Entries that are read from archive are deleted.
|
|
|
|
# NOTE Unlike entries from table entries, entries from
|
|
|
|
# table archive are not marked as read.
|
2024-01-06 23:03:08 +01:00
|
|
|
async def get_entries_of_feed(db_file, feed, url):
|
2023-09-29 13:49:24 +02:00
|
|
|
"""
|
2023-10-04 14:37:31 +02:00
|
|
|
Remove entries that don't exist in a given parsed feed.
|
2023-11-15 15:00:49 +01:00
|
|
|
Check the entries returned from feed and delete read non
|
|
|
|
existing entries, otherwise move to table archive, if unread.
|
2023-11-02 06:14:01 +01:00
|
|
|
|
2023-11-13 14:45:10 +01:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
feed : list
|
|
|
|
Parsed feed document.
|
2024-01-06 23:03:08 +01:00
|
|
|
url : str
|
2023-11-13 14:45:10 +01:00
|
|
|
Feed URL. URL of associated feed.
|
2023-09-29 13:49:24 +02:00
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
SELECT id, title, link, entry_id, timestamp, read
|
|
|
|
FROM entries
|
|
|
|
WHERE feed_id = ?
|
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
)
|
2024-01-06 23:03:08 +01:00
|
|
|
items = cur.execute(sql, (url,)).fetchall()
|
2024-01-04 13:38:22 +01:00
|
|
|
return items
|
2023-11-13 14:45:10 +01:00
|
|
|
|
|
|
|
|
2024-01-02 14:19:27 +01:00
|
|
|
# TODO What is this function for? 2024-01-02
|
|
|
|
# async def get_feeds(db_file):
|
|
|
|
# """
|
|
|
|
# Query table feeds for Title, URL, Categories, Tags.
|
|
|
|
|
|
|
|
# Parameters
|
|
|
|
# ----------
|
|
|
|
# db_file : str
|
|
|
|
# Path to database file.
|
|
|
|
|
|
|
|
# Returns
|
|
|
|
# -------
|
|
|
|
# result : list
|
|
|
|
# Title, URL, Categories, Tags of feeds.
|
|
|
|
# """
|
|
|
|
# with create_connection(db_file) as conn:
|
|
|
|
# cur = conn.cursor()
|
|
|
|
# sql = (
|
|
|
|
# "SELECT name, address, type, categories, tags "
|
|
|
|
# "FROM feeds"
|
|
|
|
# )
|
|
|
|
# result = cur.execute(sql).fetchall()
|
|
|
|
# return result
|
2023-07-16 17:23:44 +02:00
|
|
|
|
2024-01-06 23:03:08 +01:00
|
|
|
# TODO select by "feed_id" (of table "status") from
|
|
|
|
# "feed" urls that are enabled in table "status"
|
2023-11-13 14:45:10 +01:00
|
|
|
async def get_feeds_url(db_file):
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
Query active feeds for URLs.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
result : list
|
|
|
|
URLs of active feeds.
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2023-09-29 13:49:24 +02:00
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
2023-11-13 14:45:10 +01:00
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
SELECT url
|
|
|
|
FROM feeds
|
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
)
|
2023-11-02 06:14:01 +01:00
|
|
|
result = cur.execute(sql).fetchall()
|
|
|
|
return result
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
|
2024-01-02 12:42:41 +01:00
|
|
|
async def get_feeds(db_file):
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2023-10-04 14:37:31 +02:00
|
|
|
Query table feeds and list items.
|
2023-11-02 06:14:01 +01:00
|
|
|
|
2023-11-13 14:45:10 +01:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
2024-01-03 16:04:01 +01:00
|
|
|
results : ???
|
2024-01-02 12:42:41 +01:00
|
|
|
URLs of feeds.
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2024-01-06 23:03:08 +01:00
|
|
|
# TODO
|
|
|
|
# 1) Select id from table feeds
|
|
|
|
# Select name, url (feeds) updated, enabled, feed_id (status)
|
|
|
|
# 2) Sort feeds by id. Sort status by feed_id
|
|
|
|
# results += cur.execute(sql).fetchall()
|
2024-01-13 18:17:43 +01:00
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
SELECT name, url, id
|
|
|
|
FROM feeds
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
results = cur.execute(sql).fetchall()
|
|
|
|
return results
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def last_entries(db_file, num):
|
|
|
|
"""
|
2023-12-18 16:29:32 +01:00
|
|
|
Query entries.
|
2023-11-13 14:45:10 +01:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
num : str
|
|
|
|
Number.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
titles_list : str
|
|
|
|
List of recent N entries as message.
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2024-01-13 18:17:43 +01:00
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
# sql = (
|
|
|
|
# "SELECT title, link "
|
|
|
|
# "FROM entries "
|
|
|
|
# "ORDER BY ROWID DESC "
|
|
|
|
# "LIMIT :num"
|
|
|
|
# )
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
SELECT title, link, timestamp
|
|
|
|
FROM entries
|
|
|
|
WHERE read = 0
|
|
|
|
UNION ALL
|
|
|
|
SELECT title, link, timestamp
|
|
|
|
FROM archive
|
|
|
|
WHERE read = 0
|
|
|
|
ORDER BY timestamp DESC
|
|
|
|
LIMIT :num
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
results = cur.execute(
|
|
|
|
sql, (num,)).fetchall()
|
|
|
|
return results
|
2023-09-29 13:49:24 +02:00
|
|
|
|
|
|
|
|
2023-11-13 14:45:10 +01:00
|
|
|
async def search_feeds(db_file, query):
|
|
|
|
"""
|
|
|
|
Query feeds.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
query : str
|
|
|
|
Search query.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
titles_list : str
|
|
|
|
Feeds of specified keywords as message.
|
|
|
|
"""
|
2024-01-13 18:17:43 +01:00
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
SELECT name, id, url
|
|
|
|
FROM feeds
|
|
|
|
WHERE name LIKE ?
|
|
|
|
OR url LIKE ?
|
|
|
|
LIMIT 50
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
results = cur.execute(
|
|
|
|
sql, [f'%{query}%', f'%{query}%']).fetchall()
|
|
|
|
return results
|
2023-07-16 17:23:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def search_entries(db_file, query):
|
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
Query entries.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
query : str
|
|
|
|
Search query.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
titles_list : str
|
|
|
|
Entries of specified keywords as message.
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2024-01-13 18:17:43 +01:00
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
SELECT title, link
|
|
|
|
FROM entries
|
|
|
|
WHERE title LIKE ?
|
|
|
|
UNION ALL
|
|
|
|
SELECT title, link
|
|
|
|
FROM archive
|
|
|
|
WHERE title LIKE ?
|
|
|
|
LIMIT 50
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
results = cur.execute(
|
|
|
|
sql, (f'%{query}%', f'%{query}%')).fetchall()
|
|
|
|
return results
|
2023-09-29 13:49:24 +02:00
|
|
|
|
2024-01-07 10:57:54 +01:00
|
|
|
|
2023-11-22 12:47:34 +01:00
|
|
|
"""
|
2023-12-01 14:22:03 +01:00
|
|
|
FIXME
|
|
|
|
|
|
|
|
Error due to missing date, but it appears that date is present:
|
2023-11-23 17:55:36 +01:00
|
|
|
ERROR DATE: source = https://blog.heckel.io/feed/
|
|
|
|
ERROR DATE: date = 2008-05-13T13:51:50+00:00
|
|
|
|
ERROR DATE: result = https://blog.heckel.io/feed/
|
2023-12-01 14:22:03 +01:00
|
|
|
|
|
|
|
19:32:05 ERROR DATE: source = https://mwl.io/feed
|
|
|
|
19:32:05 ERROR DATE: date = 2023-11-30T10:56:39+00:00
|
|
|
|
19:32:05 ERROR DATE: result = https://mwl.io/feed
|
|
|
|
19:32:05 ERROR DATE: source = https://mwl.io/feed
|
|
|
|
19:32:05 ERROR DATE: date = 2023-11-22T16:59:08+00:00
|
|
|
|
19:32:05 ERROR DATE: result = https://mwl.io/feed
|
|
|
|
19:32:06 ERROR DATE: source = https://mwl.io/feed
|
|
|
|
19:32:06 ERROR DATE: date = 2023-11-16T10:33:57+00:00
|
|
|
|
19:32:06 ERROR DATE: result = https://mwl.io/feed
|
|
|
|
19:32:06 ERROR DATE: source = https://mwl.io/feed
|
|
|
|
19:32:06 ERROR DATE: date = 2023-11-09T07:37:57+00:00
|
|
|
|
19:32:06 ERROR DATE: result = https://mwl.io/feed
|
|
|
|
|
2023-11-22 12:47:34 +01:00
|
|
|
"""
|
2024-01-06 23:03:08 +01:00
|
|
|
async def check_entry_exist(
|
|
|
|
db_file, url, entry_id=None, title=None, link=None, date=None):
|
2023-10-04 14:37:31 +02:00
|
|
|
"""
|
|
|
|
Check whether an entry exists.
|
2023-11-13 14:45:10 +01:00
|
|
|
If entry has an ID, check by ID.
|
|
|
|
If entry has timestamp, check by title, link and date.
|
|
|
|
Otherwise, check by title and link.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
source : str
|
|
|
|
Feed URL. URL of associated feed.
|
2024-01-06 23:03:08 +01:00
|
|
|
entry_id : str, optional
|
2023-11-13 14:45:10 +01:00
|
|
|
Entry ID. The default is None.
|
|
|
|
title : str, optional
|
|
|
|
Entry title. The default is None.
|
|
|
|
link : str, optional
|
|
|
|
Entry URL. The default is None.
|
|
|
|
date : str, optional
|
|
|
|
Entry Timestamp. The default is None.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
bool
|
|
|
|
True or None.
|
2023-07-16 17:23:44 +02:00
|
|
|
"""
|
2024-01-13 18:17:43 +01:00
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
exist = False
|
|
|
|
if entry_id:
|
|
|
|
feed_id = get_feed_id(cur, url)
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
SELECT id
|
|
|
|
FROM entries
|
|
|
|
WHERE entry_id = :entry_id and feed_id = :feed_id
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = cur.execute(sql, {
|
|
|
|
"entry_id": entry_id,
|
|
|
|
"feed_id": feed_id
|
|
|
|
}).fetchone()
|
|
|
|
if result: exist = True
|
|
|
|
elif date:
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
SELECT id
|
|
|
|
FROM entries
|
|
|
|
WHERE title = :title and link = :link and timestamp = :date
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
result = cur.execute(sql, {
|
|
|
|
"title": title,
|
|
|
|
"link": link,
|
|
|
|
"timestamp": date
|
|
|
|
}).fetchone()
|
|
|
|
if result: exist = True
|
|
|
|
except:
|
|
|
|
print(current_time(), "ERROR DATE: source =", url)
|
|
|
|
print(current_time(), "ERROR DATE: date =", date)
|
|
|
|
else:
|
|
|
|
sql = (
|
|
|
|
"""
|
|
|
|
SELECT id
|
|
|
|
FROM entries
|
|
|
|
WHERE title = :title and link = :link
|
|
|
|
"""
|
|
|
|
)
|
2023-11-15 15:00:49 +01:00
|
|
|
result = cur.execute(sql, {
|
|
|
|
"title": title,
|
2024-01-13 18:17:43 +01:00
|
|
|
"link": link
|
2023-11-15 15:00:49 +01:00
|
|
|
}).fetchone()
|
2024-01-07 10:57:54 +01:00
|
|
|
if result: exist = True
|
2024-01-13 18:17:43 +01:00
|
|
|
# try:
|
|
|
|
# if result:
|
|
|
|
# return True
|
|
|
|
# else:
|
|
|
|
# return None
|
|
|
|
# except:
|
|
|
|
# print(current_time(), "ERROR DATE: result =", url)
|
|
|
|
return exist
|
2023-11-02 06:14:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def set_settings_value(db_file, key_value):
|
|
|
|
"""
|
|
|
|
Set settings value.
|
|
|
|
|
2023-11-13 14:45:10 +01:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
key_value : list
|
|
|
|
key : str
|
2023-12-14 09:43:30 +01:00
|
|
|
enabled, interval, masters, quantum, random.
|
2023-11-13 14:45:10 +01:00
|
|
|
value : int
|
|
|
|
Numeric value.
|
2023-11-02 06:14:01 +01:00
|
|
|
"""
|
|
|
|
# if isinstance(key_value, list):
|
|
|
|
# key = key_value[0]
|
|
|
|
# val = key_value[1]
|
|
|
|
# elif key_value == "enable":
|
|
|
|
# key = "enabled"
|
|
|
|
# val = 1
|
|
|
|
# else:
|
|
|
|
# key = "enabled"
|
|
|
|
# val = 0
|
|
|
|
key = key_value[0]
|
2024-01-02 12:42:41 +01:00
|
|
|
value = key_value[1]
|
2023-11-02 06:14:01 +01:00
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
2024-01-04 13:38:22 +01:00
|
|
|
# try:
|
2023-11-13 14:45:10 +01:00
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
UPDATE settings
|
|
|
|
SET value = :value
|
|
|
|
WHERE key = :key
|
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
)
|
|
|
|
cur.execute(sql, {
|
2023-12-11 10:04:45 +01:00
|
|
|
"key": key,
|
2024-01-02 12:42:41 +01:00
|
|
|
"value": value
|
2023-11-13 14:45:10 +01:00
|
|
|
})
|
2024-01-04 13:38:22 +01:00
|
|
|
# except:
|
|
|
|
# logging.debug(
|
|
|
|
# "No specific value set for key {}.".format(key)
|
|
|
|
# )
|
2023-11-02 06:14:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def get_settings_value(db_file, key):
|
|
|
|
"""
|
|
|
|
Get settings value.
|
|
|
|
|
2023-11-13 14:45:10 +01:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
key : str
|
2023-12-11 10:04:45 +01:00
|
|
|
Key: archive, enabled, filter-allow, filter-deny,
|
|
|
|
interval, length, old, quantum, random.
|
2023-11-13 14:45:10 +01:00
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
val : str
|
|
|
|
Numeric value.
|
2023-11-02 06:14:01 +01:00
|
|
|
"""
|
|
|
|
with create_connection(db_file) as conn:
|
2024-01-04 13:38:22 +01:00
|
|
|
cur = conn.cursor()
|
2023-11-02 06:14:01 +01:00
|
|
|
try:
|
2023-11-13 14:45:10 +01:00
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
SELECT value
|
|
|
|
FROM settings
|
|
|
|
WHERE key = ?
|
|
|
|
"""
|
2023-11-13 14:45:10 +01:00
|
|
|
)
|
2024-01-04 13:38:22 +01:00
|
|
|
value = cur.execute(sql, (key,)).fetchone()[0]
|
|
|
|
return value
|
2023-11-02 06:14:01 +01:00
|
|
|
except:
|
2024-01-04 13:38:22 +01:00
|
|
|
logging.debug(
|
|
|
|
"No specific value set for key {}.".format(key)
|
|
|
|
)
|
2023-12-11 10:04:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def set_filters_value(db_file, key_value):
|
|
|
|
"""
|
|
|
|
Set settings value.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
key_value : list
|
|
|
|
key : str
|
|
|
|
filter-allow, filter-deny, filter-replace.
|
|
|
|
value : int
|
|
|
|
Numeric value.
|
|
|
|
"""
|
|
|
|
# if isinstance(key_value, list):
|
|
|
|
# key = key_value[0]
|
|
|
|
# val = key_value[1]
|
|
|
|
# elif key_value == "enable":
|
|
|
|
# key = "enabled"
|
|
|
|
# val = 1
|
|
|
|
# else:
|
|
|
|
# key = "enabled"
|
|
|
|
# val = 0
|
|
|
|
key = key_value[0]
|
|
|
|
val = key_value[1]
|
|
|
|
async with DBLOCK:
|
|
|
|
with create_connection(db_file) as conn:
|
|
|
|
cur = conn.cursor()
|
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
UPDATE filters
|
|
|
|
SET value = :value
|
|
|
|
WHERE key = :key
|
|
|
|
"""
|
2023-12-11 10:04:45 +01:00
|
|
|
)
|
|
|
|
cur.execute(sql, {
|
|
|
|
"key": key,
|
|
|
|
"value": val
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
async def get_filters_value(db_file, key):
|
|
|
|
"""
|
|
|
|
Get filters value.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
db_file : str
|
|
|
|
Path to database file.
|
|
|
|
key : str
|
|
|
|
Key: allow, deny.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
val : str
|
|
|
|
List of strings.
|
|
|
|
"""
|
|
|
|
with create_connection(db_file) as conn:
|
2024-01-04 13:38:22 +01:00
|
|
|
cur = conn.cursor()
|
2023-12-11 10:04:45 +01:00
|
|
|
try:
|
|
|
|
sql = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"""
|
|
|
|
SELECT value
|
|
|
|
FROM filters
|
|
|
|
WHERE key = ?
|
|
|
|
"""
|
2023-12-11 10:04:45 +01:00
|
|
|
)
|
2024-01-04 13:38:22 +01:00
|
|
|
value = cur.execute(sql, (key,)).fetchone()[0]
|
|
|
|
return value
|
2023-12-11 10:04:45 +01:00
|
|
|
except:
|
2024-01-04 13:38:22 +01:00
|
|
|
logging.debug(
|
|
|
|
"No specific value set for key {}.".format(key)
|
|
|
|
)
|