diff --git a/jobs/create.py b/jobs/create.py index f8b96fb..bbc04cf 100644 --- a/jobs/create.py +++ b/jobs/create.py @@ -4,21 +4,23 @@ import time import os from misskey import Misskey +from misskey.exceptions import MisskeyAPIException from dotenv import load_dotenv from jobs.sentiment import get_sentiment load_dotenv() +env = { + 'local_only': os.getenv('LOCAL', 'False').lower() \ + in ('true', '1', 't', 'on', 'ok'), + 'visibility': os.getenv('VISIBILITY', 'public').lower(), + 'frequency': int(os.getenv('EVERY_MINUTES', '60')), + 'quantity': int(os.getenv('HOW_MANY', '1')) +} def publish_note(): """ Takes the latest published news and posts a note """ - local_only = os.getenv('LOCAL', 'False').lower() \ - in ('true', '1', 't', 'on', 'ok') - visibility = os.getenv('VISIBILITY', 'public').lower() - frequency = int(os.getenv('EVERY_MINUTES', '60')) - quantity = int(os.getenv('HOW_MANY', '1')) - - if quantity >= frequency//2: - quantity = frequency//2-1 + if env['quantity'] >= env['frequency']//2: + env['quantity'] = env['frequency']//2-1 db = sqlite3.connect('feed-bot.sqlite') c = db.cursor() @@ -28,28 +30,35 @@ def publish_note(): SELECT * FROM news WHERE notedAt IS NULL OR notedAt = '' ORDER BY publishedAt DESC LIMIT ? - ''', str(quantity)) + ''', str(env['quantity'])) data = c.fetchall() if data is not None: for d in data: - sentiment = get_sentiment(d[4] + d[5]) - text = "\n" + d[4] + "\n" + d[5] + " (" +d[1] + ")\n\n" + d[3] - cw = None if sentiment >= 0 else ":nsfw: News article" + note_params = { + 'sentiment': get_sentiment(d[4] + d[5]), + 'text': "\n" + d[4] + "\n" + d[5] + " (" +d[1] + ")\n\n" + d[3], + 'cw': None + } + if note_params['sentiment'] < 0: + note_params['cw'] = ":nsfw: News article flagged CW" time.sleep(2) - api = mk.notes_create( - text=text, - visibility=visibility, - local_only=local_only, - cw=cw - ) - n_id = api['createdNote']['id'] - n_at = int(datetime.strptime( - api['createdNote']['createdAt'], '%Y-%m-%dT%H:%M:%S.%fZ' - ).timestamp()) + try: + api = mk.notes_create( + text=note_params['text'], + visibility=env['visibility'], + local_only=env['local_only'], + cw=note_params['cw'] + ) + n_id = api['createdNote']['id'] + n_at = int(datetime.strptime( + api['createdNote']['createdAt'], '%Y-%m-%dT%H:%M:%S.%fZ' + ).timestamp()) - c.execute(''' - UPDATE news SET sentiment = ?, noteId = ?, notedAt = ? WHERE id = ? - ''', (sentiment, n_id, n_at, d[0])) - db.commit() - db.close() + c.execute(''' + UPDATE news SET sentiment = ?, noteId = ?, notedAt = ? WHERE id = ? + ''', (note_params['sentiment'], n_id, n_at, d[0])) + db.commit() + except MisskeyAPIException as e: + print(f"MK API error: {e}") + db.close() diff --git a/jobs/delete.py b/jobs/delete.py index 34b8634..07f817f 100644 --- a/jobs/delete.py +++ b/jobs/delete.py @@ -3,6 +3,7 @@ import os import time from misskey import Misskey +from misskey.exceptions import MisskeyAPIException from dotenv import load_dotenv load_dotenv() @@ -27,10 +28,10 @@ def purge(): time.sleep(2) try: denoted = mk.notes_delete(note_id=n[0]) + if denoted is not None: + c.execute('DELETE FROM news WHERE noteId = ?', (n[0],)) except MisskeyAPIException: pass - if denoted is not None: - c.execute('DELETE FROM news WHERE noteId = ?', (n[0],)) else: print('No notes to delete.')