import sys import os import os.path import re from datetime import datetime, timedelta from setup import Setup from mastodon import Mastodon from database import Database from query import Query import pdb def cleanhtml(raw_html): cleanr = re.compile('<.*?>') cleantext = re.sub(cleanr, '', raw_html) return cleantext def unescape(s): s = s.replace("'", "'") return s def replying(): reply = False content = cleanhtml(text) content = unescape(content) try: start = content.index("@") end = content.index(" ") if len(content) > end: content = content[0: start:] + content[end +1::] neteja = content.count('@') i = 0 while i < neteja : start = content.rfind("@") end = len(content) content = content[0: start:] + content[end +1::] i += 1 question = content.lower() query_word = question query_word_length = len(query_word) if query_word[:4] == 'soft': reply = True if query_word[:6] == 'server': reply = True if query_word[:3] == 'mau': reply = True return (reply, query_word) except ValueError as v_error: print(v_error) query_word = '' return (reply, query_word) # main if __name__ == '__main__': setup = Setup() mastodon = Mastodon( access_token = setup.mastodon_app_token, api_base_url= setup.mastodon_hostname ) db = Database() query = Query() now = datetime.now() bot_id = mastodon.me().id notifications = mastodon.notifications() if len(notifications) == 0: print('No mentions') sys.exit(0) for notif in notifications: notification_id = notif.id if notif.type != 'mention': print(f'dismissing notification {notification_id}') mastodon.notifications_dismiss(notification_id) continue account_id = notif.account.id username = notif.account.acct status_id = notif.status.id text = notif.status.content visibility = notif.status.visibility reply, query_word = replying() if reply == True: if query_word[:4] == 'soft': key_word = query_word[:4] search_soft = query_word[5:] if search_soft != '': servers, users, mau = db.get_soft_data(search_soft) toot_text = f'@{username}, my data for {search_soft} software:\n\n' if servers != 0: toot_text += f'software :{search_soft}:\nservers: {servers:,}\nusers: {users:,}\nMAU: {mau:,}' else: toot_text += 'software not found!' mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) print(f'Notification {notification_id} replied') mastodon.notifications_dismiss(notification_id) if query_word[:6] == 'server': key_word = query_word[:6] search_server = query_word[7:] if search_server != '': server, software, version, users, mau, alive = db.fediquery_server_data(search_server) toot_text = f'@{username}, my data for {search_server}:\n\n' if server == '': toot_text += f"\nServer not found but it's alive. Added!\n\n" server, software, version, users, mau, alive = query.getsoft(search_server) if server != '': toot_text += f'server: {server}\nsoftware: :{software}:\nversion: {version}\nMAU: {mau:,}\nusers: {users:,}\nalive: {alive}' else: toot_text += 'server not found!' mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) print(f'Notification {notification_id} replied') mastodon.notifications_dismiss(notification_id) else: try: print(f'Dismissing notification {notification_id}') mastodon.notifications_dismiss(notification_id) except MastodonNotFoundError as notfound_error: print(f'{notfound_error}') continue