2023-11-22 12:47:34 +01:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
FIXME
|
|
|
|
|
|
2024-02-10 18:53:53 +01:00
|
|
|
|
0) URGENT!!! Place "await asyncio.sleep(next_update_time)" ***inside*** the
|
|
|
|
|
task, and not outside as it is now!
|
|
|
|
|
|
|
|
|
|
|
2023-11-22 12:47:34 +01:00
|
|
|
|
1) Function check_readiness or event "changed_status" is causing for
|
|
|
|
|
triple status messages and also false ones that indicate of lack
|
|
|
|
|
of feeds.
|
|
|
|
|
|
|
|
|
|
TODO
|
|
|
|
|
|
2024-02-10 18:53:53 +01:00
|
|
|
|
0) Move functions send_status and send_update to module action
|
|
|
|
|
|
2023-11-22 12:47:34 +01:00
|
|
|
|
1) Deprecate "add" (see above) and make it interactive.
|
|
|
|
|
Slixfeed: Do you still want to add this URL to subscription list?
|
|
|
|
|
See: case _ if message_lowercase.startswith("add"):
|
|
|
|
|
|
|
|
|
|
2) Use loop (with gather) instead of TaskGroup.
|
|
|
|
|
|
|
|
|
|
3) Assure message delivery before calling a new task.
|
|
|
|
|
See https://slixmpp.readthedocs.io/en/latest/event_index.html#term-marker_acknowledged
|
|
|
|
|
|
|
|
|
|
4) Do not send updates when busy or away.
|
|
|
|
|
See https://slixmpp.readthedocs.io/en/latest/event_index.html#term-changed_status
|
|
|
|
|
|
2023-11-26 06:48:09 +01:00
|
|
|
|
5) Animate "You have X news items"
|
|
|
|
|
📬️ when sent
|
|
|
|
|
📫️ after sent
|
|
|
|
|
|
2023-11-22 12:47:34 +01:00
|
|
|
|
NOTE
|
|
|
|
|
|
|
|
|
|
1) Self presence
|
|
|
|
|
Apparently, it is possible to view self presence.
|
|
|
|
|
This means that there is no need to store presences in order to switch or restore presence.
|
|
|
|
|
check_readiness
|
|
|
|
|
<presence from="slixfeed@canchat.org/xAPgJLHtMMHF" xml:lang="en" id="ab35c07b63a444d0a7c0a9a0b272f301" to="slixfeed@canchat.org/xAPgJLHtMMHF"><status>📂 Send a URL from a blog or a news website.</status><x xmlns="vcard-temp:x:update"><photo /></x></presence>
|
|
|
|
|
JID: self.boundjid.bare
|
2024-01-24 19:11:39 +01:00
|
|
|
|
MUC: self.alias
|
2023-11-22 12:47:34 +01:00
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
import logging
|
|
|
|
|
import os
|
2024-01-06 23:03:08 +01:00
|
|
|
|
import slixfeed.action as action
|
2024-02-04 19:56:19 +01:00
|
|
|
|
import slixfeed.config as config
|
2024-01-18 21:57:49 +01:00
|
|
|
|
# from slixfeed.dt import current_time
|
2024-02-04 19:56:19 +01:00
|
|
|
|
import slixfeed.sqlite as sqlite
|
2023-12-28 15:50:23 +01:00
|
|
|
|
# from xmpp import Slixfeed
|
2024-02-07 01:26:42 +01:00
|
|
|
|
from slixfeed.xmpp.presence import XmppPresence
|
|
|
|
|
from slixfeed.xmpp.message import XmppMessage
|
|
|
|
|
from slixfeed.xmpp.connect import XmppConnect
|
|
|
|
|
from slixfeed.xmpp.utility import get_chat_type
|
2024-01-26 12:34:07 +01:00
|
|
|
|
import time
|
2023-11-22 12:47:34 +01:00
|
|
|
|
|
2024-02-07 23:24:59 +01:00
|
|
|
|
# main_task = []
|
|
|
|
|
# jid_tasker = {}
|
|
|
|
|
# task_manager = {}
|
2023-11-22 12:47:34 +01:00
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
|
|
|
|
|
|
2024-02-04 18:08:12 +01:00
|
|
|
|
# def init_tasks(self):
|
|
|
|
|
# global task_ping
|
|
|
|
|
# # if task_ping is None or task_ping.done():
|
|
|
|
|
# # task_ping = asyncio.create_task(ping(self, jid=None))
|
|
|
|
|
# try:
|
|
|
|
|
# task_ping.cancel()
|
|
|
|
|
# except:
|
|
|
|
|
# logging.info('No ping task to cancel')
|
|
|
|
|
# task_ping = asyncio.create_task(ping(self, jid=None))
|
|
|
|
|
|
|
|
|
|
|
2024-02-10 18:53:53 +01:00
|
|
|
|
def task_ping(self):
|
|
|
|
|
# global task_ping_instance
|
2024-02-04 18:08:12 +01:00
|
|
|
|
try:
|
2024-02-10 18:53:53 +01:00
|
|
|
|
self.task_ping_instance.cancel()
|
2024-02-04 18:08:12 +01:00
|
|
|
|
except:
|
|
|
|
|
logging.info('No ping task to cancel.')
|
2024-02-10 18:53:53 +01:00
|
|
|
|
self.task_ping_instance = asyncio.create_task(XmppConnect.ping(self))
|
2024-02-04 18:08:12 +01:00
|
|
|
|
|
|
|
|
|
|
2023-11-22 12:47:34 +01:00
|
|
|
|
"""
|
|
|
|
|
FIXME
|
|
|
|
|
|
|
|
|
|
Tasks don't begin at the same time.
|
|
|
|
|
|
|
|
|
|
This is noticeable when calling "check" before "status".
|
|
|
|
|
|
|
|
|
|
await taskhandler.start_tasks(
|
|
|
|
|
self,
|
|
|
|
|
jid,
|
|
|
|
|
["check", "status"]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
"""
|
2024-02-04 18:08:12 +01:00
|
|
|
|
async def start_tasks_xmpp(self, jid, tasks=None):
|
2024-02-07 23:24:59 +01:00
|
|
|
|
"""
|
|
|
|
|
NOTE
|
|
|
|
|
|
|
|
|
|
For proper activation of tasks involving task 'interval', it is essential
|
|
|
|
|
to place task 'interval' as the last to start due to await asyncio.sleep()
|
|
|
|
|
which otherwise would postpone tasks that would be set after task 'interval'
|
|
|
|
|
"""
|
2024-02-04 18:08:12 +01:00
|
|
|
|
if jid == self.boundjid.bare:
|
|
|
|
|
return
|
|
|
|
|
try:
|
2024-02-07 23:24:59 +01:00
|
|
|
|
self.task_manager[jid]
|
2024-02-04 18:08:12 +01:00
|
|
|
|
except KeyError as e:
|
2024-02-07 23:24:59 +01:00
|
|
|
|
self.task_manager[jid] = {}
|
2024-02-04 19:19:56 +01:00
|
|
|
|
logging.debug('KeyError:', str(e))
|
|
|
|
|
logging.info('Creating new task manager for JID {}'.format(jid))
|
2024-02-04 18:08:12 +01:00
|
|
|
|
if not tasks:
|
2024-02-07 01:26:42 +01:00
|
|
|
|
tasks = ['status', 'check', 'interval']
|
2024-02-04 18:08:12 +01:00
|
|
|
|
logging.info('Stopping tasks {} for JID {}'.format(tasks, jid))
|
|
|
|
|
for task in tasks:
|
2024-02-07 23:24:59 +01:00
|
|
|
|
# if self.task_manager[jid][task]:
|
2024-02-04 18:08:12 +01:00
|
|
|
|
try:
|
2024-02-07 23:24:59 +01:00
|
|
|
|
self.task_manager[jid][task].cancel()
|
2024-02-04 18:08:12 +01:00
|
|
|
|
except:
|
2024-02-04 19:19:56 +01:00
|
|
|
|
logging.info('No task {} for JID {} (start_tasks_xmpp)'
|
2024-02-10 18:53:53 +01:00
|
|
|
|
.format(task, jid))
|
2024-02-04 18:08:12 +01:00
|
|
|
|
logging.info('Starting tasks {} for JID {}'.format(tasks, jid))
|
2023-11-22 12:47:34 +01:00
|
|
|
|
for task in tasks:
|
2023-11-26 06:48:09 +01:00
|
|
|
|
# print("task:", task)
|
|
|
|
|
# print("tasks:")
|
|
|
|
|
# print(tasks)
|
2023-11-23 17:55:36 +01:00
|
|
|
|
# breakpoint()
|
2023-11-22 12:47:34 +01:00
|
|
|
|
match task:
|
2024-02-04 18:08:12 +01:00
|
|
|
|
case 'check':
|
2024-02-07 23:24:59 +01:00
|
|
|
|
self.task_manager[jid]['check'] = asyncio.create_task(
|
2024-01-02 12:42:41 +01:00
|
|
|
|
check_updates(jid))
|
2024-02-04 19:56:19 +01:00
|
|
|
|
case 'status':
|
2024-02-07 23:24:59 +01:00
|
|
|
|
self.task_manager[jid]['status'] = asyncio.create_task(
|
2024-01-02 12:42:41 +01:00
|
|
|
|
send_status(self, jid))
|
2024-02-04 18:08:12 +01:00
|
|
|
|
case 'interval':
|
2024-02-07 23:24:59 +01:00
|
|
|
|
self.task_manager[jid]['interval'] = asyncio.create_task(
|
2024-02-10 18:53:53 +01:00
|
|
|
|
task_send(self, jid))
|
2024-02-07 23:24:59 +01:00
|
|
|
|
# for task in self.task_manager[jid].values():
|
2023-11-23 17:55:36 +01:00
|
|
|
|
# print("task_manager[jid].values()")
|
2024-02-07 23:24:59 +01:00
|
|
|
|
# print(self.task_manager[jid].values())
|
2023-11-23 17:55:36 +01:00
|
|
|
|
# print("task")
|
|
|
|
|
# print(task)
|
|
|
|
|
# print("jid")
|
|
|
|
|
# print(jid)
|
|
|
|
|
# breakpoint()
|
|
|
|
|
# await task
|
|
|
|
|
|
2024-01-02 12:42:41 +01:00
|
|
|
|
|
2024-02-10 18:53:53 +01:00
|
|
|
|
async def task_send(self, jid):
|
|
|
|
|
print("task_send for", jid)
|
|
|
|
|
try:
|
|
|
|
|
self.task_manager[jid]['interval'].cancel()
|
|
|
|
|
except:
|
|
|
|
|
logging.info('No task interval for JID {} (start_tasks_xmpp)'
|
|
|
|
|
.format(jid))
|
|
|
|
|
jid_file = jid.replace('/', '_')
|
|
|
|
|
print(jid_file)
|
|
|
|
|
db_file = config.get_pathname_to_database(jid_file)
|
|
|
|
|
print(db_file)
|
|
|
|
|
update_interval = await config.get_setting_value(db_file, 'interval')
|
|
|
|
|
print(update_interval)
|
|
|
|
|
update_interval = 60 * int(update_interval)
|
|
|
|
|
print(update_interval)
|
|
|
|
|
last_update_time = await sqlite.get_last_update_time(db_file)
|
|
|
|
|
print(last_update_time)
|
|
|
|
|
if last_update_time:
|
|
|
|
|
print('if')
|
|
|
|
|
last_update_time = float(last_update_time)
|
|
|
|
|
diff = time.time() - last_update_time
|
|
|
|
|
if diff < update_interval:
|
|
|
|
|
next_update_time = update_interval - diff
|
|
|
|
|
print('next_update_time')
|
|
|
|
|
print(next_update_time)
|
|
|
|
|
print(next_update_time/60/60)
|
|
|
|
|
await asyncio.sleep(next_update_time) # FIXME!
|
|
|
|
|
print('after await sleep')
|
|
|
|
|
|
|
|
|
|
# print("jid :", jid, "\n"
|
|
|
|
|
# "time :", time.time(), "\n"
|
|
|
|
|
# "last_update_time :", last_update_time, "\n"
|
|
|
|
|
# "difference :", diff, "\n"
|
|
|
|
|
# "update interval :", update_interval, "\n"
|
|
|
|
|
# "next_update_time :", next_update_time, "\n"
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
# elif diff > val:
|
|
|
|
|
# next_update_time = val
|
|
|
|
|
print('await (if)')
|
|
|
|
|
await sqlite.update_last_update_time(db_file)
|
|
|
|
|
else:
|
|
|
|
|
print('await (else)')
|
|
|
|
|
await sqlite.set_last_update_time(db_file)
|
|
|
|
|
print("await is done for", jid)
|
|
|
|
|
await xmpp_send_update(self, jid)
|
|
|
|
|
await start_tasks_xmpp(self, jid, ['status'])
|
|
|
|
|
await refresh_task(self, jid, task_send, 'interval')
|
2023-11-22 12:47:34 +01:00
|
|
|
|
|
|
|
|
|
|
2024-02-10 18:53:53 +01:00
|
|
|
|
async def xmpp_send_update(self, jid, num=None):
|
2023-11-22 12:47:34 +01:00
|
|
|
|
"""
|
|
|
|
|
Send news items as messages.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
jid : str
|
|
|
|
|
Jabber ID.
|
|
|
|
|
num : str, optional
|
|
|
|
|
Number. The default is None.
|
|
|
|
|
"""
|
2024-02-10 18:53:53 +01:00
|
|
|
|
print('Sending a news update to JID {}'.format(jid))
|
2024-01-27 18:15:28 +01:00
|
|
|
|
jid_file = jid.replace('/', '_')
|
2024-02-10 18:53:53 +01:00
|
|
|
|
print(jid_file)
|
2024-02-04 19:56:19 +01:00
|
|
|
|
db_file = config.get_pathname_to_database(jid_file)
|
2024-02-10 18:53:53 +01:00
|
|
|
|
print(db_file)
|
2024-02-04 19:56:19 +01:00
|
|
|
|
enabled = await config.get_setting_value(db_file, 'enabled')
|
2024-02-10 18:53:53 +01:00
|
|
|
|
print(enabled)
|
2023-12-14 09:43:30 +01:00
|
|
|
|
if enabled:
|
2024-02-10 18:53:53 +01:00
|
|
|
|
print('enabled')
|
2024-01-02 19:11:36 +01:00
|
|
|
|
if not num:
|
2024-02-04 19:56:19 +01:00
|
|
|
|
num = await config.get_setting_value(db_file, 'quantum')
|
2024-01-02 19:11:36 +01:00
|
|
|
|
else:
|
|
|
|
|
num = int(num)
|
2024-02-10 18:53:53 +01:00
|
|
|
|
print(num)
|
2024-02-04 19:56:19 +01:00
|
|
|
|
results = await sqlite.get_unread_entries(db_file, num)
|
2024-02-10 18:53:53 +01:00
|
|
|
|
print(results)
|
2024-01-13 18:17:43 +01:00
|
|
|
|
news_digest = ''
|
|
|
|
|
media = None
|
2024-02-07 01:26:42 +01:00
|
|
|
|
chat_type = await get_chat_type(self, jid)
|
2024-02-10 18:53:53 +01:00
|
|
|
|
print(jid, num, chat_type)
|
2024-01-02 19:11:36 +01:00
|
|
|
|
for result in results:
|
2024-02-10 18:53:53 +01:00
|
|
|
|
print(result)
|
2024-01-06 23:03:08 +01:00
|
|
|
|
ix = result[0]
|
|
|
|
|
title_e = result[1]
|
|
|
|
|
url = result[2]
|
2024-01-13 18:17:43 +01:00
|
|
|
|
enclosure = result[3]
|
|
|
|
|
feed_id = result[4]
|
|
|
|
|
date = result[5]
|
2024-02-04 19:56:19 +01:00
|
|
|
|
title_f = sqlite.get_feed_title(db_file, feed_id)
|
2024-02-04 18:08:12 +01:00
|
|
|
|
title_f = title_f[0]
|
2024-01-13 18:17:43 +01:00
|
|
|
|
news_digest += action.list_unread_entries(result, title_f)
|
2024-01-02 19:11:36 +01:00
|
|
|
|
# print(db_file)
|
|
|
|
|
# print(result[0])
|
|
|
|
|
# breakpoint()
|
2024-02-04 19:56:19 +01:00
|
|
|
|
await sqlite.mark_as_read(db_file, ix)
|
2024-01-13 18:17:43 +01:00
|
|
|
|
|
|
|
|
|
# Find media
|
2024-01-14 13:46:38 +01:00
|
|
|
|
# if url.startswith("magnet:"):
|
|
|
|
|
# media = action.get_magnet(url)
|
|
|
|
|
# elif enclosure.startswith("magnet:"):
|
|
|
|
|
# media = action.get_magnet(enclosure)
|
|
|
|
|
# elif enclosure:
|
2024-01-13 18:27:18 +01:00
|
|
|
|
if enclosure:
|
2024-01-13 18:17:43 +01:00
|
|
|
|
media = enclosure
|
|
|
|
|
else:
|
|
|
|
|
media = await action.extract_image_from_html(url)
|
|
|
|
|
|
|
|
|
|
if media and news_digest:
|
2024-02-07 23:24:59 +01:00
|
|
|
|
print('SENDING MESSAGE (if media and news_digest)')
|
|
|
|
|
print(news_digest)
|
2024-02-10 18:53:53 +01:00
|
|
|
|
print(media)
|
2024-01-13 18:17:43 +01:00
|
|
|
|
# Send textual message
|
2024-02-07 01:26:42 +01:00
|
|
|
|
XmppMessage.send(self, jid, news_digest, chat_type)
|
2024-01-13 18:17:43 +01:00
|
|
|
|
news_digest = ''
|
|
|
|
|
# Send media
|
2024-02-07 01:26:42 +01:00
|
|
|
|
XmppMessage.send_oob(self, jid, media, chat_type)
|
2024-01-13 18:17:43 +01:00
|
|
|
|
media = None
|
|
|
|
|
|
|
|
|
|
if news_digest:
|
2024-02-07 23:24:59 +01:00
|
|
|
|
print('SENDING MESSAGE (if news_digest)')
|
|
|
|
|
print(news_digest)
|
2024-02-10 18:53:53 +01:00
|
|
|
|
XmppMessage.send(self, jid, news_digest, chat_type)
|
2023-12-14 09:43:30 +01:00
|
|
|
|
# TODO Add while loop to assure delivery.
|
|
|
|
|
# print(await current_time(), ">>> ACT send_message",jid)
|
|
|
|
|
# NOTE Do we need "if statement"? See NOTE at is_muc.
|
2024-02-10 18:53:53 +01:00
|
|
|
|
# if chat_type in ('chat', 'groupchat'):
|
|
|
|
|
# # TODO Provide a choice (with or without images)
|
|
|
|
|
# XmppMessage.send(self, jid, news_digest, chat_type)
|
2024-02-07 01:26:42 +01:00
|
|
|
|
# See XEP-0367
|
2024-01-13 18:17:43 +01:00
|
|
|
|
# if media:
|
|
|
|
|
# # message = xmpp.Slixfeed.make_message(
|
|
|
|
|
# # self, mto=jid, mbody=new, mtype=chat_type)
|
|
|
|
|
# message = xmpp.Slixfeed.make_message(
|
|
|
|
|
# self, mto=jid, mbody=media, mtype=chat_type)
|
|
|
|
|
# message['oob']['url'] = media
|
|
|
|
|
# message.send()
|
2024-01-09 23:36:16 +01:00
|
|
|
|
|
2023-12-14 09:43:30 +01:00
|
|
|
|
# TODO Do not refresh task before
|
|
|
|
|
# verifying that it was completed.
|
2024-02-10 18:53:53 +01:00
|
|
|
|
|
|
|
|
|
# await start_tasks_xmpp(self, jid, ['status'])
|
|
|
|
|
# await refresh_task(self, jid, send_update, 'interval')
|
|
|
|
|
|
2023-12-04 15:41:02 +01:00
|
|
|
|
# interval = await initdb(
|
2023-11-22 12:47:34 +01:00
|
|
|
|
# jid,
|
2024-02-04 19:56:19 +01:00
|
|
|
|
# sqlite.get_settings_value,
|
2023-11-22 12:47:34 +01:00
|
|
|
|
# "interval"
|
|
|
|
|
# )
|
2024-02-07 23:24:59 +01:00
|
|
|
|
# self.task_manager[jid]["interval"] = loop.call_at(
|
2023-11-22 12:47:34 +01:00
|
|
|
|
# loop.time() + 60 * interval,
|
|
|
|
|
# loop.create_task,
|
|
|
|
|
# send_update(jid)
|
|
|
|
|
# )
|
|
|
|
|
|
2023-12-04 15:41:02 +01:00
|
|
|
|
# print(await current_time(), "asyncio.get_event_loop().time()")
|
|
|
|
|
# print(await current_time(), asyncio.get_event_loop().time())
|
2023-11-22 12:47:34 +01:00
|
|
|
|
# await asyncio.sleep(60 * interval)
|
|
|
|
|
|
|
|
|
|
# loop.call_later(
|
|
|
|
|
# 60 * interval,
|
|
|
|
|
# loop.create_task,
|
|
|
|
|
# send_update(jid)
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
# print
|
|
|
|
|
# await handle_event()
|
|
|
|
|
|
|
|
|
|
|
2024-02-10 18:53:53 +01:00
|
|
|
|
def clean_tasks_xmpp(self, jid, tasks=None):
|
|
|
|
|
if not tasks:
|
|
|
|
|
tasks = ['interval', 'status', 'check']
|
|
|
|
|
logging.info('Stopping tasks {} for JID {}'.format(tasks, jid))
|
|
|
|
|
for task in tasks:
|
|
|
|
|
# if self.task_manager[jid][task]:
|
|
|
|
|
try:
|
|
|
|
|
self.task_manager[jid][task].cancel()
|
|
|
|
|
except:
|
|
|
|
|
logging.debug('No task {} for JID {} (clean_tasks_xmpp)'
|
|
|
|
|
.format(task, jid))
|
|
|
|
|
|
|
|
|
|
|
2023-11-22 12:47:34 +01:00
|
|
|
|
async def send_status(self, jid):
|
|
|
|
|
"""
|
|
|
|
|
Send status message.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
jid : str
|
|
|
|
|
Jabber ID.
|
|
|
|
|
"""
|
2024-02-04 18:08:12 +01:00
|
|
|
|
logging.info('Sending a status message to JID {}'.format(jid))
|
|
|
|
|
status_text = '📜️ Slixfeed RSS News Bot'
|
2024-01-27 18:15:28 +01:00
|
|
|
|
jid_file = jid.replace('/', '_')
|
2024-02-04 19:56:19 +01:00
|
|
|
|
db_file = config.get_pathname_to_database(jid_file)
|
|
|
|
|
enabled = await config.get_setting_value(db_file, 'enabled')
|
2023-11-22 12:47:34 +01:00
|
|
|
|
if not enabled:
|
2024-02-04 18:08:12 +01:00
|
|
|
|
status_mode = 'xa'
|
2024-02-07 23:24:59 +01:00
|
|
|
|
status_text = '📪️ Send "Start" to receive updates'
|
2023-11-22 12:47:34 +01:00
|
|
|
|
else:
|
2024-02-04 19:56:19 +01:00
|
|
|
|
feeds = await sqlite.get_number_of_items(db_file, 'feeds')
|
2023-12-04 15:41:02 +01:00
|
|
|
|
# print(await current_time(), jid, "has", feeds, "feeds")
|
2023-11-22 12:47:34 +01:00
|
|
|
|
if not feeds:
|
2024-02-04 18:08:12 +01:00
|
|
|
|
status_mode = 'available'
|
|
|
|
|
status_text = '📪️ Send a URL from a blog or a news website'
|
2023-11-22 12:47:34 +01:00
|
|
|
|
else:
|
2024-02-04 19:56:19 +01:00
|
|
|
|
unread = await sqlite.get_number_of_entries_unread(db_file)
|
2023-11-22 12:47:34 +01:00
|
|
|
|
if unread:
|
2024-02-04 18:08:12 +01:00
|
|
|
|
status_mode = 'chat'
|
|
|
|
|
status_text = '📬️ There are {} news items'.format(str(unread))
|
2023-11-22 12:47:34 +01:00
|
|
|
|
# status_text = (
|
|
|
|
|
# "📰 News items: {}"
|
|
|
|
|
# ).format(str(unread))
|
|
|
|
|
# status_text = (
|
|
|
|
|
# "📰 You have {} news items"
|
|
|
|
|
# ).format(str(unread))
|
|
|
|
|
else:
|
2024-02-04 18:08:12 +01:00
|
|
|
|
status_mode = 'available'
|
|
|
|
|
status_text = '📭️ No news'
|
2023-11-22 12:47:34 +01:00
|
|
|
|
|
|
|
|
|
# breakpoint()
|
2023-12-04 15:41:02 +01:00
|
|
|
|
# print(await current_time(), status_text, "for", jid)
|
2024-02-07 01:26:42 +01:00
|
|
|
|
XmppPresence.send(self, jid, status_text, status_type=status_mode)
|
2023-11-22 12:47:34 +01:00
|
|
|
|
# await asyncio.sleep(60 * 20)
|
2024-02-04 18:08:12 +01:00
|
|
|
|
await refresh_task(self, jid, send_status, 'status', '90')
|
2023-11-22 12:47:34 +01:00
|
|
|
|
# loop.call_at(
|
|
|
|
|
# loop.time() + 60 * 20,
|
|
|
|
|
# loop.create_task,
|
|
|
|
|
# send_status(jid)
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def refresh_task(self, jid, callback, key, val=None):
|
|
|
|
|
"""
|
|
|
|
|
Apply new setting at runtime.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
jid : str
|
|
|
|
|
Jabber ID.
|
|
|
|
|
key : str
|
|
|
|
|
Key.
|
|
|
|
|
val : str, optional
|
|
|
|
|
Value. The default is None.
|
|
|
|
|
"""
|
2024-02-04 18:08:12 +01:00
|
|
|
|
logging.info('Refreshing task {} for JID {}'.format(callback, jid))
|
2023-11-22 12:47:34 +01:00
|
|
|
|
if not val:
|
2024-01-27 18:15:28 +01:00
|
|
|
|
jid_file = jid.replace('/', '_')
|
2024-02-04 19:56:19 +01:00
|
|
|
|
db_file = config.get_pathname_to_database(jid_file)
|
|
|
|
|
val = await config.get_setting_value(db_file, key)
|
2024-02-07 23:24:59 +01:00
|
|
|
|
# if self.task_manager[jid][key]:
|
|
|
|
|
if jid in self.task_manager:
|
2023-11-22 12:47:34 +01:00
|
|
|
|
try:
|
2024-02-07 23:24:59 +01:00
|
|
|
|
self.task_manager[jid][key].cancel()
|
2023-11-22 12:47:34 +01:00
|
|
|
|
except:
|
2024-02-04 18:08:12 +01:00
|
|
|
|
logging.info('No task of type {} to cancel for '
|
|
|
|
|
'JID {} (refresh_task)'.format(key, jid)
|
2024-01-04 02:16:24 +01:00
|
|
|
|
)
|
2024-02-07 23:24:59 +01:00
|
|
|
|
# self.task_manager[jid][key] = loop.call_at(
|
2023-12-28 18:58:49 +01:00
|
|
|
|
# loop.time() + 60 * float(val),
|
|
|
|
|
# loop.create_task,
|
|
|
|
|
# (callback(self, jid))
|
|
|
|
|
# # send_update(jid)
|
|
|
|
|
# )
|
2024-02-07 23:24:59 +01:00
|
|
|
|
self.task_manager[jid][key] = loop.create_task(
|
2023-12-28 18:58:49 +01:00
|
|
|
|
wait_and_run(self, callback, jid, val)
|
2023-11-22 12:47:34 +01:00
|
|
|
|
)
|
2024-02-07 23:24:59 +01:00
|
|
|
|
# self.task_manager[jid][key] = loop.call_later(
|
2023-11-22 12:47:34 +01:00
|
|
|
|
# 60 * float(val),
|
|
|
|
|
# loop.create_task,
|
|
|
|
|
# send_update(jid)
|
|
|
|
|
# )
|
2024-02-07 23:24:59 +01:00
|
|
|
|
# self.task_manager[jid][key] = send_update.loop.call_at(
|
2023-11-22 12:47:34 +01:00
|
|
|
|
# send_update.loop.time() + 60 * val,
|
|
|
|
|
# send_update.loop.create_task,
|
|
|
|
|
# send_update(jid)
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
|
2023-12-28 18:58:49 +01:00
|
|
|
|
async def wait_and_run(self, callback, jid, val):
|
|
|
|
|
await asyncio.sleep(60 * float(val))
|
|
|
|
|
await callback(self, jid)
|
|
|
|
|
|
|
|
|
|
|
2023-11-22 12:47:34 +01:00
|
|
|
|
# TODO Take this function out of
|
|
|
|
|
# <class 'slixmpp.clientxmpp.ClientXMPP'>
|
|
|
|
|
async def check_updates(jid):
|
|
|
|
|
"""
|
|
|
|
|
Start calling for update check up.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
jid : str
|
|
|
|
|
Jabber ID.
|
|
|
|
|
"""
|
2024-02-04 18:08:12 +01:00
|
|
|
|
logging.info('Scanning for updates for JID {}'.format(jid))
|
2023-11-22 12:47:34 +01:00
|
|
|
|
while True:
|
2024-01-27 18:15:28 +01:00
|
|
|
|
jid_file = jid.replace('/', '_')
|
2024-02-04 19:56:19 +01:00
|
|
|
|
db_file = config.get_pathname_to_database(jid_file)
|
2024-02-07 23:24:59 +01:00
|
|
|
|
urls = await sqlite.get_active_feeds_url(db_file)
|
2024-01-06 23:03:08 +01:00
|
|
|
|
for url in urls:
|
|
|
|
|
await action.scan(db_file, url)
|
2024-02-04 19:56:19 +01:00
|
|
|
|
val = config.get_value('settings', 'Settings', 'check')
|
2023-12-18 16:29:32 +01:00
|
|
|
|
await asyncio.sleep(60 * float(val))
|
2023-11-22 12:47:34 +01:00
|
|
|
|
# Schedule to call this function again in 90 minutes
|
|
|
|
|
# loop.call_at(
|
|
|
|
|
# loop.time() + 60 * 90,
|
|
|
|
|
# loop.create_task,
|
|
|
|
|
# self.check_updates(jid)
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
NOTE
|
|
|
|
|
This is an older system, utilizing local storage instead of XMPP presence.
|
2023-12-08 12:32:01 +01:00
|
|
|
|
This function is good for use with protocols that might not have presence.
|
2024-02-07 23:24:59 +01:00
|
|
|
|
ActivityPub, IRC, LXMF, Matrix, Nostr, SMTP, Tox.
|
2023-11-22 12:47:34 +01:00
|
|
|
|
"""
|
|
|
|
|
async def select_file(self):
|
|
|
|
|
"""
|
|
|
|
|
Initiate actions by JID (Jabber ID).
|
|
|
|
|
"""
|
|
|
|
|
while True:
|
2024-02-04 19:56:19 +01:00
|
|
|
|
db_dir = config.get_default_data_directory()
|
2023-11-22 12:47:34 +01:00
|
|
|
|
if not os.path.isdir(db_dir):
|
2024-02-04 18:08:12 +01:00
|
|
|
|
msg = ('Slixfeed does not work without a database.\n'
|
|
|
|
|
'To create a database, follow these steps:\n'
|
|
|
|
|
'Add Slixfeed contact to your roster.\n'
|
|
|
|
|
'Send a feed to the bot by URL:\n'
|
|
|
|
|
'https://reclaimthenet.org/feed/')
|
2023-12-04 15:41:02 +01:00
|
|
|
|
# print(await current_time(), msg)
|
2023-11-22 12:47:34 +01:00
|
|
|
|
print(msg)
|
|
|
|
|
else:
|
|
|
|
|
os.chdir(db_dir)
|
|
|
|
|
files = os.listdir()
|
|
|
|
|
# TODO Use loop (with gather) instead of TaskGroup
|
|
|
|
|
# for file in files:
|
|
|
|
|
# if file.endswith(".db") and not file.endswith(".db-jour.db"):
|
|
|
|
|
# jid = file[:-3]
|
|
|
|
|
# jid_tasker[jid] = asyncio.create_task(self.task_jid(jid))
|
|
|
|
|
# await jid_tasker[jid]
|
|
|
|
|
async with asyncio.TaskGroup() as tg:
|
|
|
|
|
for file in files:
|
2024-02-04 19:56:19 +01:00
|
|
|
|
if (file.endswith('.db') and
|
|
|
|
|
not file.endswith('.db-jour.db')):
|
2023-11-22 12:47:34 +01:00
|
|
|
|
jid = file[:-3]
|
2024-02-07 23:24:59 +01:00
|
|
|
|
main_task.extend([tg.create_task(self.task_jid(jid))])
|
2023-11-22 12:47:34 +01:00
|
|
|
|
# main_task = [tg.create_task(self.task_jid(jid))]
|
2024-02-07 23:24:59 +01:00
|
|
|
|
# self.task_manager.update({jid: tg})
|
2023-11-22 12:47:34 +01:00
|
|
|
|
|
|
|
|
|
|