Oauth refresh on request (#403)
* updating token on every request if auth is oauth * remove print line * headers only updated if auth is oauth * some refactoring * dont know what happened here * check for self.auth --------- Co-authored-by: simon <simon.sporrong@24hr.se>
This commit is contained in:
parent
94f1513822
commit
ef312d75ed
2 changed files with 19 additions and 7 deletions
|
|
@ -23,15 +23,13 @@ def load_headers_file(auth: str) -> Dict:
|
||||||
def prepare_headers(
|
def prepare_headers(
|
||||||
session: requests.Session,
|
session: requests.Session,
|
||||||
proxies: Optional[Dict] = None,
|
proxies: Optional[Dict] = None,
|
||||||
auth: Optional[str] = None,
|
input_dict: Optional[CaseInsensitiveDict] = None,
|
||||||
) -> Dict:
|
) -> Dict:
|
||||||
if auth:
|
if input_dict:
|
||||||
input_json = load_headers_file(auth)
|
|
||||||
input_dict = CaseInsensitiveDict(input_json)
|
|
||||||
|
|
||||||
if is_oauth(input_dict):
|
if is_oauth(input_dict):
|
||||||
oauth = YTMusicOAuth(session, proxies)
|
oauth = YTMusicOAuth(session, proxies)
|
||||||
headers = oauth.load_headers(dict(input_dict), auth)
|
headers = oauth.load_headers(dict(input_dict), input_dict['filepath'])
|
||||||
|
|
||||||
elif is_browser(input_dict):
|
elif is_browser(input_dict):
|
||||||
headers = input_dict
|
headers = input_dict
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,8 @@ from functools import partial
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
|
||||||
from ytmusicapi.auth.headers import prepare_headers
|
from requests.structures import CaseInsensitiveDict
|
||||||
|
from ytmusicapi.auth.headers import load_headers_file, prepare_headers
|
||||||
from ytmusicapi.parsers.i18n import Parser
|
from ytmusicapi.parsers.i18n import Parser
|
||||||
from ytmusicapi.helpers import *
|
from ytmusicapi.helpers import *
|
||||||
from ytmusicapi.mixins.browsing import BrowsingMixin
|
from ytmusicapi.mixins.browsing import BrowsingMixin
|
||||||
|
|
@ -15,6 +16,7 @@ from ytmusicapi.mixins.explore import ExploreMixin
|
||||||
from ytmusicapi.mixins.library import LibraryMixin
|
from ytmusicapi.mixins.library import LibraryMixin
|
||||||
from ytmusicapi.mixins.playlists import PlaylistsMixin
|
from ytmusicapi.mixins.playlists import PlaylistsMixin
|
||||||
from ytmusicapi.mixins.uploads import UploadsMixin
|
from ytmusicapi.mixins.uploads import UploadsMixin
|
||||||
|
from ytmusicapi.auth.oauth import YTMusicOAuth, is_oauth
|
||||||
|
|
||||||
|
|
||||||
class YTMusic(BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin, PlaylistsMixin,
|
class YTMusic(BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin, PlaylistsMixin,
|
||||||
|
|
@ -68,6 +70,8 @@ class YTMusic(BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin
|
||||||
Available languages can be checked in the FAQ.
|
Available languages can be checked in the FAQ.
|
||||||
"""
|
"""
|
||||||
self.auth = auth
|
self.auth = auth
|
||||||
|
self.input_dict = None
|
||||||
|
self.is_oauth_auth = False
|
||||||
|
|
||||||
if isinstance(requests_session, requests.Session):
|
if isinstance(requests_session, requests.Session):
|
||||||
self._session = requests_session
|
self._session = requests_session
|
||||||
|
|
@ -80,8 +84,13 @@ class YTMusic(BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin
|
||||||
|
|
||||||
self.proxies = proxies
|
self.proxies = proxies
|
||||||
self.cookies = {'CONSENT': 'YES+1'}
|
self.cookies = {'CONSENT': 'YES+1'}
|
||||||
|
if self.auth is not None:
|
||||||
|
input_json = load_headers_file(self.auth)
|
||||||
|
self.input_dict = CaseInsensitiveDict(input_json)
|
||||||
|
self.input_dict['filepath'] = self.auth
|
||||||
|
self.is_oauth_auth = is_oauth(self.input_dict)
|
||||||
|
|
||||||
self.headers = prepare_headers(self._session, proxies, auth)
|
self.headers = prepare_headers(self._session, proxies, self.input_dict)
|
||||||
|
|
||||||
if 'x-goog-visitor-id' not in self.headers:
|
if 'x-goog-visitor-id' not in self.headers:
|
||||||
self.headers.update(get_visitor_id(self._send_get_request))
|
self.headers.update(get_visitor_id(self._send_get_request))
|
||||||
|
|
@ -121,7 +130,12 @@ class YTMusic(BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise Exception("Your cookie is missing the required value __Secure-3PAPISID")
|
raise Exception("Your cookie is missing the required value __Secure-3PAPISID")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _send_request(self, endpoint: str, body: Dict, additionalParams: str = "") -> Dict:
|
def _send_request(self, endpoint: str, body: Dict, additionalParams: str = "") -> Dict:
|
||||||
|
|
||||||
|
if self.is_oauth_auth:
|
||||||
|
self.headers = prepare_headers(self._session, self.proxies, self.input_dict)
|
||||||
body.update(self.context)
|
body.update(self.context)
|
||||||
params = YTM_PARAMS
|
params = YTM_PARAMS
|
||||||
if self.is_browser_auth:
|
if self.is_browser_auth:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue