Instance session in class refactor

This commit is contained in:
localhost_frssoft 2022-12-04 04:01:11 +03:00
parent 019b65e565
commit 220717d522
4 changed files with 65 additions and 64 deletions

View file

@ -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_radios import list_radios
from src.fw_artists import list_artists from src.fw_artists import list_artists
from src.fw_albums import list_albums from src.fw_albums import list_albums
@ -41,7 +41,7 @@ def main():
'Favorites', 'Favorites',
'Search', 'Search',
'Switch instance'] 'Switch instance']
if not s.headers.get('Authorization'): if not current_instance.s.headers.get('Authorization'):
menu.append('Sign in') menu.append('Sign in')
if support_message != '': if support_message != '':
menu.append('Donate') menu.append('Donate')
@ -79,7 +79,7 @@ def main():
list_instances = conf.get('public_list_instances') + public_server_list_instances list_instances = conf.get('public_list_instances') + public_server_list_instances
instance = fzf.prompt(list_instances, instance = fzf.prompt(list_instances,
'--header='+quote(f'Select instance\nServer instances: +{new_ins_count}'))[0] '--header='+quote(f'Select instance\nServer instances: +{new_ins_count}'))[0]
select_instance(instance) current_instance.select_instance(instance)
if selected == 'Sign in': if selected == 'Sign in':
print(f''' print(f'''
If You want sign in, please visit: If You want sign in, please visit:
@ -98,7 +98,7 @@ Insert token from "Access token" here''')
del register_token del register_token
del f del f
select_instance(instance) current_instance.select_instance(instance)
if selected == 'Donate': if selected == 'Donate':
print('Support instance message:') print('Support instance message:')
print(support_message) print(support_message)

View file

@ -25,44 +25,45 @@ else:
f.write('{}') f.write('{}')
auth = {} auth = {}
class current_instance:
s = requests.Session() s = requests.Session()
instance = get_config('instance') instance = get_config('instance')
token = auth.get(instance) token = auth.get(instance)
if token: @logger.catch
s.headers.update({
"Authorization": "Bearer " + token,
"Accept-encoding": 'gzip'
})
set_http_header(['Authorization: ' + 'Bearer ' + token])
else:
s.headers.update({"Accept-encoding": 'gzip'})
s.get(f'https://{instance}/') # Get cookies from unauthorized instance for working some functionality (radios)
set_http_header()
def select_instance(new_instance=None): def select_instance(new_instance=None):
global instance current_instance.instance = new_instance
instance = new_instance
with open(auth_file, 'rt') as f: with open(auth_file, 'rt') as f:
auth = json.loads(f.read()) auth = json.loads(f.read())
new_token = auth.get(instance) new_token = auth.get(current_instance.instance)
s.headers.update({"Authorization": None, current_instance.s.headers.update({"Authorization": None,
"Accept-encoding": 'gzip'}) "Accept-encoding": 'gzip'})
set_http_header() set_http_header()
if new_token: if new_token:
s.get(f'https://{instance}') s.get(f'https://{current_instance.instance}')
s.headers.update({ s.headers.update({
"Authorization": "Bearer " + new_token, "Authorization": "Bearer " + new_token,
"Accept-encoding": 'gzip' "Accept-encoding": 'gzip'
}) })
player.http_header_fields = ['Authorization: ' + 'Bearer ' + new_token] set_http_header(['Authorization: ' + 'Bearer ' + new_token])
set_http_header(['Authorization: ' + 'Bearer ' + token])
if current_instance.token:
current_instance.s.headers.update({
"Authorization": "Bearer " + current_instance.token,
"Accept-encoding": 'gzip'
})
set_http_header(['Authorization: ' + 'Bearer ' + current_instance.token])
else:
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()
@logger.catch @logger.catch
def get_instance_settings(): 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() return r.json()
@ -76,9 +77,9 @@ def get_audio_file(track_uuid, listen_url=False, download=False,
"to": to "to": to
} }
if listen_url: if listen_url:
url = f'https://{instance}{track_uuid}?' url = f'https://{current_instance.instance}{track_uuid}?'
else: 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) 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 'include_channels': include_channels
} }
if pg: if pg:
r = s.get(pg) r = current_instance.s.get(pg)
else: 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() 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 'include_channels': include_channels
} }
if pg: if pg:
r = s.get(pg) r = current_instance.s.get(pg)
else: 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() return r.json()
@ -130,9 +131,9 @@ def get_artists(page=None, q=None, artist=None, album=None, favourites=None, ref
'refresh': refresh 'refresh': refresh
} }
if pg: if pg:
r = s.get(pg) r = current_instance.s.get(pg)
else: 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() return r.json()
@ -147,9 +148,9 @@ def get_albums(page=None, q=None, artist=None, include_channels=None, refresh=Fa
'refresh': refresh 'refresh': refresh
} }
if pg: if pg:
r = s.get(pg) r = current_instance.s.get(pg)
else: 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() return r.json()
@ -161,9 +162,9 @@ def get_channels(page=None, q=None, tag=None, pg=None):
'tag': tag 'tag': tag
} }
if pg: if pg:
r = s.get(pg) r = current_instance.s.get(pg)
else: 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() return r.json()
@ -177,9 +178,9 @@ def get_playlists(page=None, page_size=None, q=None, ordering='-modification_dat
'ordering': ordering 'ordering': ordering
} }
if pg: if pg:
r = s.get(pg) r = current_instance.s.get(pg)
else: 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() r.raise_for_status()
return r.json() 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): def get_playlist_tracks(playlist_id, pg=None):
'''Retrieve all tracks in the playlist''' '''Retrieve all tracks in the playlist'''
if pg: if pg:
r = s.get(pg) r = current_instance.s.get(pg)
else: 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() return r.json()
@ -203,9 +204,9 @@ def list_libraries(page=None, page_size=None, q=None, scope='all', pg=None):
'scope': scope, 'scope': scope,
} }
if pg: if pg:
r = s.get(pg) r = current_instance.s.get(pg)
else: 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() return r.json()
@logger.catch @logger.catch
@ -214,9 +215,9 @@ def get_tags(q=None, pg=None):
'q': q, 'q': q,
} }
if pg: if pg:
r = s.get(pg) r = current_instance.s.get(pg)
else: 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() return r.json()
@ -225,20 +226,20 @@ def federate_search_by_url(object):
params = { params = {
'object': object '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() return r.json()
@logger.catch @logger.catch
def favorite_track(track_id): 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() r.raise_for_status()
return r.json return r.json
@logger.catch @logger.catch
def unfavorite_track(track_id): 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() r.raise_for_status()
return r.json return r.json
@ -246,24 +247,24 @@ def unfavorite_track(track_id):
@logger.catch @logger.catch
def hide_content(content): def hide_content(content):
'''This function hide content (write permission)''' '''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() r.raise_for_status()
return r.json return r.json
# [FunkWhale radios] # [FunkWhale radios]
def get_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() return r.json()
def post_radio_session(requested_radio): 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() return r.json()
@logger.catch @logger.catch
def get_track_radio(radio_session): 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() return r.json()

View file

@ -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_libraries import libraries
from src.fw_tags import list_tags from src.fw_tags import list_tags
from src.utils import download_track from src.utils import download_track
@ -29,7 +29,7 @@ def list_radios():
if descr and descr != "": if descr and descr != "":
radio_option += f' | {descr}' radio_option += f' | {descr}'
view.append(f'{index}.{radio_option}') 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('Favourites')
view.append('Less listened') view.append('Less listened')
view.extend(['Tag', 'Random', 'Libraries', 'Users', 'Recently Added']) view.extend(['Tag', 'Random', 'Libraries', 'Users', 'Recently Added'])

View file

@ -4,7 +4,7 @@ from urllib.parse import unquote
def get_remote_file_name(url): def get_remote_file_name(url):
'''This function return filename by content-disposition header''' '''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') content_dispos = r.headers.get('content-disposition')
if content_dispos.startswith('attachment; filename*=UTF-8\'\''): if content_dispos.startswith('attachment; filename*=UTF-8\'\''):
return unquote(content_dispos.split('attachment; filename*=UTF-8\'\'')[-1]) 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): def download_track(url, name=None):
url = url.split('?')[0] # Stripe all params from url 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: if not name:
name = get_remote_file_name(url) name = get_remote_file_name(url)
if not name: if not name: