Removed eventlet and added get_session and set_session

This commit is contained in:
RemixDev 2021-02-11 19:04:05 +01:00
parent 6f90d520f2
commit ad4ccf1b1d
No known key found for this signature in database
GPG key ID: B33962B465BDB51C
5 changed files with 29 additions and 17 deletions

View file

@ -1,5 +1,4 @@
import eventlet import requests
requests = eventlet.import_patched('requests')
from deezer.gw import GW from deezer.gw import GW
from deezer.api import API from deezer.api import API
import json import json
@ -18,15 +17,13 @@ class TrackFormats():
LOCAL = 0 LOCAL = 0
class Deezer: class Deezer:
def __init__(self): def __init__(self, accept_language=None):
self.http_headers = { self.http_headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " \ "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " \
"Chrome/79.0.3945.130 Safari/537.36", "Chrome/79.0.3945.130 Safari/537.36",
"Accept-Language": None "Accept-Language": accept_language
} }
self.session = requests.Session() self.session = requests.Session()
self.session.mount('http://', requests.adapters.HTTPAdapter(pool_maxsize=100))
self.session.mount('https://', requests.adapters.HTTPAdapter(pool_maxsize=100))
self.logged_in = False self.logged_in = False
self.current_user = {} self.current_user = {}
@ -36,11 +33,27 @@ class Deezer:
self.api = API(self.session, self.http_headers) self.api = API(self.session, self.http_headers)
self.gw = GW(self.session, self.http_headers) self.gw = GW(self.session, self.http_headers)
def get_accept_language(self):
return self.http_headers['Accept-Language']
def set_accept_language(self, lang): def set_accept_language(self, lang):
self.http_headers['Accept-Language'] = lang self.http_headers['Accept-Language'] = lang
def get_accept_language(self): def get_session(self):
return self.http_headers['Accept-Language'] return {
'logged_in': self.logged_in,
'current_user': self.current_user,
'childs': self.childs,
'selected_account': self.selected_account,
'cookies': self.session.cookies.get_dict()
}
def set_session(self, data):
self.logged_in = data['logged_in']
self.current_user = data['current_user']
self.childs = data['childs']
self.selected_account = data['selected_account']
self.session.cookies.update(data['cookies'])
def login(self, email, password, re_captcha_token, child=0): def login(self, email, password, re_captcha_token, child=0):
# Check if user already logged in # Check if user already logged in

View file

@ -1,5 +1,5 @@
import eventlet import requests
requests = eventlet.import_patched('requests') from time import sleep
class SearchOrder(): class SearchOrder():
"""Possible values for order parameter in search""" """Possible values for order parameter in search"""
@ -34,12 +34,12 @@ class API:
) )
result_json = result.json() result_json = result.json()
except: except:
eventlet.sleep(2) sleep(2)
return self.api_call(method, args) return self.api_call(method, args)
if 'error' in result_json.keys(): if 'error' in result_json.keys():
if 'code' in result_json['error']: if 'code' in result_json['error']:
if result_json['error']['code'] in [4, 700]: if result_json['error']['code'] in [4, 700]:
eventlet.sleep(5) sleep(5)
return self.api_call(method, args) return self.api_call(method, args)
if result_json['error']['code'] == 100: raise ItemsLimitExceededException(f"ItemsLimitExceededException: {method}") if result_json['error']['code'] == 100: raise ItemsLimitExceededException(f"ItemsLimitExceededException: {method}")
if result_json['error']['code'] == 200: raise PermissionException(f"PermissionException: {method}") if result_json['error']['code'] == 200: raise PermissionException(f"PermissionException: {method}")

View file

@ -1,5 +1,5 @@
import eventlet import requests
requests = eventlet.import_patched('requests') from time import sleep
import json import json
from deezer.utils import map_artist_album, map_user_track, map_user_artist, map_user_album, map_user_playlist from deezer.utils import map_artist_album, map_user_track, map_user_artist, map_user_album, map_user_playlist
@ -72,7 +72,7 @@ class GW:
) )
result_json = result.json() result_json = result.json()
except: except:
eventlet.sleep(2) sleep(2)
return self.api_call(method, args, params) return self.api_call(method, args, params)
if len(result_json['error']): if len(result_json['error']):
raise APIError(json.dumps(result_json['error'])) raise APIError(json.dumps(result_json['error']))

View file

@ -1,2 +1 @@
requests requests
eventlet

View file

@ -26,5 +26,5 @@ setup(
python_requires='>=3.6', python_requires='>=3.6',
packages=find_packages(exclude=("tests",)), packages=find_packages(exclude=("tests",)),
include_package_data=True, include_package_data=True,
install_requires=["requests", "eventlet"], install_requires=["requests"],
) )