Skip to content

Commit

Permalink
fixing pylinting with a more readable code
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoman committed Dec 7, 2023
1 parent 7eec45d commit bfcbad2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
41 changes: 23 additions & 18 deletions jobs/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
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 @@ -29,21 +30,25 @@ 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 flagged CW"
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)
try:
api = mk.notes_create(
text=text,
visibility=visibility,
local_only=local_only,
cw=cw
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(
Expand All @@ -52,8 +57,8 @@ def publish_note():

c.execute('''
UPDATE news SET sentiment = ?, noteId = ?, notedAt = ? WHERE id = ?
''', (sentiment, n_id, n_at, d[0]))
''', (note_params['sentiment'], n_id, n_at, d[0]))
db.commit()
except MisskeyAPIException as e:
print(f"MK API error: {e}")
db.close()
db.close()
4 changes: 2 additions & 2 deletions jobs/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,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 bfcbad2

Please sign in to comment.