2022-11-09 18:51:20 +00:00
import json , requests , time
2022-11-09 23:23:18 +00:00
from os . path import exists
2022-11-09 18:51:20 +00:00
from loguru import logger
2022-11-10 15:55:36 +00:00
from pyfzf . pyfzf import FzfPrompt
fzf = FzfPrompt ( )
2022-11-09 18:51:20 +00:00
2022-11-09 23:23:18 +00:00
conf_file = ' config.json '
default_conf = {
2022-11-09 18:51:20 +00:00
' 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 "
2022-11-10 00:23:46 +00:00
] ,
2022-11-21 08:07:52 +00:00
' enable_server_transcoding ' : False ,
2022-11-13 02:16:46 +00:00
' prefetch_playlist ' : True ,
2022-11-14 23:02:22 +00:00
' mpv_volume ' : 100 ,
2022-11-14 20:05:19 +00:00
' show_like_button ' : True ,
' show_artist_name_in_albums ' : False
2022-11-09 18:51:20 +00:00
}
2022-11-09 23:23:18 +00:00
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 ( ) :
2022-11-10 00:23:46 +00:00
''' Check config and remove or add keys if not found in default config '''
2022-11-09 23:23:18 +00:00
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 } ' )
2022-11-10 00:23:46 +00:00
for k , v in default_conf . items ( ) :
if k not in correct_conf . keys ( ) :
correct_conf [ k ] = v
logger . warning ( f ' { k } added in config. Value: { v } ' )
2022-11-09 23:23:18 +00:00
set_defaults ( correct_conf )
2022-11-10 15:55:36 +00:00
return correct_conf
2022-11-09 23:23:18 +00:00
if not exists ( conf_file ) :
set_defaults ( )
else :
check_config ( )
2022-11-10 00:23:46 +00:00
def get_config ( key ) :
''' return value from config by key '''
with open ( conf_file , ' rt ' ) as f :
loaded_config = json . loads ( f . read ( ) )
return loaded_config . get ( key )
2022-11-09 18:51:20 +00:00
@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 :
2022-11-09 23:37:23 +00:00
if i [ 0 ] not in default_conf [ ' public_list_instances ' ] and i [ 1 ] :
2022-11-09 18:51:20 +00:00
new_instances . append ( i [ 0 ] )
return new_instances