Slixfeed/slixfeed/xmpp/status.py
Schimon Jehudah, Adv. 878753b26e Increase usage of "self" handlers;
Remove CSV database.
2024-11-24 17:25:49 +02:00

92 lines
3.3 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
import os
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
logger.debug(f'{function_name}: jid: {jid_bare}')
status_text = '📜️ Slixfeed RSS News Bot'
enabled = Config.get_setting_value(self, jid_bare, 'enabled')
if enabled:
db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db')
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'
status_text = f'📬️ There are {str(unread)} news items'
else:
# print('status no news for ' + jid_bare)
status_mode = 'away'
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] = {}
logger.info('Creating new task manager for JID {jid_bare}')
logger.info('Stopping task "status" for JID {jid_bare}')
try:
self.task_manager[jid_bare]['status'].cancel()
except:
logger.info(f'No task "status" for JID {jid_bare} (XmppStatusTask.start_task)')
logger.info(f'Starting tasks "status" for JID {jid_bare}')
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:
logger.debug(f'No task "status" for JID {jid_bare}')