From 3f186d4e0034051f984a676e2eb122efdd587165 Mon Sep 17 00:00:00 2001 From: sigma67 Date: Tue, 19 May 2020 15:49:55 +0200 Subject: [PATCH] Add option to pass raw headers to setup() (in place of terminal input) Based on this change, coverage now runs without user input --- tests/test.cfg.example | 1 + tests/test.py | 2 +- ytmusicapi/setup.py | 22 ++++++++++++---------- ytmusicapi/ytmusic.py | 16 ++++++---------- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/tests/test.cfg.example b/tests/test.cfg.example index ca6d3da..3f7fcce 100644 --- a/tests/test.cfg.example +++ b/tests/test.cfg.example @@ -2,6 +2,7 @@ brand_account = 101234229123420379537 headers = headers_auth_json_as_string headers_file = ./headers_auth.json +headers_raw = raw_headers_pasted_from_browser [playlists] own = owned_playlist_id diff --git a/tests/test.py b/tests/test.py index d33b499..5495e38 100644 --- a/tests/test.py +++ b/tests/test.py @@ -15,7 +15,7 @@ class TestYTMusic(unittest.TestCase): self.assertRaises(Exception, YTMusic, "{}") def test_setup(self): - YTMusic.setup(config['auth']['headers_file']) + YTMusic.setup(config['auth']['headers_file'], config['auth']['headers_raw']) ############### # BROWSING diff --git a/ytmusicapi/setup.py b/ytmusicapi/setup.py index d658504..e8ab731 100644 --- a/ytmusicapi/setup.py +++ b/ytmusicapi/setup.py @@ -6,17 +6,19 @@ import platform path = os.path.dirname(os.path.realpath(__file__)) + os.sep -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:") - +def setup(filepath=None, headers_raw=None): contents = [] - while True: - try: - line = input() - except EOFError: - break - contents.append(line) + if not headers_raw: + 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:") + while True: + try: + line = input() + except EOFError: + break + contents.append(line) + else: + contents = input.split('\n') required_headers = ["Cookie"] try: diff --git a/ytmusicapi/ytmusic.py b/ytmusicapi/ytmusic.py index b036099..2b6d9cf 100644 --- a/ytmusicapi/ytmusic.py +++ b/ytmusicapi/ytmusic.py @@ -78,15 +78,17 @@ class YTMusic: raise Exception("Please provide authentication before using this function") @classmethod - def setup(cls, filepath: str = None): + def setup(cls, filepath: str = None, headers_raw: str = None): """ 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. + :param headers_raw: Optional request headers copied from browser. + Otherwise requested from terminal :return: configuration headers string """ - return setup(filepath) + return setup(filepath, headers_raw) ############### # BROWSING @@ -807,10 +809,7 @@ class YTMusic: body = {'playlistId': playlistId} actions = [] if title: - actions.append({ - 'action': 'ACTION_SET_PLAYLIST_NAME', - 'playlistName': title - }) + actions.append({'action': 'ACTION_SET_PLAYLIST_NAME', 'playlistName': title}) if description: actions.append({ @@ -832,10 +831,7 @@ class YTMusic: }) if addPlaylistId: - actions.append({ - 'action': 'ACTION_ADD_PLAYLIST', - 'addedFullListId': addPlaylistId} - ) + actions.append({'action': 'ACTION_ADD_PLAYLIST', 'addedFullListId': addPlaylistId}) body['actions'] = actions endpoint = 'browse/edit_playlist'