Added fediquery.py, allow query data about 'soft' and 'server'

This commit is contained in:
spla 2023-01-05 11:45:11 +01:00
pare dc5819821b
commit c7363875e9
S'han modificat 2 arxius amb 189 adicions i 1 eliminacions

Veure arxiu

@ -19,7 +19,7 @@ Within Python Virtual Environment:
3. Run `python fediverse.py` to query world alive servers API. It gets data from server's nodeinfo.
4. Use your favourite scheduling method to set `python fediverse.py` to run twice daily, `python fetchservers.py` one time daily.
4. Use your favourite scheduling method to set `python fediverse.py` to run twice daily, `python fetchservers.py` one time daily and `fediquery.py` to run every minute.
18.2.2021 - New feature! Added [Lemmy project](https://join.lemmy.ml)
12.5.2021 - New feature! Added Wordpress support. The code can now detect Wordpress instances with ActivityPub enabled plugin.
@ -30,3 +30,4 @@ Within Python Virtual Environment:
2.3.2022 - Improved server nodeinfo detection.
4.1.2023 - Refactored.
4.1.2023 - Now peers are obtained from mastodon.social's peers list.
5.1.2o23 - Added fediquery.py. Allow ask the main bot fediverse about `soft` and `server` and it replies to the asking user with its data if any.

187
fediquery.py Normal file
Veure arxiu

@ -0,0 +1,187 @@
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
import pdb
def cleanhtml(raw_html):
cleanr = re.compile('<.*?>')
cleantext = re.sub(cleanr, '', raw_html)
return cleantext
def unescape(s):
s = s.replace("&apos;", "'")
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()
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':
i += 1
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'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