2024-06-13 17:53:53 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import asyncio
|
2024-11-24 16:25:49 +01:00
|
|
|
import os
|
2024-06-13 17:53:53 +02:00
|
|
|
from slixfeed.config import Config
|
|
|
|
import slixfeed.sqlite as sqlite
|
|
|
|
from slixfeed.log import Logger
|
|
|
|
from slixfeed.xmpp.presence import XmppPresence
|
|
|
|
import sys
|
|
|
|
|
|
|
|
logger = Logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class XmppStatus:
|
|
|
|
|
|
|
|
|
|
|
|
def send_status_message(self, jid_bare):
|
|
|
|
"""
|
|
|
|
Send status message.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
jid : str
|
|
|
|
Jabber ID.
|
|
|
|
"""
|
|
|
|
function_name = sys._getframe().f_code.co_name
|
2024-11-24 16:25:49 +01:00
|
|
|
logger.debug(f'{function_name}: jid: {jid_bare}')
|
2024-06-13 17:53:53 +02:00
|
|
|
status_text = '📜️ Slixfeed RSS News Bot'
|
2024-07-07 12:31:12 +02:00
|
|
|
enabled = Config.get_setting_value(self, jid_bare, 'enabled')
|
2024-06-13 17:53:53 +02:00
|
|
|
if enabled:
|
2024-11-24 16:25:49 +01:00
|
|
|
db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db')
|
2024-06-13 17:53:53 +02:00
|
|
|
jid_task = self.pending_tasks[jid_bare] if jid_bare in self.pending_tasks else None
|
|
|
|
if jid_task and len(jid_task):
|
|
|
|
# print('status dnd for ' + jid_bare)
|
|
|
|
status_mode = 'dnd'
|
|
|
|
status_text = jid_task[list(jid_task.keys())[0]]
|
|
|
|
else:
|
|
|
|
# print('status enabled for ' + jid_bare)
|
|
|
|
feeds = sqlite.get_number_of_items(db_file, 'feeds_properties')
|
|
|
|
if not feeds:
|
|
|
|
# print('status no feeds for ' + jid_bare)
|
|
|
|
status_mode = 'available'
|
|
|
|
status_text = '📪️ Send a URL from a blog or a news site'
|
|
|
|
else:
|
|
|
|
unread = sqlite.get_number_of_entries_unread(db_file)
|
|
|
|
if unread:
|
|
|
|
# print('status unread for ' + jid_bare)
|
|
|
|
status_mode = 'chat'
|
2024-11-24 16:25:49 +01:00
|
|
|
status_text = f'📬️ There are {str(unread)} news items'
|
2024-06-13 17:53:53 +02:00
|
|
|
else:
|
|
|
|
# print('status no news for ' + jid_bare)
|
2024-07-05 18:04:24 +02:00
|
|
|
status_mode = 'away'
|
2024-06-13 17:53:53 +02:00
|
|
|
status_text = '📭️ No news'
|
|
|
|
else:
|
|
|
|
# print('status disabled for ' + jid_bare)
|
|
|
|
status_mode = 'xa'
|
|
|
|
status_text = '📪️ Send "Start" to receive updates'
|
|
|
|
XmppPresence.send(self, jid_bare, status_text, status_type=status_mode)
|
|
|
|
|
|
|
|
|
|
|
|
class XmppStatusTask:
|
|
|
|
|
|
|
|
|
|
|
|
async def task_status(self, jid_bare):
|
|
|
|
while True:
|
|
|
|
XmppStatus.send_status_message(self, jid_bare)
|
|
|
|
await asyncio.sleep(60 * 90)
|
|
|
|
|
|
|
|
|
|
|
|
def restart_task(self, jid_bare):
|
|
|
|
if jid_bare == self.boundjid.bare:
|
|
|
|
return
|
|
|
|
if jid_bare not in self.task_manager:
|
|
|
|
self.task_manager[jid_bare] = {}
|
2024-11-24 16:25:49 +01:00
|
|
|
logger.info('Creating new task manager for JID {jid_bare}')
|
|
|
|
logger.info('Stopping task "status" for JID {jid_bare}')
|
2024-06-13 17:53:53 +02:00
|
|
|
try:
|
|
|
|
self.task_manager[jid_bare]['status'].cancel()
|
|
|
|
except:
|
2024-11-24 16:25:49 +01:00
|
|
|
logger.info(f'No task "status" for JID {jid_bare} (XmppStatusTask.start_task)')
|
|
|
|
logger.info(f'Starting tasks "status" for JID {jid_bare}')
|
2024-06-13 17:53:53 +02:00
|
|
|
self.task_manager[jid_bare]['status'] = asyncio.create_task(
|
|
|
|
XmppStatusTask.task_status(self, jid_bare))
|
|
|
|
|
|
|
|
|
|
|
|
def stop_task(self, jid_bare):
|
|
|
|
if (jid_bare in self.task_manager and
|
|
|
|
'status' in self.task_manager[jid_bare]):
|
|
|
|
self.task_manager[jid_bare]['status'].cancel()
|
|
|
|
else:
|
2024-11-24 16:25:49 +01:00
|
|
|
logger.debug(f'No task "status" for JID {jid_bare}')
|