add brand account setup script (#520)

This commit is contained in:
sigma67 2024-02-04 15:21:17 +01:00 • committed by sigma67
commit 170eea358d
15 changed files with 309 additions and 262 deletions

View file

@ -14,16 +14,25 @@ These two files can be easily obtained as the default outputs of running the fol
Copy ``tests/test.cfg.example`` to ``tests/test.cfg`` to run the tests. The entry descriptions should be self-explanatory.
First, run
.. code-block:: bash
cp tests/test.example.cfg tests/test.cfg
The entry descriptions should be self-explanatory.
For the headers_raw, you need to indent the overflowing lines with a tab character. For the upload test you need a suitable music file in the test directory.
Adjust the file to contain appropriate information for your YouTube account and local setup.
Brand accounts can be created by first signing into the google account you wish to have as the parent/controlling
account then navigating `here. <https://www.youtube.com/create_channel?action_create_new_channel_redirect=true>`_
Once the brand account/channel has been created, you can obtain the account ID needed for your test.cfg by
navigating to your `google account page <https://myaccount.google.com>`_ and selecting the brand account via the
profile drop down in the top right, the brand ID should then be present in the URL.
You can populate the brand account with content using the script provided in ``tests/setup/setup_account.py``.
Coverage badge
--------------
Make sure you installed the dev requirements as explained in `CONTRIBUTING.rst <https://github.com/sigma67/ytmusicapi/blob/master/CONTRIBUTING.rst>`_. Run

View file

@ -11,13 +11,17 @@ def get_resource(file: str) -> str:
return data_dir.joinpath(file).as_posix()
@pytest.fixture(name="config")
def fixture_config() -> configparser.RawConfigParser:
def get_config() -> configparser.RawConfigParser:
config = configparser.RawConfigParser()
config.read(get_resource("test.cfg"), "utf-8")
return config
@pytest.fixture(name="config")
def fixture_config() -> configparser.RawConfigParser:
return get_config()
@pytest.fixture(name="sample_album")
def fixture_sample_album() -> str:
"""Eminem - Revival"""

View file

@ -89,8 +89,10 @@ class TestBrowsing:
def test_get_album_other_versions(self, yt):
# Eminem - Curtain Call: The Hits (Explicit Variant)
album = yt.get_album("MPREb_LQCAymzbaKJ")
assert len(variants := album["other_versions"]) >= 1 # appears to be regional
assert (variant := variants[0])["type"] == "Album"
variants = album["other_versions"]
assert len(variants) >= 1 # appears to be regional
variant = variants[0]
assert variant["type"] == "Album"
assert len(variant["artists"]) == 1
assert variant["artists"][0] == {"name": "Eminem", "id": "UCedvOgsKFzcK3hA5taf3KoQ"}
assert variant["audioPlaylistId"] is not None
@ -99,7 +101,8 @@ class TestBrowsing:
# Cassö & RAYE - Prada
album = yt.get_album("MPREb_of3qfisa0yU")
assert not album["isExplicit"]
assert (variant := album["other_versions"][0])["type"] == "Single"
variant = album["other_versions"][0]
assert variant["type"] == "Single"
assert variant["isExplicit"]
assert len(variant["artists"]) == 3
assert variant["artists"][0]["id"] == "UCGWMNnI1Ky5bMcRlr73Cj2Q"

View file

@ -1,7 +1,6 @@
class TestPodcasts:
def test_get_podcast(self, yt, yt_brand):
podcast_id = "PLxq_lXOUlvQDgCVFj9L79kqJybW0k6OaB" # Think Fast, Talk Smart: The Podcast
def test_get_podcast(self, config, yt, yt_brand):
podcast_id = config["podcasts"]["podcast_id"]
results = yt.get_podcast(podcast_id)
assert len(results["episodes"]) == 100
assert not results["saved"]
@ -16,14 +15,14 @@ class TestPodcasts:
results = yt.get_podcast(result["browseId"])
assert len(results) > 0
def test_get_episode(self, yt, yt_brand):
podcast_id = "KNkyHCLOr1o" # 124. Making Meetings Meaningful, Pt. 1: How to Structure
result = yt.get_episode(podcast_id)
def test_get_episode(self, config, yt, yt_brand):
episode_id = config["podcasts"]["episode_id"]
result = yt.get_episode(episode_id)
assert len(result["description"]) == 50
assert not result["saved"]
assert result["playlistId"] is not None
result = yt_brand.get_episode(podcast_id)
result = yt_brand.get_episode(episode_id)
assert result["saved"]
def test_many_episodes(self, yt):

View file

@ -0,0 +1,56 @@
import itertools
from contextlib import suppress
from pathlib import Path
from conftest import get_config
from ytmusicapi import YTMusic
config = get_config()
# To get started, go to https://www.youtube.com/account and create a new channel (=brand account)
brand_account = "114511851949139689139"
# run ytmusicapi browser in tests/setup, paste headers from your test account
yt_brand = YTMusic(Path(__file__).parent.joinpath("oauth.json").as_posix(), brand_account)
def populate_account():
"""idempotent requests to populate an account"""
# library
playlist_id = "RDCLAK5uy_l9ex2d91-Qb1i-W7d0MLCEl_ZjRXss0Dk" # fixed playlist with many artists
yt_playlist = yt_brand.get_playlist(playlist_id)
artists = [track["artists"] for track in yt_playlist["tracks"]]
unique_artists = list(set(artist["id"] for artist in itertools.chain.from_iterable(artists)))
with suppress(Exception):
for artist in unique_artists:
print(f"Adding artist {artist}")
yt_brand.subscribe_artists([artist]) # add one by one to avoid "requested entity not found"
# add some albums, which also populates songs and artists as a side effect
unique_albums = set(track["album"]["id"] for track in yt_playlist["tracks"])
playlist_ids = [yt_brand.get_album(album)["audioPlaylistId"] for album in unique_albums if album]
for playlist_id in playlist_ids:
print(f"Adding album {playlist_id}")
yt_brand.rate_playlist(playlist_id, "LIKE")
# like some songs
for track in yt_playlist["tracks"]:
print(f"liking track {track['videoId']}")
yt_brand.rate_song(track["videoId"], "LIKE")
# create own playlist
playlistId = yt_brand.create_playlist(
title="ytmusicapi test playlist don't delete",
description="description",
source_playlist="PLJZsotfVeN2D3pHlgWT_FFSYsJe_thVmh",
)
print(f"Created playlist {playlistId}, don't forget to set this in test.cfg playlists/own")
# podcasts
yt_brand.rate_playlist(config["podcasts"]["podcast_id"], rating="LIKE")
yt_brand.add_playlist_items("SE", [config["podcasts"]["episode_id"]])
if __name__ == "__main__":
populate_account()

View file

@ -1,3 +0,0 @@
from conftest import fixture_config
config = fixture_config()

View file

@ -1,38 +0,0 @@
[auth]
brand_account = 101234229123420379537
brand_account_empty = 1123456629123420379537
headers = headers_auth_json_as_string
headers_empty = headers_account_with_empty_library_as_json_as_string
browser_file = ./browser.json
oauth_file = ./oauth.json
headers_raw = raw_headers_pasted_from_browser
[queries]
uploads_songs = query_gives_gt_20_songs
library_any = query_gives_gt_5_results
library_songs = query_gives_gt_10_songs
library_albums = query_gives_gte_5_albums
library_artists = query_gives_gte_1_artist
library_playlists = query_gives_gte_1_playlist
[playlists]
own = owned_playlist_id
own_length = number_of_tracks_in_playlist
[albums]
album_browse_id = sample_id_of_regionally_available_album
album_track_length = available_album_length
[uploads]
file = song_in_tests_directory.mp3
private_album_id = sample_id_of_private_album
private_artist_id = sample_id_of_private_artist
private_upload_id = sample_video_id_of_private_upload
[limits]
library_playlists = 100
library_songs = 200
library_artists = 300
library_subscriptions = 50
library_upload_albums = 200
library_upload_artists = 250