Skip to content

Commit

Permalink
A better API exceptions handling (#4)
Browse files Browse the repository at this point in the history
* better API exceptions handling

* fixing pylinting with a more readable code

* fixing trailing whitespace
  • Loading branch information
tassoman authored Dec 7, 2023
1 parent cd1608b commit c538d5a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
63 changes: 36 additions & 27 deletions jobs/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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<b>" + d[4] + "</b>\n" + d[5] + " <i>(" +d[1] + ")</i>\n\n" + d[3]
cw = None if sentiment >= 0 else ":nsfw: News article"
note_params = {
'sentiment': get_sentiment(d[4] + d[5]),
'text': "\n<b>" + d[4] + "</b>\n" + d[5] + " <i>(" +d[1] + ")</i>\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()
5 changes: 3 additions & 2 deletions jobs/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import time
from misskey import Misskey
from misskey.exceptions import MisskeyAPIException
from dotenv import load_dotenv

load_dotenv()
Expand All @@ -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.')

Expand Down

0 comments on commit c538d5a

Please sign in to comment.