funkwlmpv/src/settings.py

100 lines
3.5 KiB
Python

import json, requests, time
from os.path import exists
from loguru import logger
conf_file = 'config.json'
default_conf = {
'instance': 'fw.ponychord.rocks',
'public_list_instances': [
"open.audio",
"audio.securetown.top",
"funkwhale.co.uk",
"am.pirateradio.social",
"audio.liberta.vip",
"audio.gafamfree.party",
"tanukitunes.com",
"funkwhale.juniorjpdj.pl",
"tavia.mle.party",
"funkwhale.thurk.org",
"buzzworkers.com",
"soundship.de",
"funkwhale.kameha.click",
"music.chosto.me",
"zik.goe.land",
"music.humanoids.be",
"music.hempton.us",
"mizik.o-k-i.net",
"klh.radiolivre.org",
"hudba.feildel.fr",
"funkwhale.mita.me",
"funk.deko.cloud",
"audio.graz.social",
"funkwhale.desmu.fr",
"listen.knsm.cc",
"funkwhale.gegeweb.eu",
"shitnoise.monster"
]
}
def set_defaults(corrected_config=None):
conf_rewrite = default_conf
if corrected_config:
conf_rewrite = corrected_config
with open(conf_file, 'wt') as f:
f.write(json.dumps(conf_rewrite, indent=4))
def check_config():
'''Check config and remove keys if not found in default config'''
with open(conf_file, 'rt') as f:
loaded_config = json.loads(f.read())
correct_conf = {}
for k, v in loaded_config.items():
if k in default_conf.keys():
correct_conf[k] = v
else:
logger.warning(f'{k} from config will be removed. Value: {v}')
set_defaults(correct_conf)
if not exists(conf_file):
set_defaults()
else:
check_config()
@logger.catch
def get_new_funkwhale_servers():
# Uses official API network.funkwhale.audio for getting new instances
public_server_api = 'https://network.funkwhale.audio/dashboards/api/tsdb/query'
now = int(time.time())
timeback = now - 86400
request_public_servers = {
'from': f"{timeback}",
'to': f"{now}",
'queries': [
{
'refId': "A",
'intervalMs': 60000,
'maxDataPoints': 1174,
'datasourceId': 1,
'rawSql': "SELECT * FROM (\n SELECT\n DISTINCT on (c.domain) c.domain as \"Name\",\n c.up as \"Is up\",\n coalesce(c.open_registrations, false) as \"Open registrations\",\n coalesce(anonymous_can_listen, false) as \"Anonymous can listen\",\n coalesce(c.usage_users_total, 0) as \"Total users\",\n coalesce(c.usage_users_active_month, 0) as \"Active users (this month)\",\n coalesce(c.software_version_major, 0)::text || '.' || coalesce(c.software_version_minor, 0)::text || '.' || coalesce(c.software_version_patch, 0)::text as \"Version\",\n c.time as \"Last checked\",\n d.first_seen as \"First seen\"\n FROM checks as c\n INNER JOIN domains AS d ON d.name = c.domain\n WHERE d.blocked = false AND c.up = true AND c.time > now() - INTERVAL '7 days'\n AND c.anonymous_can_listen IN ('true')\n AND c.open_registrations IN ('true','false')\n\n ORDER BY c.domain, c.time DESC\n) as t ORDER BY \"Active users (this month)\" DESC",
'format': "table"
}
]
}
r = requests.post(public_server_api, json=request_public_servers)
results = r.json()
new_instances = []
if results:
new_instances_list = results['results']['A']['tables'][0]['rows']
for i in new_instances_list:
if i[0] not in defaut_conf['public_list_instances'] and i[1]:
new_instances.append(i[0])
return new_instances