From c038f7dbd592a1c1612ae9efca899657bb46a857 Mon Sep 17 00:00:00 2001 From: sigma67 Date: Mon, 23 Mar 2020 19:51:23 +0100 Subject: [PATCH] Simplify setup with CLI tool to create headers_auth file --- docs/source/setup.rst | 23 +++++++++++++++-------- docs/source/usage.rst | 2 +- ytmusicapi/setup.py | 42 ++++++++++++++++++++++++++++++++++++++++++ ytmusicapi/test.py | 3 +++ ytmusicapi/ytmusic.py | 9 +++++++++ 5 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 ytmusicapi/setup.py diff --git a/docs/source/setup.rst b/docs/source/setup.rst index 8221714..558479f 100644 --- a/docs/source/setup.rst +++ b/docs/source/setup.rst @@ -1,22 +1,29 @@ Setup ===== + +Installation +------------ .. code-block:: bash pip install ytmusicapi -**Authenticated requests** - -To run authenticated requests you need to copy your request headers from a POST request in your YTMusic Web Client. +Authenticated requests +---------------------- +To run authenticated requests you need to set up you need to copy your request headers from a POST request in your YTMusic Web Client. To do so, follow these steps: - - Copy ``headers_auth.json.example`` (see below) to ``headers_auth.json`` - - Open YTMusic in Firefox - - Go to the developer tools (Ctrl-Shift-I) and right click a POST request. + - Open https://music.youtube.com in Firefox + - Go to the developer tools (Ctrl-Shift-I) and find an authenticated POST request. You can filter for /browse to easily find a suitable request. - Copy the request headers (right click > copy > copy request headers) - - Paste the three missing items to `headers_auth.json` -**headers_auth.json.example** +Now call :py:func:`YTMusic.setup()` and paste the request headers and it will create configuration file +in the correct format in the current directory. + +Manual file creation +-------------------- + +Alternatively, you can paste the three missing items to `headers_auth.json` below and create your own file: .. literalinclude:: ../../headers_auth.json.example :language: JSON \ No newline at end of file diff --git a/docs/source/usage.rst b/docs/source/usage.rst index b17dd55..12d9527 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -13,7 +13,7 @@ Usage If an endpoint requires authentication you will receive an error: ``Please provide authentication before using this function`` -For authenticated requests you need to set up your credentials first: `Setup `_ +For authenticated requests you need to set up your credentials first: :doc:`Setup ` **Authenticated** request to interact with your playlist: diff --git a/ytmusicapi/setup.py b/ytmusicapi/setup.py new file mode 100644 index 0000000..a3fff27 --- /dev/null +++ b/ytmusicapi/setup.py @@ -0,0 +1,42 @@ +import pkg_resources +import json +import os + +path = os.path.dirname(os.path.realpath(__file__)) + os.sep + +def setup(): + print("Please paste the request headers from Firefox and press Ctrl-D continue:") + + contents = [] + while True: + try: + line = input() + except EOFError: + break + contents.append(line) + + required_headers = ["Authorization", "X-Youtube-Identity-Token", "Cookie"] + try: + headers = {} + for content in contents: + header = content.split(': ') + key = header[0] + if key in required_headers: + headers[key] = ': '.join(header[1:]) + + except: + raise Exception("Error parsing your input, please try again.") + + if len(headers) is not 3: + missing = set(required_headers) - set(headers) + raise Exception("The following entries are missing in your headers: " + ", ".join(missing) + + ". Please try a different request (such as /browse) and make sure you are logged in.") + + with open(pkg_resources.resource_filename('ytmusicapi', 'headers.json')) as json_file: + 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) + + return \ No newline at end of file diff --git a/ytmusicapi/test.py b/ytmusicapi/test.py index 53441b9..30e02c2 100644 --- a/ytmusicapi/test.py +++ b/ytmusicapi/test.py @@ -6,6 +6,9 @@ youtube_auth = YTMusic('../headers_auth.json') class TestYTMusic(unittest.TestCase): + def test_setup(self): + YTMusic.setup() + def test_get_playlists(self): playlists = youtube_auth.get_playlists() self.assertGreater(len(playlists), 0) diff --git a/ytmusicapi/ytmusic.py b/ytmusicapi/ytmusic.py index 352bc18..75b04e9 100644 --- a/ytmusicapi/ytmusic.py +++ b/ytmusicapi/ytmusic.py @@ -5,6 +5,7 @@ import ntpath import os from ytmusicapi.helpers import \ parse_playlist_items, parse_uploaded_items, parse_search_result, html_to_txt +from ytmusicapi.setup import setup params = '?alt=json&key=AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30' base_url = 'https://music.youtube.com/youtubei/v1/' @@ -42,6 +43,14 @@ class YTMusic: if self.auth == "": raise Exception("Please provide authentication before using this function") + @classmethod + def setup(self): + """ + Requests browser headers from the user and stores a configuration + JSON file in the correct format in the current directory + """ + setup() + def search(self, query, filter=None): """ Search YouTube music