initial commit
This commit is contained in:
parent
17a778e7d4
commit
0ed22467b9
|
@ -0,0 +1,101 @@
|
|||
#!/usr/bin/python3
|
||||
import requests
|
||||
import hashlib
|
||||
import pickle
|
||||
import time
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
|
||||
DOMAIN = 'mastodon.social'
|
||||
|
||||
def main():
|
||||
global DOMAIN
|
||||
if len(sys.argv) > 1:
|
||||
DOMAIN = sys.argv[1]
|
||||
|
||||
res = http_get('https://api.joinmastodon.org/servers?language=en&category=®ion=&ownership=®istrations=')
|
||||
res = json.loads(res.text)
|
||||
|
||||
failed_servers = 0
|
||||
banned_by = []
|
||||
for it in res:
|
||||
print('checking "{}" has banned "{}"... '\
|
||||
.format(it['domain'], DOMAIN))
|
||||
acct = get_admin_account(it['domain'])
|
||||
if acct == False:
|
||||
failed_servers += 1
|
||||
continue
|
||||
|
||||
r = http_get('https://'+DOMAIN+'/'+acct, True)
|
||||
if r == False:
|
||||
failed_servers += 1
|
||||
continue
|
||||
if r.status_code == 404:
|
||||
banned_by.append(it['domain'])
|
||||
print('YES')
|
||||
else:
|
||||
print('NO')
|
||||
print()
|
||||
print('////////')
|
||||
print('Instance: '+DOMAIN)
|
||||
print('Banned by: '+json.dumps(banned_by))
|
||||
print('Servers count: total={} | failed={} | used_in_percentage={}'.format(len(res), failed_servers, len(res)-failed_servers))
|
||||
print('Percentage of bans: '+str( (len(banned_by) * 100) / (len(res)-failed_servers) )+'% has banned the instance')
|
||||
print()
|
||||
|
||||
def http_get(url, show=False):
|
||||
try:
|
||||
fl = '/tmp/.check-instance-ban__'+md5(url)
|
||||
if os.path.exists(fl):
|
||||
ts = int(os.path.getmtime(fl))
|
||||
secs = int(time.time()) - ts
|
||||
if secs <= 1800: # cache 30 mins
|
||||
if show:
|
||||
print(' HTTP-cached: '+url)
|
||||
return cache_read(fl)
|
||||
if show:
|
||||
print(' HTTP: '+url)
|
||||
r = requests.get(url, timeout=5)
|
||||
cache_write(fl, r)
|
||||
return r
|
||||
except requests.exceptions.ConnectionError:
|
||||
pass
|
||||
return False
|
||||
|
||||
def cache_read(f):
|
||||
with open(f,'rb') as in_file:
|
||||
return pickle.load(in_file)
|
||||
return None
|
||||
|
||||
def cache_write(f,o):
|
||||
with open(f, 'wb') as out_file:
|
||||
pickle.dump(o, out_file)
|
||||
return True
|
||||
|
||||
def get_admin_account(domain):
|
||||
r = http_get('https://'+domain+'/api/v1/instance')
|
||||
if r == False:
|
||||
return False
|
||||
js = json.loads(r.text)
|
||||
return '@'+js['contact_account']['username']+'@'+domain
|
||||
|
||||
def md5(s):
|
||||
return hashlib.md5(s.encode('utf8')).hexdigest()
|
||||
|
||||
def readf(f):
|
||||
r = open(f,'r')
|
||||
c = r.read().strip()
|
||||
r.close()
|
||||
return c
|
||||
|
||||
def appenf(f,c):
|
||||
return writef(f,c,'a')
|
||||
|
||||
def writef(f,c,t='w'):
|
||||
w = open(f,t)
|
||||
w.write(c)
|
||||
w.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue