Add option to set message formatting.

Restore summary.
This commit is contained in:
Schimon Jehudah 2024-02-26 01:17:50 +00:00
parent bfde775369
commit b5e5a179f4
7 changed files with 62 additions and 48 deletions

View file

@ -186,12 +186,13 @@ async def xmpp_send_update(self, jid, num=None):
ix = result[0] ix = result[0]
title_e = result[1] title_e = result[1]
url = result[2] url = result[2]
enclosure = result[3] summary = result[3]
feed_id = result[4] enclosure = result[4]
date = result[5] feed_id = result[5]
date = result[6]
title_f = sqlite.get_feed_title(db_file, feed_id) title_f = sqlite.get_feed_title(db_file, feed_id)
title_f = title_f[0] title_f = title_f[0]
news_digest += list_unread_entries(result, title_f) news_digest += list_unread_entries(result, title_f, jid_file)
# print(db_file) # print(db_file)
# print(result[0]) # print(result[0])
# breakpoint() # breakpoint()
@ -408,7 +409,7 @@ def is_feed(feed):
return value return value
def list_unread_entries(result, feed_title): def list_unread_entries(result, feed_title, jid_file):
# TODO Add filtering # TODO Add filtering
# TODO Do this when entry is added to list and mark it as read # TODO Do this when entry is added to list and mark it as read
# DONE! # DONE!
@ -428,29 +429,41 @@ def list_unread_entries(result, feed_title):
# NOTE Why doesn't this work without list? # NOTE Why doesn't this work without list?
# i.e. for result in results # i.e. for result in results
# for result in results.fetchall(): # for result in results.fetchall():
ix = result[0] ix = str(result[0])
title = result[1] title = str(result[1])
# # TODO Retrieve summary from feed # # TODO Retrieve summary from feed
# # See fetch.view_entry # # See fetch.view_entry
# summary = result[2] summary = result[3]
# # Remove HTML tags # Remove HTML tags
# try: try:
# summary = BeautifulSoup(summary, "lxml").text summary = BeautifulSoup(summary, "lxml").text
# except: except:
# print(result[2]) print(result[3])
# breakpoint() breakpoint()
# # TODO Limit text length # TODO Limit text length
# summary = summary.replace("\n\n\n", "\n\n") # summary = summary.replace("\n\n\n", "\n\n")
# length = sqlite.get_setting_value(db_file, "length") summary = summary.replace('\n', ' ')
# summary = summary[:length] + " […]" summary = summary.replace(' ', ' ')
summary = summary.replace(' ', ' ')
db_file = config.get_pathname_to_database(jid_file)
length = config.get_setting_value(db_file, "length")
length = int(length)
summary = summary[:length] + " […]"
# summary = summary.strip().split('\n') # summary = summary.strip().split('\n')
# summary = ["> " + line for line in summary] # summary = ["> " + line for line in summary]
# summary = "\n".join(summary) # summary = "\n".join(summary)
link = result[2] link = result[2]
link = remove_tracking_parameters(link) link = remove_tracking_parameters(link)
link = (replace_hostname(link, "link")) or link link = (replace_hostname(link, "link")) or link
news_item = ("\n{}\n{}\n{} [{}]\n").format(str(title), str(link), # news_item = ("\n{}\n{}\n{} [{}]\n").format(str(title), str(link),
str(feed_title), str(ix)) # str(feed_title), str(ix))
formatting = config.get_setting_value(db_file, 'formatting')
news_item = formatting.format(feed_title=feed_title,
title=title,
summary=summary,
link=link,
ix=ix)
news_item = news_item.replace('\\n', '\n')
return news_item return news_item
@ -1207,6 +1220,7 @@ async def scan(db_file, url):
entry = { entry = {
"title": title, "title": title,
"link": link, "link": link,
"summary": summary,
"enclosure": media_link, "enclosure": media_link,
"entry_id": entry_id, "entry_id": entry_id,
"date": date, "date": date,

View file

@ -36,6 +36,9 @@ quantum = 3
# Pick random item from database # Pick random item from database
random = 0 random = 0
# Set message formatting
formatting = {title}\n> {summary}\n{link}\n{feed_title} [{ix}]\n\n
# Utilized in case of missing protocol support. # Utilized in case of missing protocol support.
[Bridge] [Bridge]
gopher = gopher =

View file

@ -52,15 +52,15 @@ def get_setting_value(db_file, key):
value = value[0] value = value[0]
else: else:
value = get_value("settings", "Settings", key) value = get_value("settings", "Settings", key)
try: # try:
value = int(value) # value = int(value)
except ValueError as e: # except ValueError as e:
print('ValueError for value {} (key {}):\n{}'.format(value, key, e)) # print('ValueError for value {} (key {}):\n{}'.format(value, key, e))
if isinstance(value, bool): # if isinstance(value, bool):
if value: # if value:
value = 1 # value = 1
else: # else:
value = 0 # value = 0
return value return value

View file

@ -77,11 +77,13 @@ def create_tables(db_file):
id INTEGER NOT NULL, id INTEGER NOT NULL,
title TEXT NOT NULL, title TEXT NOT NULL,
link TEXT NOT NULL, link TEXT NOT NULL,
summary TEXT,
enclosure TEXT, enclosure TEXT,
entry_id TEXT NOT NULL, entry_id TEXT NOT NULL,
feed_id INTEGER NOT NULL, feed_id INTEGER NOT NULL,
timestamp TEXT, timestamp TEXT,
read INTEGER NOT NULL DEFAULT 0, read INTEGER NOT NULL DEFAULT 0,
reject INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY ("feed_id") REFERENCES "feeds" ("id") FOREIGN KEY ("feed_id") REFERENCES "feeds" ("id")
ON UPDATE CASCADE ON UPDATE CASCADE
ON DELETE CASCADE, ON DELETE CASCADE,
@ -89,26 +91,19 @@ def create_tables(db_file):
); );
""" """
) )
categories_table_sql = (
"""
CREATE TABLE IF NOT EXISTS categories (
id INTEGER NOT NULL,
name TEXT,
PRIMARY KEY ("id")
);
"""
)
entries_table_sql = ( entries_table_sql = (
""" """
CREATE TABLE IF NOT EXISTS entries ( CREATE TABLE IF NOT EXISTS entries (
id INTEGER NOT NULL, id INTEGER NOT NULL,
title TEXT NOT NULL, title TEXT NOT NULL,
link TEXT NOT NULL, link TEXT NOT NULL,
summary TEXT,
enclosure TEXT, enclosure TEXT,
entry_id TEXT NOT NULL, entry_id TEXT NOT NULL,
feed_id INTEGER NOT NULL, feed_id INTEGER NOT NULL,
timestamp TEXT, timestamp TEXT,
read INTEGER NOT NULL DEFAULT 0, read INTEGER NOT NULL DEFAULT 0,
reject INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY ("feed_id") REFERENCES "feeds" ("id") FOREIGN KEY ("feed_id") REFERENCES "feeds" ("id")
ON UPDATE CASCADE ON UPDATE CASCADE
ON DELETE CASCADE, ON DELETE CASCADE,
@ -1000,11 +995,11 @@ async def get_unread_entries(db_file, num):
cur = conn.cursor() cur = conn.cursor()
sql = ( sql = (
""" """
SELECT id, title, link, enclosure, feed_id, timestamp SELECT id, title, link, summary, enclosure, feed_id, timestamp
FROM entries FROM entries
WHERE read = 0 WHERE read = 0
UNION ALL UNION ALL
SELECT id, title, link, enclosure, feed_id, timestamp SELECT id, title, link, summary, enclosure, feed_id, timestamp
FROM archive FROM archive
ORDER BY timestamp DESC ORDER BY timestamp DESC
LIMIT :num LIMIT :num
@ -1541,14 +1536,15 @@ async def add_entries_and_update_timestamp(db_file, feed_id, new_entries):
""" """
INSERT INSERT
INTO entries( INTO entries(
title, link, enclosure, entry_id, feed_id, timestamp, read) title, link, summary, enclosure, entry_id, feed_id, timestamp, read)
VALUES( VALUES(
:title, :link, :enclosure, :entry_id, :feed_id, :timestamp, :read) :title, :link, :summary, :enclosure, :entry_id, :feed_id, :timestamp, :read)
""" """
) )
par = { par = {
"title": entry["title"], "title": entry["title"],
"link": entry["link"], "link": entry["link"],
"summary": entry["summary"],
"enclosure": entry["enclosure"], "enclosure": entry["enclosure"],
"entry_id": entry["entry_id"], "entry_id": entry["entry_id"],
"feed_id": feed_id, "feed_id": feed_id,

View file

@ -1,2 +1,2 @@
__version__ = '0.1.22' __version__ = '0.1.23'
__version_info__ = (0, 1, 22) __version_info__ = (0, 1, 23)

View file

@ -1373,7 +1373,7 @@ class Slixfeed(slixmpp.ClientXMPP):
else: else:
form['instructions'] = 'Adding subscription' form['instructions'] = 'Adding subscription'
title = '' title = ''
tags = '' tags = '' # TODO Suggest tags by element "categories"
form.add_field(ftype='fixed', form.add_field(ftype='fixed',
value='Properties') value='Properties')
form.add_field(var='name', form.add_field(var='name',
@ -1430,6 +1430,7 @@ class Slixfeed(slixmpp.ClientXMPP):
return session return session
# TODO Create a new form. Do not "recycle" the last form.
async def _handle_subscription_complete(self, payload, session): async def _handle_subscription_complete(self, payload, session):
jid = session['from'].bare jid = session['from'].bare
values = payload['values'] values = payload['values']

View file

@ -28,7 +28,7 @@ import logging
import os import os
import slixfeed.action as action import slixfeed.action as action
import slixfeed.config as config import slixfeed.config as config
from slixfeed.dt import current_time, timestamp import slixfeed.dt as dt
import slixfeed.fetch as fetch import slixfeed.fetch as fetch
import slixfeed.sqlite as sqlite import slixfeed.sqlite as sqlite
import slixfeed.task as task import slixfeed.task as task
@ -557,7 +557,7 @@ async def message(self, message):
if not result['error']: if not result['error']:
data = result['content'] data = result['content']
code = result['status_code'] code = result['status_code']
title = get_document_title(data) title = action.get_document_title(data)
title = title.strip().lower() title = title.strip().lower()
for i in (' ', '-'): for i in (' ', '-'):
title = title.replace(i, '_') title = title.replace(i, '_')
@ -1137,10 +1137,10 @@ async def message(self, message):
if not os.path.isdir(data_dir + '/logs/'): if not os.path.isdir(data_dir + '/logs/'):
os.mkdir(data_dir + '/logs/') os.mkdir(data_dir + '/logs/')
action.log_to_markdown( action.log_to_markdown(
current_time(), os.path.join(data_dir, 'logs', jid_file), dt.current_time(), os.path.join(data_dir, 'logs', jid_file),
jid, message_text) jid, message_text)
action.log_to_markdown( action.log_to_markdown(
current_time(), os.path.join(data_dir, 'logs', jid_file), dt.current_time(), os.path.join(data_dir, 'logs', jid_file),
self.boundjid.bare, response) self.boundjid.bare, response)
print( print(