Skip to content

Commit

Permalink
better API exceptions handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoman committed Dec 7, 2023
1 parent 4ec897d commit 7eec45d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
34 changes: 19 additions & 15 deletions jobs/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
import os
from misskey import Misskey
from misskey.exceptions import MisskeyAPIException
from dotenv import load_dotenv
from jobs.sentiment import get_sentiment

Expand Down Expand Up @@ -35,21 +36,24 @@ def publish_note():
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"
cw = None if sentiment >= 0 else ":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=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())

c.execute('''
UPDATE news SET sentiment = ?, noteId = ?, notedAt = ? WHERE id = ?
''', (sentiment, n_id, n_at, d[0]))
db.commit()
c.execute('''
UPDATE news SET sentiment = ?, noteId = ?, notedAt = ? WHERE id = ?
''', (sentiment, n_id, n_at, d[0]))
db.commit()
except MisskeyAPIException as e:
print(f"MK API error: {e}")
db.close()
3 changes: 2 additions & 1 deletion 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,7 +28,7 @@ def purge():
time.sleep(2)
try:
denoted = mk.notes_delete(note_id=n[0])
except Misskey.exceptions.MisskeyAPIException:
except MisskeyAPIException:
pass
if denoted is not None:
c.execute('DELETE FROM news WHERE noteId = ?', (n[0],))
Expand Down

0 comments on commit 7eec45d

Please sign in to comment.