Some changes:

* Fixes radio
* Preparing for separate config in code and config file
* Added fetching new instances from network.funkwhale.audio
This commit is contained in:
localhost_frssoft 2022-11-09 21:51:20 +03:00
parent 8245cd433e
commit 52cf8869b4
5 changed files with 89 additions and 13 deletions

3
.gitignore vendored
View File

@ -1,7 +1,4 @@
**/__pycache__
tags_db
music_dl
playlist.m3u8
preload
instance.hist
.auth.json

View File

@ -3,6 +3,7 @@ from src.fw_radios import list_radios
from src.fw_artists import list_artists
from src.fw_albums import list_albums
from src.fw_channels import list_channels
import src.settings as settings
import json, sys
from loguru import logger
from pyfzf.pyfzf import FzfPrompt
@ -45,7 +46,7 @@ def main():
if selected == 'Switch instance':
with open('config.json', 'rt') as f:
conf = json.loads(f.read())
list_instances = conf.get('public_list_instances')
list_instances = conf.get('public_list_instances') + settings.get_new_funkwhale_servers()
instance = fzf.prompt(list_instances, '--header \'Select instance\'')[0]
select_instance(instance)
if selected == 'Sign in':

View File

@ -186,11 +186,8 @@ def post_radio_session(requested_radio):
return r.json()
@logger.catch
def get_track_radio(radio_session):
r = s.post(f'https://{instance}/api/v1/radios/tracks/',json=radio_session)
try:
r.raise_for_status()
return r.json()
except:
logger.exception('Radio: get next track failed')
r = s.post(f'https://{instance}/api/v1/radios/tracks/', json=radio_session)
return r.json()

View File

@ -96,7 +96,7 @@ def radio_load(id_radio=None, type_radio='custom', name=None, related_object=Non
try:
select = fzf.prompt(('Next', 'Prev', 'Pause', 'Download', 'Info', 'Exit'), f"--header=\'Radio {name} playing...\'")[0]
if select == 'Next':
radio_get_track(radio_session_id)
threading.Thread(target=radio_get_track, args=(radio_session_id,), daemon=True).start()
player.playlist_next()
elif select == 'Prev':
player.playlist_prev()
@ -130,7 +130,15 @@ def radio_load(id_radio=None, type_radio='custom', name=None, related_object=Non
player.playlist_clear()
player.stop()
break
except KeyboardInterrupt:
except:
try:
radio_event_gen.clear()
except:
logger.exception('Error stopping Thread radio generator')
pass
player.playlist_clear()
player.stop()
logger.exception('Radio force stopped')
break
@ -138,7 +146,11 @@ def radio_get_track(radio_session_id):
radio_context = get_track_radio({'session': radio_session_id})
if not radio_context:
return
if radio_context == "Radio doesn't have more candidates":
if isinstance(radio_context, str):
logger.error(radio_context)
return
if radio_context.get('error'):
logger.error(radio_context.get('error'))
return
else:
track = radio_context.get('track')

69
src/settings.py Normal file
View File

@ -0,0 +1,69 @@
import json, requests, time
from loguru import logger
defaut_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"
]
}
@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