From dc4f5dad85fdb9fc83ee1f5b926d4dd7a28173fc Mon Sep 17 00:00:00 2001 From: sigma67 Date: Sun, 12 Apr 2020 23:27:47 +0200 Subject: [PATCH] Breaking: setup() now requires a filepath parameter, returns credential string This change enables providing a string instead of a file for authentication --- ytmusicapi/setup.py | 9 +++++---- ytmusicapi/ytmusic.py | 33 ++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/ytmusicapi/setup.py b/ytmusicapi/setup.py index b7f316e..2ef962c 100644 --- a/ytmusicapi/setup.py +++ b/ytmusicapi/setup.py @@ -6,7 +6,7 @@ import platform 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'" 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) headers.update(default_headers) - with open(path + 'headers_auth.json', 'w') as file: - json.dump(headers, file, ensure_ascii=True, indent=4, sort_keys=True) + if filepath is not None: + with open(filepath, 'w') as file: + json.dump(headers, file, ensure_ascii=True, indent=4, sort_keys=True) - return \ No newline at end of file + return json.dumps(headers) diff --git a/ytmusicapi/ytmusic.py b/ytmusicapi/ytmusic.py index e646403..84b448b 100644 --- a/ytmusicapi/ytmusic.py +++ b/ytmusicapi/ytmusic.py @@ -17,21 +17,29 @@ class YTMusic: Permits both authenticated and non-authenticated requests. 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. - :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. + See :py:func:`setup` for how to fill in the correct credentials. Default: A default header is used without authentication. """ self.auth = auth - file = auth if auth else pkg_resources.resource_filename('ytmusicapi', 'headers.json') - - with open(file) as json_file: - self.headers = json.load(json_file) + try: + if auth is None or os.path.isfile(auth): + file = auth if auth else pkg_resources.resource_filename('ytmusicapi', 'headers.json') + 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: self.context = json.load(json_file) @@ -52,12 +60,15 @@ class YTMusic: raise Exception("Please provide authentication before using this function") @classmethod - def setup(self): + def setup(cls, filepath=None): """ - Requests browser headers from the user and stores a configuration - JSON file in the correct format in the current directory + Requests browser headers from the user via command line + 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): """ @@ -384,7 +395,7 @@ class YTMusic: if not videos[0]['setVideoId']: print("Cannot remove songs, because setVideoId is missing. Do you own this playlist?") return - + body = {'playlistId': playlistId, 'actions': []} for video in videos: body['actions'].append({