Accepting queries from fediverse's users about soft or server
This commit is contained in:
pare
19fb2a8185
commit
448d405c22
S'han modificat 3 arxius amb 216 adicions i 3 eliminacions
|
@ -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.
|
||||
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
210
fediquery.py
Normal file
210
fediquery.py
Normal file
|
@ -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
|
Loading…
Referencia en una nova incidència