mirror of
http://gitea.phreedom.club/localhost_frssoft/funkwlmpv
synced 2024-11-21 21:51:28 +00:00
Instance session in class refactor
This commit is contained in:
parent
019b65e565
commit
220717d522
|
@ -1,4 +1,4 @@
|
|||
from src.fw_api import s, select_instance, instance, federate_search_by_url, get_instance_settings
|
||||
from src.fw_api import current_instance, federate_search_by_url, get_instance_settings
|
||||
from src.fw_radios import list_radios
|
||||
from src.fw_artists import list_artists
|
||||
from src.fw_albums import list_albums
|
||||
|
@ -41,7 +41,7 @@ def main():
|
|||
'Favorites',
|
||||
'Search',
|
||||
'Switch instance']
|
||||
if not s.headers.get('Authorization'):
|
||||
if not current_instance.s.headers.get('Authorization'):
|
||||
menu.append('Sign in')
|
||||
if support_message != '':
|
||||
menu.append('Donate')
|
||||
|
@ -79,7 +79,7 @@ def main():
|
|||
list_instances = conf.get('public_list_instances') + public_server_list_instances
|
||||
instance = fzf.prompt(list_instances,
|
||||
'--header='+quote(f'Select instance\nServer instances: +{new_ins_count}'))[0]
|
||||
select_instance(instance)
|
||||
current_instance.select_instance(instance)
|
||||
if selected == 'Sign in':
|
||||
print(f'''
|
||||
If You want sign in, please visit:
|
||||
|
@ -98,7 +98,7 @@ Insert token from "Access token" here''')
|
|||
del register_token
|
||||
del f
|
||||
|
||||
select_instance(instance)
|
||||
current_instance.select_instance(instance)
|
||||
if selected == 'Donate':
|
||||
print('Support instance message:')
|
||||
print(support_message)
|
||||
|
|
113
src/fw_api.py
113
src/fw_api.py
|
@ -25,44 +25,45 @@ else:
|
|||
f.write('{}')
|
||||
auth = {}
|
||||
|
||||
s = requests.Session()
|
||||
instance = get_config('instance')
|
||||
token = auth.get(instance)
|
||||
|
||||
if token:
|
||||
s.headers.update({
|
||||
"Authorization": "Bearer " + token,
|
||||
class current_instance:
|
||||
s = requests.Session()
|
||||
instance = get_config('instance')
|
||||
token = auth.get(instance)
|
||||
|
||||
@logger.catch
|
||||
def select_instance(new_instance=None):
|
||||
current_instance.instance = new_instance
|
||||
with open(auth_file, 'rt') as f:
|
||||
auth = json.loads(f.read())
|
||||
new_token = auth.get(current_instance.instance)
|
||||
current_instance.s.headers.update({"Authorization": None,
|
||||
"Accept-encoding": 'gzip'})
|
||||
set_http_header()
|
||||
if new_token:
|
||||
s.get(f'https://{current_instance.instance}')
|
||||
s.headers.update({
|
||||
"Authorization": "Bearer " + new_token,
|
||||
"Accept-encoding": 'gzip'
|
||||
})
|
||||
set_http_header(['Authorization: ' + 'Bearer ' + new_token])
|
||||
|
||||
|
||||
if current_instance.token:
|
||||
current_instance.s.headers.update({
|
||||
"Authorization": "Bearer " + current_instance.token,
|
||||
"Accept-encoding": 'gzip'
|
||||
})
|
||||
set_http_header(['Authorization: ' + 'Bearer ' + token])
|
||||
set_http_header(['Authorization: ' + 'Bearer ' + current_instance.token])
|
||||
else:
|
||||
s.headers.update({"Accept-encoding": 'gzip'})
|
||||
s.get(f'https://{instance}/') # Get cookies from unauthorized instance for working some functionality (radios)
|
||||
current_instance.s.headers.update({"Accept-encoding": 'gzip'})
|
||||
current_instance.s.get(f'https://{current_instance.instance}/') # Get cookies from unauthorized instance for working some functionality (radios)
|
||||
set_http_header()
|
||||
|
||||
|
||||
def select_instance(new_instance=None):
|
||||
global instance
|
||||
instance = new_instance
|
||||
with open(auth_file, 'rt') as f:
|
||||
auth = json.loads(f.read())
|
||||
new_token = auth.get(instance)
|
||||
s.headers.update({"Authorization": None,
|
||||
"Accept-encoding": 'gzip'})
|
||||
set_http_header()
|
||||
if new_token:
|
||||
s.get(f'https://{instance}')
|
||||
s.headers.update({
|
||||
"Authorization": "Bearer " + new_token,
|
||||
"Accept-encoding": 'gzip'
|
||||
})
|
||||
player.http_header_fields = ['Authorization: ' + 'Bearer ' + new_token]
|
||||
set_http_header(['Authorization: ' + 'Bearer ' + token])
|
||||
|
||||
|
||||
@logger.catch
|
||||
def get_instance_settings():
|
||||
r = s.get(f'https://{instance}/api/v1/instance/settings')
|
||||
r = current_instance.s.get(f'https://{current_instance.instance}/api/v1/instance/settings')
|
||||
return r.json()
|
||||
|
||||
|
||||
|
@ -76,9 +77,9 @@ def get_audio_file(track_uuid, listen_url=False, download=False,
|
|||
"to": to
|
||||
}
|
||||
if listen_url:
|
||||
url = f'https://{instance}{track_uuid}?'
|
||||
url = f'https://{current_instance.instance}{track_uuid}?'
|
||||
else:
|
||||
url = f'https://{instance}/api/v1/listen/{track_uuid}?'
|
||||
url = f'https://{current_instance.instance}/api/v1/listen/{track_uuid}?'
|
||||
return url + urllib.parse.urlencode(params)
|
||||
|
||||
|
||||
|
@ -96,9 +97,9 @@ def get_tracks(page=None, q=None, artist=None, album=None,
|
|||
'include_channels': include_channels
|
||||
}
|
||||
if pg:
|
||||
r = s.get(pg)
|
||||
r = current_instance.s.get(pg)
|
||||
else:
|
||||
r = s.get(f'https://{instance}/api/v1/tracks', params=params)
|
||||
r = current_instance.s.get(f'https://{current_instance.instance}/api/v1/tracks', params=params)
|
||||
return r.json()
|
||||
|
||||
|
||||
|
@ -112,9 +113,9 @@ def get_favorires_tracks(page=None, q=None, scope=None, include_channels=None, p
|
|||
'include_channels': include_channels
|
||||
}
|
||||
if pg:
|
||||
r = s.get(pg)
|
||||
r = current_instance.s.get(pg)
|
||||
else:
|
||||
r = s.get(f'https://{instance}/api/v1/favorites/tracks/', params=params)
|
||||
r = current_instance.s.get(f'https://{current_instance.instance}/api/v1/favorites/tracks/', params=params)
|
||||
return r.json()
|
||||
|
||||
|
||||
|
@ -130,9 +131,9 @@ def get_artists(page=None, q=None, artist=None, album=None, favourites=None, ref
|
|||
'refresh': refresh
|
||||
}
|
||||
if pg:
|
||||
r = s.get(pg)
|
||||
r = current_instance.s.get(pg)
|
||||
else:
|
||||
r = s.get(f'https://{instance}/api/v1/artists', params=params)
|
||||
r = current_instance.s.get(f'https://{current_instance.instance}/api/v1/artists', params=params)
|
||||
return r.json()
|
||||
|
||||
|
||||
|
@ -147,9 +148,9 @@ def get_albums(page=None, q=None, artist=None, include_channels=None, refresh=Fa
|
|||
'refresh': refresh
|
||||
}
|
||||
if pg:
|
||||
r = s.get(pg)
|
||||
r = current_instance.s.get(pg)
|
||||
else:
|
||||
r = s.get(f'https://{instance}/api/v1/albums', params=params)
|
||||
r = current_instance.s.get(f'https://{current_instance.instance}/api/v1/albums', params=params)
|
||||
return r.json()
|
||||
|
||||
|
||||
|
@ -161,9 +162,9 @@ def get_channels(page=None, q=None, tag=None, pg=None):
|
|||
'tag': tag
|
||||
}
|
||||
if pg:
|
||||
r = s.get(pg)
|
||||
r = current_instance.s.get(pg)
|
||||
else:
|
||||
r = s.get(f'https://{instance}/api/v1/channels', params=params)
|
||||
r = current_instance.s.get(f'https://{current_instance.instance}/api/v1/channels', params=params)
|
||||
return r.json()
|
||||
|
||||
|
||||
|
@ -177,9 +178,9 @@ def get_playlists(page=None, page_size=None, q=None, ordering='-modification_dat
|
|||
'ordering': ordering
|
||||
}
|
||||
if pg:
|
||||
r = s.get(pg)
|
||||
r = current_instance.s.get(pg)
|
||||
else:
|
||||
r = s.get(f'https://{instance}/api/v1/playlists', params=params)
|
||||
r = current_instance.s.get(f'https://{current_instance.instance}/api/v1/playlists', params=params)
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
|
||||
|
@ -188,9 +189,9 @@ def get_playlists(page=None, page_size=None, q=None, ordering='-modification_dat
|
|||
def get_playlist_tracks(playlist_id, pg=None):
|
||||
'''Retrieve all tracks in the playlist'''
|
||||
if pg:
|
||||
r = s.get(pg)
|
||||
r = current_instance.s.get(pg)
|
||||
else:
|
||||
r = s.get(f'https://{instance}/api/v1/playlists/{playlist_id}/tracks')
|
||||
r = current_instance.s.get(f'https://{current_instance.instance}/api/v1/playlists/{playlist_id}/tracks')
|
||||
return r.json()
|
||||
|
||||
|
||||
|
@ -203,9 +204,9 @@ def list_libraries(page=None, page_size=None, q=None, scope='all', pg=None):
|
|||
'scope': scope,
|
||||
}
|
||||
if pg:
|
||||
r = s.get(pg)
|
||||
r = current_instance.s.get(pg)
|
||||
else:
|
||||
r = s.get(f'https://{instance}/api/v1/libraries', params=params)
|
||||
r = current_instance.s.get(f'https://{current_instance.instance}/api/v1/libraries', params=params)
|
||||
return r.json()
|
||||
|
||||
@logger.catch
|
||||
|
@ -214,9 +215,9 @@ def get_tags(q=None, pg=None):
|
|||
'q': q,
|
||||
}
|
||||
if pg:
|
||||
r = s.get(pg)
|
||||
r = current_instance.s.get(pg)
|
||||
else:
|
||||
r = s.get(f'https://{instance}/api/v1/tags', params=params)
|
||||
r = current_instance.s.get(f'https://{current_instance.instance}/api/v1/tags', params=params)
|
||||
return r.json()
|
||||
|
||||
|
||||
|
@ -225,20 +226,20 @@ def federate_search_by_url(object):
|
|||
params = {
|
||||
'object': object
|
||||
}
|
||||
r = s.post(f'https://{instance}/api/v1/federation/fetches', json=params)
|
||||
r = current_instance.s.post(f'https://{current_instance.instance}/api/v1/federation/fetches', json=params)
|
||||
return r.json()
|
||||
|
||||
|
||||
@logger.catch
|
||||
def favorite_track(track_id):
|
||||
r = s.post(f'https://{instance}/api/v1/favorites/tracks', json={'track': int(track_id)})
|
||||
r = current_instance.s.post(f'https://{current_instance.instance}/api/v1/favorites/tracks', json={'track': int(track_id)})
|
||||
r.raise_for_status()
|
||||
return r.json
|
||||
|
||||
|
||||
@logger.catch
|
||||
def unfavorite_track(track_id):
|
||||
r = s.post(f'https://{instance}/api/v1/favorites/tracks/delete', json={'track': int(track_id)})
|
||||
r = current_instance.s.post(f'https://{current_instance.instance}/api/v1/favorites/tracks/delete', json={'track': int(track_id)})
|
||||
r.raise_for_status()
|
||||
return r.json
|
||||
|
||||
|
@ -246,24 +247,24 @@ def unfavorite_track(track_id):
|
|||
@logger.catch
|
||||
def hide_content(content):
|
||||
'''This function hide content (write permission)'''
|
||||
r = s.post(f'https://{instance}/api/v1/moderation/content-filters/', json=content)
|
||||
r = current_instance.s.post(f'https://{current_instance.instance}/api/v1/moderation/content-filters/', json=content)
|
||||
r.raise_for_status()
|
||||
return r.json
|
||||
|
||||
|
||||
# [FunkWhale radios]
|
||||
def get_radios():
|
||||
r = s.get(f'https://{instance}/api/v1/radios/radios/')
|
||||
r = current_instance.s.get(f'https://{current_instance.instance}/api/v1/radios/radios/')
|
||||
return r.json()
|
||||
|
||||
|
||||
def post_radio_session(requested_radio):
|
||||
r = s.post(f'https://{instance}/api/v1/radios/sessions/', json=requested_radio)
|
||||
r = current_instance.s.post(f'https://{current_instance.instance}/api/v1/radios/sessions/', json=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)
|
||||
r = current_instance.s.post(f'https://{current_instance.instance}/api/v1/radios/tracks/', json=radio_session)
|
||||
return r.json()
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from src.fw_api import s, get_radios, post_radio_session, get_track_radio, list_libraries, favorite_track, get_audio_file
|
||||
from src.fw_api import current_instance, get_radios, post_radio_session, get_track_radio, list_libraries, favorite_track, get_audio_file
|
||||
from src.fw_libraries import libraries
|
||||
from src.fw_tags import list_tags
|
||||
from src.utils import download_track
|
||||
|
@ -29,7 +29,7 @@ def list_radios():
|
|||
if descr and descr != "":
|
||||
radio_option += f' | {descr}'
|
||||
view.append(f'{index}.{radio_option}')
|
||||
if s.headers.get('Authorization'): # Radios avalaible only for auth user
|
||||
if current_instance.s.headers.get('Authorization'): # Radios avalaible only for auth user
|
||||
view.append('Favourites')
|
||||
view.append('Less listened')
|
||||
view.extend(['Tag', 'Random', 'Libraries', 'Users', 'Recently Added'])
|
||||
|
|
|
@ -4,7 +4,7 @@ from urllib.parse import unquote
|
|||
|
||||
def get_remote_file_name(url):
|
||||
'''This function return filename by content-disposition header'''
|
||||
r = src.fw_api.s.head(url)
|
||||
r = src.fw_api.current_instance.s.head(url)
|
||||
content_dispos = r.headers.get('content-disposition')
|
||||
if content_dispos.startswith('attachment; filename*=UTF-8\'\''):
|
||||
return unquote(content_dispos.split('attachment; filename*=UTF-8\'\'')[-1])
|
||||
|
@ -12,7 +12,7 @@ def get_remote_file_name(url):
|
|||
|
||||
def download_track(url, name=None):
|
||||
url = url.split('?')[0] # Stripe all params from url
|
||||
r = src.fw_api.s.get(url)
|
||||
r = src.fw_api.current_instance.s.get(url)
|
||||
if not name:
|
||||
name = get_remote_file_name(url)
|
||||
if not name:
|
||||
|
|
Loading…
Reference in a new issue