This commit is contained in:
sigma67 2023-04-05 23:23:05 +02:00 • committed by sigma67
commit 012a0fc85d
5 changed files with 32 additions and 11 deletions

View file

@ -16,6 +16,9 @@ dynamic = ["version", "readme"]
[project.optional-dependencies]
dev = ['pre-commit', 'flake8', 'yapf', 'coverage', 'sphinx', 'sphinx-rtd-theme']
[project.scripts]
ytmusicapi = "ytmusicapi.setup:main"
[project.urls]
homepage = "https://github.com/sigma67/ytmusicapi"
documentation = "https://ytmusicapi.readthedocs.io"

View file

@ -6,7 +6,7 @@ from pathlib import Path
from requests import Response
from unittest import mock
from ytmusicapi.setup import setup_oauth, setup # noqa: E402
from ytmusicapi.setup import main, setup # noqa: E402
from ytmusicapi.ytmusic import YTMusic # noqa: E402
@ -31,7 +31,7 @@ class TestYTMusic(unittest.TestCase):
@classmethod
def setUpClass(cls):
with YTMusic(requests_session=False) as yt:
pass
assert isinstance(yt, YTMusic)
cls.yt = YTMusic()
cls.yt_oauth = YTMusic(headers_oauth)
cls.yt_auth = YTMusic(headers_browser)
@ -39,17 +39,19 @@ class TestYTMusic(unittest.TestCase):
cls.yt_empty = YTMusic(config['auth']['headers_empty'],
config['auth']['brand_account_empty'])
@mock.patch('sys.argv', ["ytmusicapi", "browser", "--file", headers_browser])
def test_setup_browser(self):
headers = setup(headers_browser, config['auth']['headers_raw'])
self.assertGreaterEqual(len(headers), 2)
headers_raw = config['auth']['headers_raw'].split('\n')
with mock.patch('builtins.input', side_effect=(headers_raw + [EOFError()])):
headers = setup(headers_browser)
headers = main()
self.assertGreaterEqual(len(headers), 2)
# @unittest.skip("Cannot test oauth flow non-interactively")
@mock.patch('requests.Response.json')
@mock.patch('requests.Session.post')
@mock.patch('sys.argv', ["ytmusicapi", "oauth", "--file", headers_oauth])
def test_setup_oauth(self, session_mock, json_mock):
session_mock.return_value = Response()
json_mock.side_effect = [
@ -57,8 +59,7 @@ class TestYTMusic(unittest.TestCase):
json.loads(config['auth']['oauth_token'])
]
with mock.patch('builtins.input', return_value='y'):
headers = setup_oauth(headers_oauth)
self.assertEqual(len(headers), 6)
main()
self.assertTrue(Path(headers_oauth).exists())
###############
@ -477,7 +478,7 @@ class TestYTMusic(unittest.TestCase):
def test_get_library_upload_artist(self):
tracks = self.yt_oauth.get_library_upload_artist(config['uploads']['private_artist_id'],
100)
100)
self.assertGreater(len(tracks), 0)

View file

@ -8,7 +8,7 @@ except PackageNotFoundError:
# package is not installed
pass
__copyright__ = 'Copyright 2022 sigma67'
__copyright__ = 'Copyright 2023 sigma67'
__license__ = 'MIT'
__title__ = 'ytmusicapi'
__all__ = ["YTMusic", "setup_oauth", "setup"]

View file

@ -789,12 +789,12 @@ class BrowsingMixin:
def set_tasteprofile(self, artists: List[str], taste_profile: Dict = None) -> None:
"""
Favorites artists to see more recommendations from the artist.
Use get_tasteprofile() to see which artists are available to be recommended
Use :py:func:`get_tasteprofile` to see which artists are available to be recommended
:param artists: A List with names of artists, must be contained in the tasteprofile
:param taste_profile: tasteprofile result from :py:func:`get_tasteprofile`.
Pass this if you call :py:func:`get_tasteprofile` anyway to save an extra request.
:return None if successful
:return: None if successful
"""
if taste_profile is None:

View file

@ -1,3 +1,6 @@
import argparse
import sys
from pathlib import Path
from typing import Dict
import requests
@ -37,5 +40,19 @@ def setup_oauth(filepath: str = None,
return YTMusicOAuth(session, proxies).setup(filepath)
if __name__ == "__main__":
setup_oauth()
def parse_args(args):
parser = argparse.ArgumentParser(description='Setup ytmusicapi.')
parser.add_argument("setup_type",
type=str,
choices=["oauth", "browser"],
help="choose a setup type.")
parser.add_argument("--file", type=Path, help="optional path to output file.")
return parser.parse_args(args)
def main():
args = parse_args(sys.argv[1:])
filename = args.file if args.file else f"{args.setup_type}.json"
print(f"Creating {filename} with your authentication credentials...")
func = setup_oauth if args.setup_type == "oauth" else setup
return func(filename)