Breaking: setup() now requires a filepath parameter, returns credential string
This change enables providing a string instead of a file for authentication
This commit is contained in:
parent
4f718e6730
commit
dc4f5dad85
2 changed files with 27 additions and 15 deletions
|
|
@ -6,7 +6,7 @@ import platform
|
||||||
path = os.path.dirname(os.path.realpath(__file__)) + os.sep
|
path = os.path.dirname(os.path.realpath(__file__)) + os.sep
|
||||||
|
|
||||||
|
|
||||||
def setup():
|
def setup(filepath=None):
|
||||||
eof = "Ctrl-D" if platform.system() != "Windows" else "'Enter, Ctrl-Z, Enter'"
|
eof = "Ctrl-D" if platform.system() != "Windows" else "'Enter, Ctrl-Z, Enter'"
|
||||||
print("Please paste the request headers from Firefox and press " + eof + " to continue:")
|
print("Please paste the request headers from Firefox and press " + eof + " to continue:")
|
||||||
|
|
||||||
|
|
@ -39,7 +39,8 @@ def setup():
|
||||||
default_headers = json.load(json_file)
|
default_headers = json.load(json_file)
|
||||||
|
|
||||||
headers.update(default_headers)
|
headers.update(default_headers)
|
||||||
with open(path + 'headers_auth.json', 'w') as file:
|
if filepath is not None:
|
||||||
json.dump(headers, file, ensure_ascii=True, indent=4, sort_keys=True)
|
with open(filepath, 'w') as file:
|
||||||
|
json.dump(headers, file, ensure_ascii=True, indent=4, sort_keys=True)
|
||||||
|
|
||||||
return
|
return json.dumps(headers)
|
||||||
|
|
|
||||||
|
|
@ -17,21 +17,29 @@ class YTMusic:
|
||||||
Permits both authenticated and non-authenticated requests.
|
Permits both authenticated and non-authenticated requests.
|
||||||
Authentication header data must be provided on initialization.
|
Authentication header data must be provided on initialization.
|
||||||
"""
|
"""
|
||||||
def __init__(self, auth=""):
|
def __init__(self, auth=None):
|
||||||
"""
|
"""
|
||||||
Create a new instance to interact with YouTube Music.
|
Create a new instance to interact with YouTube Music.
|
||||||
|
|
||||||
:param auth: Optional. Provide authentication credentials to manage your library.
|
:param auth: Optional. Provide a string or path to file.
|
||||||
|
Authentication credentials are needed to manage your library.
|
||||||
Should be an adjusted version of `headers_auth.json.example` in the project root.
|
Should be an adjusted version of `headers_auth.json.example` in the project root.
|
||||||
|
See :py:func:`setup` for how to fill in the correct credentials.
|
||||||
Default: A default header is used without authentication.
|
Default: A default header is used without authentication.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.auth = auth
|
self.auth = auth
|
||||||
|
|
||||||
file = auth if auth else pkg_resources.resource_filename('ytmusicapi', 'headers.json')
|
try:
|
||||||
|
if auth is None or os.path.isfile(auth):
|
||||||
with open(file) as json_file:
|
file = auth if auth else pkg_resources.resource_filename('ytmusicapi', 'headers.json')
|
||||||
self.headers = json.load(json_file)
|
with open(file) as json_file:
|
||||||
|
self.headers = json.load(json_file)
|
||||||
|
else:
|
||||||
|
self.headers = json.loads(auth)
|
||||||
|
except Exception as e:
|
||||||
|
print("Failed loading provided credentials. Make sure to provide a string or a file path. "
|
||||||
|
"Reason: " + str(e))
|
||||||
|
|
||||||
with open(pkg_resources.resource_filename('ytmusicapi', 'context.json')) as json_file:
|
with open(pkg_resources.resource_filename('ytmusicapi', 'context.json')) as json_file:
|
||||||
self.context = json.load(json_file)
|
self.context = json.load(json_file)
|
||||||
|
|
@ -52,12 +60,15 @@ class YTMusic:
|
||||||
raise Exception("Please provide authentication before using this function")
|
raise Exception("Please provide authentication before using this function")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setup(self):
|
def setup(cls, filepath=None):
|
||||||
"""
|
"""
|
||||||
Requests browser headers from the user and stores a configuration
|
Requests browser headers from the user via command line
|
||||||
JSON file in the correct format in the current directory
|
and returns a string that can be passed to YTMusic()
|
||||||
|
|
||||||
|
:param filepath: Optional filepath to store headers to.
|
||||||
|
:return: configuration headers string
|
||||||
"""
|
"""
|
||||||
setup()
|
return setup(filepath)
|
||||||
|
|
||||||
def search(self, query, filter=None):
|
def search(self, query, filter=None):
|
||||||
"""
|
"""
|
||||||
|
|
@ -384,7 +395,7 @@ class YTMusic:
|
||||||
if not videos[0]['setVideoId']:
|
if not videos[0]['setVideoId']:
|
||||||
print("Cannot remove songs, because setVideoId is missing. Do you own this playlist?")
|
print("Cannot remove songs, because setVideoId is missing. Do you own this playlist?")
|
||||||
return
|
return
|
||||||
|
|
||||||
body = {'playlistId': playlistId, 'actions': []}
|
body = {'playlistId': playlistId, 'actions': []}
|
||||||
for video in videos:
|
for video in videos:
|
||||||
body['actions'].append({
|
body['actions'].append({
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue