Simplify setup with CLI tool to create headers_auth file
This commit is contained in:
parent
5d237ddd3c
commit
c038f7dbd5
5 changed files with 70 additions and 9 deletions
|
|
@ -1,22 +1,29 @@
|
||||||
Setup
|
Setup
|
||||||
=====
|
=====
|
||||||
|
|
||||||
|
Installation
|
||||||
|
------------
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
pip install ytmusicapi
|
pip install ytmusicapi
|
||||||
|
|
||||||
**Authenticated requests**
|
Authenticated requests
|
||||||
|
----------------------
|
||||||
To run authenticated requests you need to copy your request headers from a POST request in your YTMusic Web Client.
|
|
||||||
|
|
||||||
|
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:
|
To do so, follow these steps:
|
||||||
|
|
||||||
- Copy ``headers_auth.json.example`` (see below) to ``headers_auth.json``
|
- Open https://music.youtube.com in Firefox
|
||||||
- Open YTMusic 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.
|
||||||
- Go to the developer tools (Ctrl-Shift-I) and right click a POST request.
|
|
||||||
- Copy the request headers (right click > copy > copy request headers)
|
- 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
|
.. literalinclude:: ../../headers_auth.json.example
|
||||||
:language: JSON
|
:language: JSON
|
||||||
|
|
@ -13,7 +13,7 @@ Usage
|
||||||
If an endpoint requires authentication you will receive an error:
|
If an endpoint requires authentication you will receive an error:
|
||||||
``Please provide authentication before using this function``
|
``Please provide authentication before using this function``
|
||||||
|
|
||||||
For authenticated requests you need to set up your credentials first: `Setup <setup>`_
|
For authenticated requests you need to set up your credentials first: :doc:`Setup <setup>`
|
||||||
|
|
||||||
**Authenticated** request to interact with your playlist:
|
**Authenticated** request to interact with your playlist:
|
||||||
|
|
||||||
|
|
|
||||||
42
ytmusicapi/setup.py
Normal file
42
ytmusicapi/setup.py
Normal file
|
|
@ -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
|
||||||
|
|
@ -6,6 +6,9 @@ youtube_auth = YTMusic('../headers_auth.json')
|
||||||
|
|
||||||
|
|
||||||
class TestYTMusic(unittest.TestCase):
|
class TestYTMusic(unittest.TestCase):
|
||||||
|
def test_setup(self):
|
||||||
|
YTMusic.setup()
|
||||||
|
|
||||||
def test_get_playlists(self):
|
def test_get_playlists(self):
|
||||||
playlists = youtube_auth.get_playlists()
|
playlists = youtube_auth.get_playlists()
|
||||||
self.assertGreater(len(playlists), 0)
|
self.assertGreater(len(playlists), 0)
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import ntpath
|
||||||
import os
|
import os
|
||||||
from ytmusicapi.helpers import \
|
from ytmusicapi.helpers import \
|
||||||
parse_playlist_items, parse_uploaded_items, parse_search_result, html_to_txt
|
parse_playlist_items, parse_uploaded_items, parse_search_result, html_to_txt
|
||||||
|
from ytmusicapi.setup import setup
|
||||||
|
|
||||||
params = '?alt=json&key=AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30'
|
params = '?alt=json&key=AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30'
|
||||||
base_url = 'https://music.youtube.com/youtubei/v1/'
|
base_url = 'https://music.youtube.com/youtubei/v1/'
|
||||||
|
|
@ -42,6 +43,14 @@ class YTMusic:
|
||||||
if self.auth == "":
|
if self.auth == "":
|
||||||
raise Exception("Please provide authentication before using this function")
|
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):
|
def search(self, query, filter=None):
|
||||||
"""
|
"""
|
||||||
Search YouTube music
|
Search YouTube music
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue