From 448d405c22fa75a9ebe22e1f07f73520d6332932 Mon Sep 17 00:00:00 2001 From: spla Date: Sat, 15 Apr 2023 17:33:13 +0200 Subject: [PATCH] Accepting queries from fediverse's users about soft or server --- README.md | 5 +- app/libraries/database.py | 4 +- fediquery.py | 210 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 216 insertions(+), 3 deletions(-) create mode 100644 fediquery.py diff --git a/README.md b/README.md index b29f911..dfed7c5 100644 --- a/README.md +++ b/README.md @@ -18,5 +18,8 @@ Within Python Virtual Environment: 3. Run `python fediverse.py` to check an get all information from all peers nodeinfo endpoints. At the end it will publish the results to the configured Mastodon account. -8. Use your favourite scheduling method to set `python fetchpeers.py` to run at least once a day and `python fediverse.py` to run at desired pace to publish the results. +4. Use your favourite scheduling method to set `python fetchpeers.py` to run at least once a day and `python fediverse.py` to run at desired pace to publish the results. Also set `python fediquery.py` to run every minute to accept queries from any fediverse' users about any `server` or `soft`. + +15.4.2023 - Added fediquery.py. It allows queries from any fediverse' user about soft and server (keywords). It replies to the asking user with its data, if any. + diff --git a/app/libraries/database.py b/app/libraries/database.py index 1a50b2a..51b63ca 100644 --- a/app/libraries/database.py +++ b/app/libraries/database.py @@ -975,7 +975,7 @@ class Database(): cur = conn.cursor() - cur.execute("select count(server), sum(users), sum(mau) from fediverse where software=(%s) and alive", (search_soft,)) + cur.execute("select count(server), sum(users), sum(mau) from mau where software=(%s) and alive", (search_soft,)) row = cur.fetchone() @@ -1019,7 +1019,7 @@ class Database(): cur = conn.cursor() - cur.execute("select server, software, version, users, mau, alive from fediverse where server=(%s)", (search_server,)) + cur.execute("select server, software, version, users, mau, alive from mau where server=(%s)", (search_server,)) row = cur.fetchone() diff --git a/fediquery.py b/fediquery.py new file mode 100644 index 0000000..2d4dfd2 --- /dev/null +++ b/fediquery.py @@ -0,0 +1,210 @@ +import sys +import os +import os.path +import re +from datetime import datetime, timedelta +from app.libraries.setup import Setup +from mastodon import Mastodon +from app.libraries.database import Database +from app.libraries.nodeinfo import Nodeinfo +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 + + 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() + + ndi = Nodeinfo() + + 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 == '' or server != '' and not alive: + + server, api = db.get_nodeinfo_endpoint(server) + + if server != '' and api != '': + + try: + + server, soft, version, users, mau, alive = ndi.getdata(server, api) + + if soft != '': + + db.write_alive_server(server, soft, version, users, mau, alive) + + else: + + db.write_not_alive_server(server) + + except: + + pass + + if server != '' and alive: + + toot_text += f"\nServer not found but it's alive. Added!\n\n" + + if alive: + + toot_text += f'server: {server}\nsoftware: :{software}:\nversion: {version}\nMAU: {int(mau):,}\nusers: {int(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