Improved servers nodeinfo detection

This commit is contained in:
spla 2022-03-02 22:26:05 +01:00
pare 36d7ff62b1
commit 0b52e0090d
S'han modificat 2 arxius amb 417 adicions i 483 eliminacions

Veure arxiu

@ -1,3 +1,5 @@
from multiprocessing import set_start_method
from multiprocessing import get_context
import time import time
import urllib3 import urllib3
from datetime import datetime from datetime import datetime
@ -7,11 +9,10 @@ import json
import sys import sys
import os.path import os.path
import requests import requests
import psycopg2
from itertools import product
import multiprocessing
import aiohttp import aiohttp
import asyncio import asyncio
import psycopg2
from itertools import product
import socket import socket
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import pdb import pdb
@ -19,18 +20,6 @@ plt.style.use('seaborn')
start_time = time.time() start_time = time.time()
apis = ['/nodeinfo/2.0?',
'/nodeinfo/2.0.json?',
'/main/nodeinfo/2.0?',
'/api/statusnet/config?',
'/api/nodeinfo/2.0.json?',
'/api/nodeinfo?',
'/api/v1/instance?',
'/wp-json/nodeinfo/2.0?',
'/api/v1/instance/nodeinfo/2.0?',
'/.well-known/x-nodeinfo2?'
]
client_exceptions = ( client_exceptions = (
aiohttp.ClientResponseError, aiohttp.ClientResponseError,
aiohttp.ClientConnectionError, aiohttp.ClientConnectionError,
@ -40,20 +29,28 @@ client_exceptions = (
socket.gaierror, socket.gaierror,
) )
def is_json(myjson): class Server:
try: name = 'Server'
json_object = json.loads(myjson)
except ValueError as e:
print(e)
return False
return True
def get_alive_servers(server): def __init_(self, server, software, users, alive, api, soft_version):
serv_api = '' self.server = server
serv_soft = '' self.software = software
soft_version = '' self.users = users
self.alive = alive
self.api = api
self.version = self.soft_version
def get_alive_servers(self, *args):
fediverse_db, fediverse_db_user = db_config()
now = datetime.now()
if len(args) != 0:
self.server = args[0]
try: try:
@ -63,16 +60,16 @@ def get_alive_servers(server):
cur = conn.cursor() cur = conn.cursor()
cur.execute("select alive, software, users_api, version, first_checked_at, downs from fediverse where server=(%s)", (server,)) cur.execute("select alive, software, users_api, version, first_checked_at, downs from fediverse where server=(%s)", (self.server,))
row = cur.fetchone() row = cur.fetchone()
if row is not None: if row is not None:
was_alive = row[0] was_alive = row[0]
serv_soft = row[1] self.software = row[1]
serv_api = row[2] self.api = row[2]
soft_version = row[3] self.soft_version = row[3]
first_checked_at = row[4] first_checked_at = row[4]
downs_qty = row[5] downs_qty = row[5]
@ -85,6 +82,7 @@ def get_alive_servers(server):
finally: finally:
if conn is not None: if conn is not None:
conn.close() conn.close()
alive = False alive = False
@ -93,68 +91,29 @@ def get_alive_servers(server):
user_agent = {'User-agent': 'Mozilla/5.0'} user_agent = {'User-agent': 'Mozilla/5.0'}
data = requests.get('https://' + server + serv_api, headers = user_agent, timeout=3) data = requests.get('https://' + self.server + self.api, headers = user_agent, timeout=3)
if serv_soft == "mastodon":
if serv_api == '/nodeinfo/2.0?':
try: try:
users = data.json()['usage']['users']['total']
soft_version = data.json()['software']['version'] self.users = data.json()['usage']['users']['total']
alive = True
except: if self.users == 0:
users = 0
soft_version = "" self.users = data.json()['usage']['users']['activeHalfyear']
if serv_api == '/nodeinfo/2.0.json?':
try: if self.software == 'socialhome':
users = data.json()['usage']['users']['total']
soft_version = data.json()['software']['version'] self.soft_version = data.json()['server']['version']
alive = True
except: else:
users = 0
soft_version = "" self.soft_version = data.json()['software']['version']
elif serv_api == '/api/v1/instance?':
try: if self.software == "wordpress" and "activitypub" in data.json()['protocols']:
users = data.json()['stats']['user_count']
soft_version = data.json()['version']
alive = True
except:
users = 0
soft_version = ""
if serv_soft == "pleroma" or serv_soft == "diaspora" or serv_soft == "peertube" or serv_soft == "pixelfed" or serv_soft == "hubzilla" or serv_soft == "writefreely" or serv_soft == "friendica":
try:
users = data.json()['usage']['users']['total']
soft_version = data.json()['software']['version']
alive = True
except:
users = 0
soft_version = ""
if serv_soft == "gnusocialv2" or serv_soft == "gnusocial":
try:
users = data.json()['usage']['users']['total']
if users == 0:
users = data.json()['usage']['users']['activeHalfyear']
soft_version = data.json()['software']['version']
alive = True
except:
users = 0
soft_version = ""
if serv_soft == "plume" or serv_soft == 'red' or serv_soft == "misskey" or serv_soft == "zap" or serv_soft == "prismo" or serv_soft == "ravenvale" or serv_soft == "osada" or serv_soft == "groundpolis":
try:
users = data.json()['usage']['users']['total']
soft_version = data.json()['software']['version']
alive = True
except:
users = 0
soft_version = ""
if serv_soft == "ganggo" or serv_soft == "squs" or serv_soft == "dolphin" or serv_soft == "lemmy" or serv_soft == "wordpress":
try:
users = data.json()['usage']['users']['total']
soft_version = data.json()['software']['version']
if serv_soft == "wordpress" and "activitypub" in data.json()['protocols']:
alive = True alive = True
elif serv_soft == "wordpress" and "activitypub" not in data.json()['protocols']: elif self.software == "wordpress" and "activitypub" not in data.json()['protocols']:
alive = False alive = False
@ -164,28 +123,21 @@ def get_alive_servers(server):
except: except:
users = 0 self.users = 0
soft_version = ""
if serv_soft == 'funkwhale': self.soft_version = ""
else:
if self.api == '/api/v1/instance?':
try: try:
users = data.json()['usage']['users']['total'] self.users = data.json()['stats']['user_count']
soft_version = data.json()['software']['version'] self.soft_version = data.json()['version']
alive = True alive = True
except: except:
users = 0 self.users = 0
soft_version = "" self.soft_version = ""
if serv_soft == 'socialhome':
try:
users = data.json()['usage']['users']['total']
soft_version = data.json()['server']['version']
alive = True
except:
users = 0
soft_version = ""
if alive: if alive:
@ -197,10 +149,13 @@ def get_alive_servers(server):
downs = 0 downs = 0
if soft_version != "" and soft_version is not None: if self.soft_version != "" and self.soft_version is not None:
print("Server " + str(server) + " (" + serv_soft + " " + soft_version + ") is alive!")
print(f'\n** Server {self.server} ({self.software} {self.soft_version}) is alive! **')
else: else:
print("Server " + str(server) + " (" + serv_soft + ") is alive!")
print(f'\n** Server {self.server} ({self.software}) is alive! **')
insert_sql = "INSERT INTO fediverse(server, users, updated_at, software, alive, users_api, version, first_checked_at, last_checked_at, downs) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ON CONFLICT DO NOTHING" insert_sql = "INSERT INTO fediverse(server, users, updated_at, software, alive, users_api, version, first_checked_at, last_checked_at, downs) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ON CONFLICT DO NOTHING"
@ -212,100 +167,84 @@ def get_alive_servers(server):
cur = conn.cursor() cur = conn.cursor()
cur.execute(insert_sql, (server, users, now, serv_soft, alive, serv_api, soft_version, now, now, downs)) cur.execute(insert_sql, (self.server, self.users, now, self.software, alive, self.api, self.soft_version, now, now, downs))
if first_checked_at != None: if first_checked_at != None:
cur.execute("UPDATE fediverse SET users=(%s), updated_at=(%s), software=(%s), alive=(%s), users_api=(%s), version=(%s), last_checked_at=(%s), downs=(%s) where server=(%s)", (users, now, serv_soft, alive, serv_api, soft_version, now, downs, server)) cur.execute("UPDATE fediverse SET users=(%s), updated_at=(%s), software=(%s), alive=(%s), users_api=(%s), version=(%s), last_checked_at=(%s), downs=(%s) where server=(%s)", (self.users, now, self.software, alive, self.api, self.soft_version, now, downs, self.server))
else: else:
cur.execute("UPDATE fediverse SET users=(%s), updated_at=(%s), software=(%s), alive=(%s), users_api=(%s), version=(%s), first_checked_at=(%s), last_checked_at=(%s), downs=(%s) where server=(%s)", (users, now, serv_soft, alive, serv_api, soft_version, now, now, downs, server)) cur.execute("UPDATE fediverse SET users=(%s), updated_at=(%s), software=(%s), alive=(%s), users_api=(%s), version=(%s), first_checked_at=(%s), last_checked_at=(%s), downs=(%s) where server=(%s)", (self.users, now, self.software, alive, self.api, self.soft_version, now, now, downs, self.server))
cur.execute("UPDATE world SET checked='t' where server=(%s)", (server,)) cur.execute("UPDATE world SET checked='t' where server=(%s)", (self.server,))
conn.commit() conn.commit()
cur.close() cur.close()
except (Exception, psycopg2.DatabaseError) as error: except (Exception, psycopg2.DatabaseError) as error:
print(error) print(error)
finally: finally:
if conn is not None: if conn is not None:
conn.close() conn.close()
except urllib3.exceptions.ProtocolError as protoerr: except urllib3.exceptions.ProtocolError as protoerr:
print(protoerr) print_dead(self.server)
print("Server " + server + " is dead :-(")
alive = False
pass pass
except requests.exceptions.ChunkedEncodingError as chunkerr: except requests.exceptions.ChunkedEncodingError as chunkerr:
print(chunkerr) print_dead(self.server)
print("Server " + server + " is dead :-(")
alive = False
pass pass
except KeyError as e: except KeyError as e:
print(e) print_dead(self.server)
print("Server " + server + " is dead :-(")
alive = False
pass pass
except ValueError as verr: except ValueError as verr:
print(verr) print_dead(self.server)
print("Server " + server + " is dead :-(")
alive = False
pass pass
except requests.exceptions.SSLError as errssl: except requests.exceptions.SSLError as errssl:
print(errssl) print_dead(self.server)
print("Server " + server + " is dead :-(")
alive = False
pass pass
except requests.exceptions.HTTPError as errh: except requests.exceptions.HTTPError as errh:
print(errh) print_dead(self.server)
print("Server " + server + " is dead :-(")
alive = False
pass pass
except requests.exceptions.ConnectionError as errc: except requests.exceptions.ConnectionError as errc:
print(errc) print_dead(self.server)
print("Server " + server + " is dead :-(")
alive = False
pass pass
except requests.exceptions.Timeout as errt: except requests.exceptions.Timeout as errt:
print(errt) print_dead(self.server)
print("Server " + server + " is dead :-(")
alive = False
pass pass
except requests.exceptions.RequestException as err: except requests.exceptions.RequestException as err:
print(err) print_dead(self.server)
print("Server " + server + " is dead :-(")
alive = False
pass pass
except socket.gaierror as gai_error: except socket.gaierror as gai_error:
print(gai_error) print_dead(self.server)
pass pass
if alive is False: if not alive:
if downs_qty != None: if downs_qty != None:
@ -326,42 +265,13 @@ def get_alive_servers(server):
if first_checked_at != None: if first_checked_at != None:
cur.execute("UPDATE fediverse SET updated_at=(%s), alive=(%s), first_checked_at=(%s), downs=(%s) where server=(%s)", (now, alive, now, downs, server)) cur.execute("UPDATE fediverse SET updated_at=(%s), alive=(%s), first_checked_at=(%s), downs=(%s) where server=(%s)", (now, alive, now, downs, self.server))
else: else:
cur.execute("UPDATE fediverse SET updated_at=(%s), alive=(%s), downs=(%s) where server=(%s)", (now, alive, downs, server)) cur.execute("UPDATE fediverse SET updated_at=(%s), alive=(%s), downs=(%s) where server=(%s)", (now, alive, downs, self.server))
cur.execute("UPDATE world SET checked='t' where server=(%s)", (server,)) cur.execute("UPDATE world SET checked='t' where server=(%s)", (self.server,))
conn.commit()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
def write_api(server, software, users, alive, api, soft_version):
insert_sql = "INSERT INTO fediverse(server, updated_at, software, users, alive, users_api, version) VALUES(%s,%s,%s,%s,%s,%s,%s) ON CONFLICT DO NOTHING"
conn = None
try:
conn = psycopg2.connect(database=fediverse_db, user=fediverse_db_user, password="", host="/var/run/postgresql", port="5432")
cur = conn.cursor()
cur.execute(insert_sql, (server, now, software, users, alive, api, soft_version))
cur.execute("UPDATE fediverse SET updated_at=(%s), software=(%s), users=(%s), alive=(%s), users_api=(%s), version=(%s) where server=(%s)", (now, software, users, alive, api, soft_version, server))
cur.execute("UPDATE world SET checked='t' where server=(%s)", (server,))
conn.commit() conn.commit()
@ -377,84 +287,9 @@ def write_api(server, software, users, alive, api, soft_version):
conn.close() conn.close()
async def getsoft(server): def print_dead(server):
try: print(f'\nServer {server} is dead :-(')
socket.gethostbyname(server)
except socket.gaierror:
pass
return
soft = ''
url = 'https://' + server
user_agent = {'User-agent': 'Mozilla/5.0'}
timeout = aiohttp.ClientTimeout(total=3)
async with aiohttp.ClientSession(headers=user_agent, timeout=timeout) as session:
for api in apis:
try:
async with session.get(url+api) as response:
if response.status == 200:
try:
response_json = await response.json()
except:
pass
except aiohttp.ClientConnectorError as err:
print(err)
pass
else:
if response.status == 200 and api != '/api/v1/instance?':
try:
soft = response_json['software']['name']
soft = soft.lower()
soft_version = response_json['software']['version']
users = response_json['usage']['users']['total']
if users > 1000000:
return
alive = True
write_api(server, soft, users, alive, api, soft_version)
print("Server " + server + " (" + soft + " " + soft_version + ") is alive!")
return
except:
pass
if response.status == 200 and soft == '' and api == "/api/v1/instance?":
soft = 'mastodon'
users = response_json['stats']['user_count']
soft_version = response_json['version']
if users > 1000000:
return
alive = True
write_api(server, soft, users, alive, api)
print("Server " + server + " (" + soft + ") is alive!")
def getserver(server, x):
server = server[0].rstrip('.').lower()
if server.find(".") == -1:
return
if server.find("@") != -1:
return
if server.find("/") != -1:
return
if server.find(":") != -1:
return
try:
loop = asyncio.get_event_loop()
coroutines = [getsoft(server)]
soft = loop.run_until_complete(asyncio.gather(*coroutines, return_exceptions=True))
except:
pass
def set_world_servers_check_to_false(): def set_world_servers_check_to_false():
@ -562,7 +397,6 @@ def usage():
print('usage: python ' + sys.argv[0] + ' --multi' + ' (multiprocessing, fast)') print('usage: python ' + sys.argv[0] + ' --multi' + ' (multiprocessing, fast)')
print('usage: python ' + sys.argv[0] + ' --mono' + ' (one process, slow)') print('usage: python ' + sys.argv[0] + ' --mono' + ' (one process, slow)')
# Returns the parameter from the specified file
def get_parameter(parameter, file_path): def get_parameter(parameter, file_path):
# Check if secrets file exists # Check if secrets file exists
@ -595,7 +429,7 @@ if __name__ == '__main__':
if sys.argv[1] == '--multi': if sys.argv[1] == '--multi':
now = datetime.now() set_start_method("spawn")
mastodon, mastodon_hostname = mastodon() mastodon, mastodon_hostname = mastodon()
@ -608,12 +442,16 @@ if __name__ == '__main__':
alive_servers = get_last_checked_servers() alive_servers = get_last_checked_servers()
getservers = Server()
########################################################################### ###########################################################################
# multiprocessing! # multiprocessing!
nprocs = multiprocessing.cpu_count() with get_context("spawn").Pool(processes=32) as pool:
with multiprocessing.Pool(processes=64) as pool:
results = pool.starmap(get_alive_servers, product(alive_servers)) res = pool.starmap(getservers.get_alive_servers, product(alive_servers))
pool.close()
pool.join()
elif sys.argv[1] == '--mono': elif sys.argv[1] == '--mono':
@ -624,17 +462,22 @@ if __name__ == '__main__':
fediverse_db, fediverse_db_user = db_config() fediverse_db, fediverse_db_user = db_config()
total_servers = 0 total_servers = 0
total_users = 0 total_users = 0
set_world_servers_check_to_false() set_world_servers_check_to_false()
alive_servers = get_last_checked_servers() alive_servers = get_last_checked_servers()
getservers = Server()
i = 0 i = 0
while i < len(alive_servers): while i < len(alive_servers):
get_alive_servers(alive_servers[i]) getservers.server = alive_servers[i]
getservers.get_alive_servers()
i += 1 i += 1
@ -645,6 +488,8 @@ if __name__ == '__main__':
########################################################################### ###########################################################################
# get current total servers and users, get users from every software # get current total servers and users, get users from every software
now = datetime.now()
gettotals_sql = "select count(server), sum(users) from fediverse where alive" gettotals_sql = "select count(server), sum(users) from fediverse where alive"
get_soft_totals_sql = "select software, sum(users) as users, count(server) as servers from fediverse where users != 0 and alive group by software order by users desc" get_soft_totals_sql = "select software, sum(users) as users, count(server) as servers from fediverse where users != 0 and alive group by software order by users desc"
soft_total_project = [] soft_total_project = []
@ -948,4 +793,4 @@ if __name__ == '__main__':
users_image_id = mastodon.media_post('users.png', "image/png", description='users graph').id users_image_id = mastodon.media_post('users.png', "image/png", description='users graph').id
mastodon.status_post(toot_text, in_reply_to_id=None, media_ids={servers_image_id, users_image_id}) #mastodon.status_post(toot_text, in_reply_to_id=None, media_ids={servers_image_id, users_image_id})

Veure arxiu

@ -1,3 +1,6 @@
from multiprocessing import set_start_method
from multiprocessing import get_context
from itertools import product
import time import time
from datetime import datetime from datetime import datetime
import os import os
@ -5,22 +8,10 @@ import json
import sys import sys
import os.path import os.path
import psycopg2 import psycopg2
from multiprocessing import Pool, Manager
import aiohttp import aiohttp
import asyncio import asyncio
import socket import socket
import pdb
apis = ['/nodeinfo/2.0?',
'/nodeinfo/2.0.json?',
'/main/nodeinfo/2.0?',
'/api/statusnet/config?',
'/api/nodeinfo/2.0.json?',
'/api/nodeinfo?',
'/api/v1/instance?',
'/wp-json/nodeinfo/2.0?',
'/api/v1/instance/nodeinfo/2.0?',
'/.well-known/x-nodeinfo2?'
]
client_exceptions = ( client_exceptions = (
aiohttp.ClientResponseError, aiohttp.ClientResponseError,
@ -31,28 +22,24 @@ client_exceptions = (
socket.gaierror, socket.gaierror,
) )
def is_json(myjson):
try:
json_object = json.loads(myjson)
except ValueError as e:
return False
return True
def write_api(server, software, users, alive, api, soft_version): def write_api(server, software, users, alive, api, soft_version):
now = datetime.now()
fediverse_db, fediverse_db_user = get_db_config()
insert_sql = "INSERT INTO fediverse(server, updated_at, software, users, alive, users_api, version) VALUES(%s,%s,%s,%s,%s,%s,%s) ON CONFLICT DO NOTHING" insert_sql = "INSERT INTO fediverse(server, updated_at, software, users, alive, users_api, version) VALUES(%s,%s,%s,%s,%s,%s,%s) ON CONFLICT DO NOTHING"
conn = None conn = None
try: try:
conn = psycopg2.connect(database=fediverse_db, user=fediverse_db_user, password="", host="/var/run/postgresql", conn = psycopg2.connect(database=fediverse_db, user=fediverse_db_user, password="", host="/var/run/postgresql", port="5432")
port="5432")
cur = conn.cursor() cur = conn.cursor()
print(f'Writing {server} nodeinfo data...')
cur.execute(insert_sql, (server, now, software, users, alive, api, soft_version)) cur.execute(insert_sql, (server, now, software, users, alive, api, soft_version))
cur.execute( cur.execute(
@ -73,16 +60,20 @@ def write_api(server, software, users, alive, api, soft_version):
finally: finally:
if conn is not None: if conn is not None:
conn.close() conn.close()
async def getsoft(server): async def getsoft(server):
fediverse_db, fediverse_db_user = get_db_config()
try: try:
socket.gethostbyname(server) socket.gethostbyname(server)
except socket.gaierror: except socket.gaierror as g_error:
print(f'Server {server} error: {g_error}')
pass pass
return return
@ -93,69 +84,132 @@ async def getsoft(server):
user_agent = {'User-agent': 'Mozilla/5.0'} user_agent = {'User-agent': 'Mozilla/5.0'}
timeout = aiohttp.ClientTimeout(total=3) timeout = aiohttp.ClientTimeout(total=3)
async with aiohttp.ClientSession(timeout=timeout, headers=user_agent) as session: async with aiohttp.ClientSession(timeout=timeout, headers=user_agent) as session:
for api in apis:
try: try:
async with session.get(url + api) as response: async with session.get(url + '/.well-known/nodeinfo') as response:
if response.status == 200: if response.status == 200:
try: try:
response_json = await response.json() response_json = await response.json()
nodeinfo = response_json['links'][0]['href'].replace(f'https://{server}','')
except: except:
pass
else:
print(f'Server {server} not responding: {response.status}')
pass pass
except aiohttp.ClientConnectorError as err: async with session.get(url + nodeinfo) as nodeinfo_response:
if nodeinfo_response.status == 200:
try:
nodeinfo_json = await nodeinfo_response.json()
except:
pass pass
else: else:
if response.status == 200 and api != '/api/v1/instance?': print(f"Server {server}'s nodeinfo not responding: {response.status}")
if api != '/.well-known/x-nodeinfo2?': pass
except aiohttp.ClientConnectorError as cc_err:
pass
except aiohttp.client_exceptions.ClientConnectorSSLError as ccssl_as:
pass
else:
if nodeinfo_response.status == 200 and nodeinfo != '/api/v1/instance?':
if nodeinfo != '/.well-known/x-nodeinfo2?':
try: try:
soft = response_json['software']['name']
soft = nodeinfo_json['software']['name']
soft = soft.lower() soft = soft.lower()
soft_version = response_json['software']['version'] soft_version = nodeinfo_json['software']['version']
users = response_json['usage']['users']['total'] users = nodeinfo_json['usage']['users']['total']
if users > 1000000: if users > 1000000:
return return
alive = True alive = True
write_api(server, soft, users, alive, api, soft_version)
write_api(server, soft, users, alive, nodeinfo, soft_version)
print('*********************************************************************')
print("Server " + server + " (" + soft + " " + soft_version + ") is alive!") print("Server " + server + " (" + soft + " " + soft_version + ") is alive!")
print('*********************************************************************')
return return
except: except:
pass pass
else: else:
try: try:
soft = response_json['server']['software']
soft = nodeinfo_json['server']['software']
soft = soft.lower() soft = soft.lower()
soft_version = response_json['server']['version'] soft_version = nodeinfo_json['server']['version']
users = response_json['usage']['users']['total'] users = nodeinfo_json['usage']['users']['total']
if users > 1000000: if users > 1000000:
return return
alive = True alive = True
if soft == 'socialhome': if soft == 'socialhome':
write_api(server, soft, users, alive, api, soft_version) write_api(server, soft, users, alive, api, soft_version)
print('*********************************************************************')
print("Server " + server + " (" + soft + " " + soft_version + ") is alive!") print("Server " + server + " (" + soft + " " + soft_version + ") is alive!")
print('*********************************************************************')
return return
except: except:
pass pass
if response.status == 200 and soft == '' and api == "/api/v1/instance?": if nodeinfo_response.status == 200 and soft == '' and nodeinfo == "/api/v1/instance?":
soft = 'mastodon' soft = 'mastodon'
users = response_json['stats']['user_count'] users = nodeinfo_json['stats']['user_count']
soft_version = response_json['version'] soft_version = nodeinfo_json['version']
if users > 1000000: if users > 1000000:
return return
alive = True alive = True
write_api(server, soft, users, alive, api)
print("Server " + server + " (" + soft + ") is alive!")
def getserver(server, x): write_api(server, soft, users, alive, api)
print('*********************************************************************')
print("Server " + server + " (" + soft + ") is alive!")
print('*********************************************************************')
else:
print(f'Server {server} is dead')
def getserver(server, *args):
if len(args) != 0:
server = server[0].rstrip('.').lower() server = server[0].rstrip('.').lower()
@ -178,49 +232,7 @@ def getserver(server, x):
pass pass
def get_parameter(parameter, file_path): def get_world_servers():
# Check if secrets file exists
if not os.path.isfile(file_path):
print("File %s not found, exiting." % file_path)
sys.exit(0)
# Find parameter in file
with open(file_path) as f:
for line in f:
if line.startswith(parameter):
return line.replace(parameter + ":", "").strip()
# Cannot find parameter, exit
print(file_path + " Missing parameter %s " % parameter)
sys.exit(0)
def get_config():
# Load configuration from config file
config_filepath = "config/config.txt"
mastodon_hostname = get_parameter("mastodon_hostname", config_filepath)
return mastodon_hostname
def get_db_config():
# Load database config from db_config file
db_config_filepath = "config/db_config.txt"
fediverse_db = get_parameter("fediverse_db", db_config_filepath)
fediverse_db_user = get_parameter("fediverse_db_user", db_config_filepath)
return (fediverse_db, fediverse_db_user)
###############################################################################
# main
if __name__ == '__main__':
now = datetime.now()
start_time = time.time()
mastodon_hostname = get_config()
fediverse_db, fediverse_db_user = get_db_config()
world_servers = [] world_servers = []
@ -256,17 +268,94 @@ if __name__ == '__main__':
conn.close() conn.close()
########################################################################### return world_servers
# multiprocessing!
m = Manager() def get_parameter(parameter, file_path):
q = m.Queue()
z = zip(world_servers)
serv_number = len(world_servers) # Check if secrets file exists
if not os.path.isfile(file_path):
print("File %s not found, exiting." % file_path)
sys.exit(0)
pool_tuple = [(x, q) for x in z] # Find parameter in file
with Pool(processes=64) as pool: with open(file_path) as f:
pool.starmap(getserver, pool_tuple) for line in f:
if line.startswith(parameter):
return line.replace(parameter + ":", "").strip()
# Cannot find parameter, exit
print(file_path + " Missing parameter %s " % parameter)
sys.exit(0)
def usage():
print('usage: python ' + sys.argv[0] + ' --multi' + ' (multiprocessing, fast)')
print('usage: python ' + sys.argv[0] + ' --mono' + ' (one process, slow)')
def get_config():
# Load configuration from config file
config_filepath = "config/config.txt"
mastodon_hostname = get_parameter("mastodon_hostname", config_filepath)
return mastodon_hostname
def get_db_config():
# Load database config from db_config file
db_config_filepath = "config/db_config.txt"
fediverse_db = get_parameter("fediverse_db", db_config_filepath)
fediverse_db_user = get_parameter("fediverse_db_user", db_config_filepath)
return (fediverse_db, fediverse_db_user)
###############################################################################
# main
if __name__ == '__main__':
# usage modes
if len(sys.argv) == 1:
usage()
elif len(sys.argv) == 2:
if sys.argv[1] == '--multi':
now = datetime.now()
start_time = time.time()
mastodon_hostname = get_config()
fediverse_db, fediverse_db_user = get_db_config()
world_servers = get_world_servers()
with get_context("spawn").Pool(processes=32) as pool:
res = pool.starmap(getserver, product(world_servers))
pool.close()
pool.join()
print('Done.')
elif sys.argv[1] == '--mono':
now = datetime.now()
start_time = time.time()
mastodon_hostname = get_config()
fediverse_db, fediverse_db_user = get_db_config()
world_servers = get_world_servers()
for server in world_servers:
getserver(server)
print('Done.') print('Done.')