Skip to content

Commit

Permalink
ruff: ruff specific rules
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma67 committed Jan 18, 2024
1 parent b87c783 commit 9170197
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 61 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ ignore = [ "F403", "F405", "F821", "E731" ]
extend-select = [
"I", # isort
"UP", # pyupgrade
"RUF", # ruff
]

[tool.mypy]
Expand Down
2 changes: 1 addition & 1 deletion tests/auth/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_setup_browser(self, config, browser_filepath: str):
headers_raw = config["auth"]["headers_raw"].split("\n")
with (
mock.patch("sys.argv", ["ytmusicapi", "browser", "--file", browser_filepath]),
mock.patch("builtins.input", side_effect=(headers_raw + [EOFError()])),
mock.patch("builtins.input", side_effect=([*headers_raw, EOFError()])),
):
headers = main()
assert len(headers) >= 2
2 changes: 1 addition & 1 deletion tests/mixins/test_explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class TestExplore:
def test_get_mood_playlists(self, yt):
categories = yt.get_mood_categories()
assert len(list(categories)) > 0
cat = list(categories)[0]
cat = next(iter(categories))
assert len(categories[cat]) > 0
playlists = yt.get_mood_playlists(categories[cat][0]["params"])
assert len(playlists) > 0
Expand Down
20 changes: 10 additions & 10 deletions ytmusicapi/mixins/browsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def get_home(self, limit=3) -> List[Dict]:
home = []
home.extend(parse_mixed_content(results))

section_list = nav(response, SINGLE_COLUMN_TAB + ["sectionListRenderer"])
section_list = nav(response, [*SINGLE_COLUMN_TAB, "sectionListRenderer"])
if "continuations" in section_list:
request_func = lambda additionalParams: self._send_request(endpoint, body, additionalParams)

Expand Down Expand Up @@ -237,10 +237,10 @@ def get_artist(self, channelId: str) -> Dict:
subscription_button = header["subscriptionButton"]["subscribeButtonRenderer"]
artist["channelId"] = subscription_button["channelId"]
artist["shuffleId"] = nav(
header, ["playButton", "buttonRenderer"] + NAVIGATION_WATCH_PLAYLIST_ID, True
header, ["playButton", "buttonRenderer", *NAVIGATION_WATCH_PLAYLIST_ID], True
)
artist["radioId"] = nav(
header, ["startRadioButton", "buttonRenderer"] + NAVIGATION_WATCH_PLAYLIST_ID, True
header, ["startRadioButton", "buttonRenderer", *NAVIGATION_WATCH_PLAYLIST_ID], True
)
artist["subscribers"] = nav(subscription_button, ["subscriberCountText", "runs", 0, "text"], True)
artist["subscribed"] = subscription_button["subscribed"]
Expand Down Expand Up @@ -296,8 +296,8 @@ def get_artist_albums(
(
nav(
option,
MULTI_SELECT
+ [
[
*MULTI_SELECT,
"selectedCommand",
"commandExecutorCommand",
"commands",
Expand Down Expand Up @@ -346,7 +346,7 @@ def get_user(self, channelId: str) -> Dict:
Example::
{
"name": "4Tune No Copyright Music",
"name": "4Tune - No Copyright Music",
"videos": {
"browseId": "UC44hbeRoCZVVMVg5z0FfIww",
"results": [
Expand Down Expand Up @@ -387,7 +387,7 @@ def get_user(self, channelId: str) -> Dict:
endpoint = "browse"
body = {"browseId": channelId}
response = self._send_request(endpoint, body)
user = {"name": nav(response, ["header", "musicVisualHeaderRenderer"] + TITLE_TEXT)}
user = {"name": nav(response, ["header", "musicVisualHeaderRenderer", *TITLE_TEXT])}
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST)
user.update(self.parser.parse_artist_contents(results))
return user
Expand Down Expand Up @@ -769,7 +769,7 @@ def get_song_related(self, browseId: str):
raise Exception("Invalid browseId provided.")

response = self._send_request("browse", {"browseId": browseId})
sections = nav(response, ["contents"] + SECTION_LIST)
sections = nav(response, ["contents", *SECTION_LIST])
return parse_mixed_content(sections)

def get_lyrics(self, browseId: str) -> Dict:
Expand All @@ -793,10 +793,10 @@ def get_lyrics(self, browseId: str) -> Dict:

response = self._send_request("browse", {"browseId": browseId})
lyrics["lyrics"] = nav(
response, ["contents"] + SECTION_LIST_ITEM + DESCRIPTION_SHELF + DESCRIPTION, True
response, ["contents", *SECTION_LIST_ITEM, *DESCRIPTION_SHELF, *DESCRIPTION], True
)
lyrics["source"] = nav(
response, ["contents"] + SECTION_LIST_ITEM + DESCRIPTION_SHELF + ["footer"] + RUN_TEXT, True
response, ["contents", *SECTION_LIST_ITEM, *DESCRIPTION_SHELF, "footer", *RUN_TEXT], True
)

return lyrics
Expand Down
6 changes: 3 additions & 3 deletions ytmusicapi/mixins/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_mood_categories(self) -> Dict:
sections: Dict[str, Any] = {}
response = self._send_request("browse", {"browseId": "FEmusic_moods_and_genres"})
for section in nav(response, SINGLE_COLUMN_TAB + SECTION_LIST):
title = nav(section, GRID + ["header", "gridHeaderRenderer"] + TITLE_TEXT)
title = nav(section, [*GRID, "header", "gridHeaderRenderer", *TITLE_TEXT])
sections[title] = []
for category in nav(section, GRID_ITEMS):
sections[title].append(
Expand Down Expand Up @@ -197,8 +197,8 @@ def get_charts(self, country: str = "ZZ") -> Dict:
charts: Dict[str, Any] = {"countries": {}}
menu = nav(
results[0],
MUSIC_SHELF
+ [
[
*MUSIC_SHELF,
"subheaders",
0,
"musicSideAlignedItemRenderer",
Expand Down
6 changes: 3 additions & 3 deletions ytmusicapi/mixins/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ def get_history(self) -> List[Dict]:
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST)
songs = []
for content in results:
data = nav(content, MUSIC_SHELF + ["contents"], True)
data = nav(content, [*MUSIC_SHELF, "contents"], True)
if not data:
error = nav(content, ["musicNotifierShelfRenderer"] + TITLE, True)
error = nav(content, ["musicNotifierShelfRenderer", *TITLE], True)
raise Exception(error)
menu_entries = [[-1] + MENU_SERVICE + FEEDBACK_TOKEN]
menu_entries = [[-1, *MENU_SERVICE, *FEEDBACK_TOKEN]]
songlist = parse_playlist_items(data, menu_entries)
for song in songlist:
song["played"] = nav(content["musicShelfRenderer"], TITLE_TEXT)
Expand Down
4 changes: 2 additions & 2 deletions ytmusicapi/mixins/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def get_playlist(
if run_count > 1:
playlist["author"] = {
"name": nav(header, SUBTITLE2),
"id": nav(header, SUBTITLE_RUNS + [2] + NAVIGATION_BROWSE_ID, True),
"id": nav(header, [*SUBTITLE_RUNS, 2, *NAVIGATION_BROWSE_ID], True),
}
if run_count == 5:
playlist["year"] = nav(header, SUBTITLE3)
Expand All @@ -149,7 +149,7 @@ def get_playlist(
request_func = lambda additionalParams: self._send_request(endpoint, body, additionalParams)

# suggestions and related are missing e.g. on liked songs
section_list = nav(response, SINGLE_COLUMN_TAB + ["sectionListRenderer"])
section_list = nav(response, [*SINGLE_COLUMN_TAB, "sectionListRenderer"])
playlist["related"] = []
if "continuations" in section_list:
additionalParams = get_continuation_params(section_list)
Expand Down
2 changes: 1 addition & 1 deletion ytmusicapi/mixins/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def search(
category = None
# category "more from youtube" is missing sometimes
if "messageRenderer" in results[0]:
category = nav(results.pop(0), ["messageRenderer"] + TEXT_RUN_TEXT)
category = nav(results.pop(0), ["messageRenderer", *TEXT_RUN_TEXT])
type = None
else:
continue
Expand Down
4 changes: 2 additions & 2 deletions ytmusicapi/mixins/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ def get_watch_playlist(
related_browse_id = get_tab_browse_id(watchNextRenderer, 2)

results = nav(
watchNextRenderer, TAB_CONTENT + ["musicQueueRenderer", "content", "playlistPanelRenderer"]
watchNextRenderer, [*TAB_CONTENT, "musicQueueRenderer", "content", "playlistPanelRenderer"]
)
playlist = next(
filter(
bool,
map(
lambda x: nav(x, ["playlistPanelVideoRenderer"] + NAVIGATION_PLAYLIST_ID, True),
lambda x: nav(x, ["playlistPanelVideoRenderer", *NAVIGATION_PLAYLIST_ID], True),
results["contents"],
),
),
Expand Down
48 changes: 24 additions & 24 deletions ytmusicapi/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
SINGLE_COLUMN = ["contents", "singleColumnBrowseResultsRenderer"]
SINGLE_COLUMN_TAB = SINGLE_COLUMN + TAB_CONTENT
SECTION = ["sectionListRenderer"]
SECTION_LIST = SECTION + ["contents"]
SECTION_LIST = [*SECTION, "contents"]
SECTION_LIST_ITEM = SECTION + CONTENT
ITEM_SECTION = ["itemSectionRenderer"] + CONTENT
ITEM_SECTION = ["itemSectionRenderer", *CONTENT]
MUSIC_SHELF = ["musicShelfRenderer"]
GRID = ["gridRenderer"]
GRID_ITEMS = GRID + ["items"]
GRID_ITEMS = [*GRID, "items"]
MENU = ["menu", "menuRenderer"]
MENU_ITEMS = MENU + ["items"]
MENU_LIKE_STATUS = MENU + ["topLevelButtons", 0, "likeButtonRenderer", "likeStatus"]
MENU_ITEMS = [*MENU, "items"]
MENU_LIKE_STATUS = [*MENU, "topLevelButtons", 0, "likeButtonRenderer", "likeStatus"]
MENU_SERVICE = ["menuServiceItemRenderer", "serviceEndpoint"]
TOGGLE_MENU = "toggleMenuServiceItemRenderer"
PLAY_BUTTON = ["overlay", "musicItemThumbnailOverlayRenderer", "content", "musicPlayButtonRenderer"]
NAVIGATION_BROWSE = ["navigationEndpoint", "browseEndpoint"]
NAVIGATION_BROWSE_ID = NAVIGATION_BROWSE + ["browseId"]
NAVIGATION_BROWSE_ID = [*NAVIGATION_BROWSE, "browseId"]
PAGE_TYPE = ["browseEndpointContextSupportedConfigs", "browseEndpointContextMusicConfig", "pageType"]
WATCH_VIDEO_ID = ["watchEndpoint", "videoId"]
NAVIGATION_VIDEO_ID = ["navigationEndpoint"] + WATCH_VIDEO_ID
NAVIGATION_VIDEO_ID = ["navigationEndpoint", *WATCH_VIDEO_ID]
QUEUE_VIDEO_ID = ["queueAddEndpoint", "queueTarget", "videoId"]
NAVIGATION_PLAYLIST_ID = ["navigationEndpoint", "watchEndpoint", "playlistId"]
NAVIGATION_WATCH_PLAYLIST_ID = ["navigationEndpoint", "watchPlaylistEndpoint", "playlistId"]
Expand All @@ -35,40 +35,40 @@
"musicVideoType",
]
TITLE = ["title", "runs", 0]
TITLE_TEXT = ["title"] + RUN_TEXT
TITLE_TEXT = ["title", *RUN_TEXT]
TEXT_RUNS = ["text", "runs"]
TEXT_RUN = TEXT_RUNS + [0]
TEXT_RUN_TEXT = TEXT_RUN + ["text"]
SUBTITLE = ["subtitle"] + RUN_TEXT
TEXT_RUN = [*TEXT_RUNS, 0]
TEXT_RUN_TEXT = [*TEXT_RUN, "text"]
SUBTITLE = ["subtitle", *RUN_TEXT]
SUBTITLE_RUNS = ["subtitle", "runs"]
SUBTITLE2 = SUBTITLE_RUNS + [2, "text"]
SUBTITLE3 = SUBTITLE_RUNS + [4, "text"]
SUBTITLE2 = [*SUBTITLE_RUNS, 2, "text"]
SUBTITLE3 = [*SUBTITLE_RUNS, 4, "text"]
THUMBNAIL = ["thumbnail", "thumbnails"]
THUMBNAILS = ["thumbnail", "musicThumbnailRenderer"] + THUMBNAIL
THUMBNAIL_RENDERER = ["thumbnailRenderer", "musicThumbnailRenderer"] + THUMBNAIL
THUMBNAIL_CROPPED = ["thumbnail", "croppedSquareThumbnailRenderer"] + THUMBNAIL
THUMBNAILS = ["thumbnail", "musicThumbnailRenderer", *THUMBNAIL]
THUMBNAIL_RENDERER = ["thumbnailRenderer", "musicThumbnailRenderer", *THUMBNAIL]
THUMBNAIL_CROPPED = ["thumbnail", "croppedSquareThumbnailRenderer", *THUMBNAIL]
FEEDBACK_TOKEN = ["feedbackEndpoint", "feedbackToken"]
BADGE_PATH = [0, "musicInlineBadgeRenderer", "accessibilityData", "accessibilityData", "label"]
BADGE_LABEL = ["badges"] + BADGE_PATH
SUBTITLE_BADGE_LABEL = ["subtitleBadges"] + BADGE_PATH
CATEGORY_TITLE = ["musicNavigationButtonRenderer", "buttonText"] + RUN_TEXT
BADGE_LABEL = ["badges", *BADGE_PATH]
SUBTITLE_BADGE_LABEL = ["subtitleBadges", *BADGE_PATH]
CATEGORY_TITLE = ["musicNavigationButtonRenderer", "buttonText", *RUN_TEXT]
CATEGORY_PARAMS = ["musicNavigationButtonRenderer", "clickCommand", "browseEndpoint", "params"]
MRLIR = "musicResponsiveListItemRenderer"
MTRIR = "musicTwoRowItemRenderer"
TASTE_PROFILE_ITEMS = ["contents", "tastebuilderRenderer", "contents"]
TASTE_PROFILE_ARTIST = ["title", "runs"]
SECTION_LIST_CONTINUATION = ["continuationContents", "sectionListContinuation"]
MENU_PLAYLIST_ID = MENU_ITEMS + [0, "menuNavigationItemRenderer"] + NAVIGATION_WATCH_PLAYLIST_ID
MENU_PLAYLIST_ID = [*MENU_ITEMS, 0, "menuNavigationItemRenderer", *NAVIGATION_WATCH_PLAYLIST_ID]
MULTI_SELECT = ["musicMultiSelectMenuItemRenderer"]
HEADER_DETAIL = ["header", "musicDetailHeaderRenderer"]
HEADER_SIDE = ["header", "musicSideAlignedItemRenderer"]
DESCRIPTION_SHELF = ["musicDescriptionShelfRenderer"]
DESCRIPTION = ["description"] + RUN_TEXT
DESCRIPTION = ["description", *RUN_TEXT]
CAROUSEL = ["musicCarouselShelfRenderer"]
IMMERSIVE_CAROUSEL = ["musicImmersiveCarouselShelfRenderer"]
CAROUSEL_CONTENTS = CAROUSEL + ["contents"]
CAROUSEL_TITLE = ["header", "musicCarouselShelfBasicHeaderRenderer"] + TITLE
CARD_SHELF_TITLE = ["header", "musicCardShelfHeaderBasicRenderer"] + TITLE_TEXT
CAROUSEL_CONTENTS = [*CAROUSEL, "contents"]
CAROUSEL_TITLE = ["header", "musicCarouselShelfBasicHeaderRenderer", *TITLE]
CARD_SHELF_TITLE = ["header", "musicCardShelfHeaderBasicRenderer", *TITLE_TEXT]
FRAMEWORK_MUTATIONS = ["frameworkUpdates", "entityBatchUpdate", "mutations"]


Expand Down
4 changes: 2 additions & 2 deletions ytmusicapi/parsers/albums.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def parse_album_header(response):
# add to library/uploaded
menu = nav(header, MENU)
toplevel = menu["topLevelButtons"]
album["audioPlaylistId"] = nav(toplevel, [0, "buttonRenderer"] + NAVIGATION_WATCH_PLAYLIST_ID, True)
album["audioPlaylistId"] = nav(toplevel, [0, "buttonRenderer", *NAVIGATION_WATCH_PLAYLIST_ID], True)
if not album["audioPlaylistId"]:
album["audioPlaylistId"] = nav(toplevel, [0, "buttonRenderer"] + NAVIGATION_PLAYLIST_ID, True)
album["audioPlaylistId"] = nav(toplevel, [0, "buttonRenderer", *NAVIGATION_PLAYLIST_ID], True)
service = nav(toplevel, [1, "buttonRenderer", "defaultServiceEndpoint"], True)
if service:
album["likeStatus"] = parse_like_status(service)
Expand Down
4 changes: 2 additions & 2 deletions ytmusicapi/parsers/browsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ def parse_mixed_content(rows):
for row in rows:
if DESCRIPTION_SHELF[0] in row:
results = nav(row, DESCRIPTION_SHELF)
title = nav(results, ["header"] + RUN_TEXT)
title = nav(results, ["header", *RUN_TEXT])
contents = nav(results, DESCRIPTION)
else:
results = next(iter(row.values()))
if "contents" not in results:
continue
title = nav(results, CAROUSEL_TITLE + ["text"])
title = nav(results, [*CAROUSEL_TITLE, "text"])
contents = []
for result in results["contents"]:
data = nav(result, [MTRIR], True)
Expand Down
2 changes: 1 addition & 1 deletion ytmusicapi/parsers/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def parse_chart_trending(data):

def parse_ranking(data):
return {
"rank": nav(data, ["customIndexColumn", "musicCustomIndexColumnRenderer"] + TEXT_RUN_TEXT),
"rank": nav(data, ["customIndexColumn", "musicCustomIndexColumnRenderer", *TEXT_RUN_TEXT]),
"trend": TRENDS[
nav(data, ["customIndexColumn", "musicCustomIndexColumnRenderer", "icon", "iconType"])
],
Expand Down
2 changes: 1 addition & 1 deletion ytmusicapi/parsers/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def parse_playlist_items(results, menu_entries: Optional[List[List]] = None, is_

videoType = nav(
data,
MENU_ITEMS + [0, "menuNavigationItemRenderer", "navigationEndpoint"] + NAVIGATION_VIDEO_TYPE,
[*MENU_ITEMS, 0, "menuNavigationItemRenderer", "navigationEndpoint", *NAVIGATION_VIDEO_TYPE],
True,
)

Expand Down
8 changes: 4 additions & 4 deletions ytmusicapi/parsers/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def parse_top_result(data, search_result_types):
search_result["videoType"] = nav(on_tap, NAVIGATION_VIDEO_TYPE)

if result_type in ["song", "video", "album"]:
search_result["videoId"] = nav(data, ["onTap"] + WATCH_VIDEO_ID, True)
search_result["videoType"] = nav(data, ["onTap"] + NAVIGATION_VIDEO_TYPE, True)
search_result["videoId"] = nav(data, ["onTap", *WATCH_VIDEO_ID], True)
search_result["videoType"] = nav(data, ["onTap", *NAVIGATION_VIDEO_TYPE], True)

search_result["title"] = nav(data, TITLE_TEXT)
runs = nav(data, ["subtitle", "runs"])
Expand All @@ -52,7 +52,7 @@ def parse_top_result(data, search_result_types):
def parse_search_result(data, search_result_types, result_type, category):
default_offset = (not result_type or result_type == "album") * 2
search_result = {"category": category}
video_type = nav(data, PLAY_BUTTON + ["playNavigationEndpoint"] + NAVIGATION_VIDEO_TYPE, True)
video_type = nav(data, [*PLAY_BUTTON, "playNavigationEndpoint", *NAVIGATION_VIDEO_TYPE], True)
if not result_type and video_type:
result_type = "song" if video_type == "MUSIC_VIDEO_TYPE_ATV" else "video"

Expand Down Expand Up @@ -120,7 +120,7 @@ def parse_search_result(data, search_result_types, result_type, category):

if result_type in ["song", "video"]:
search_result["videoId"] = nav(
data, PLAY_BUTTON + ["playNavigationEndpoint", "watchEndpoint", "videoId"], True
data, [*PLAY_BUTTON, "playNavigationEndpoint", "watchEndpoint", "videoId"], True
)
search_result["videoType"] = video_type

Expand Down
4 changes: 2 additions & 2 deletions ytmusicapi/parsers/songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def parse_song_library_status(item) -> bool:
def parse_song_menu_tokens(item):
toggle_menu = item[TOGGLE_MENU]

library_add_token = nav(toggle_menu, ["defaultServiceEndpoint"] + FEEDBACK_TOKEN, True)
library_remove_token = nav(toggle_menu, ["toggledServiceEndpoint"] + FEEDBACK_TOKEN, True)
library_add_token = nav(toggle_menu, ["defaultServiceEndpoint", *FEEDBACK_TOKEN], True)
library_remove_token = nav(toggle_menu, ["toggledServiceEndpoint", *FEEDBACK_TOKEN], True)

in_library = parse_song_library_status(item)
if in_library:
Expand Down
2 changes: 1 addition & 1 deletion ytmusicapi/parsers/uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def parse_uploaded_items(results):
"musicDeletePrivatelyOwnedEntityCommand"
]["entityId"]

videoId = nav(data, MENU_ITEMS + [0] + MENU_SERVICE)["queueAddEndpoint"]["queueTarget"]["videoId"]
videoId = nav(data, [*MENU_ITEMS, 0, *MENU_SERVICE])["queueAddEndpoint"]["queueTarget"]["videoId"]

title = get_item_text(data, 0)
like = nav(data, MENU_LIKE_STATUS)
Expand Down
2 changes: 1 addition & 1 deletion ytmusicapi/parsers/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def parse_watch_track(data):
"feedbackTokens": feedback_tokens,
"likeStatus": like_status,
"inLibrary": library_status,
"videoType": nav(data, ["navigationEndpoint"] + NAVIGATION_VIDEO_TYPE, True),
"videoType": nav(data, ["navigationEndpoint", *NAVIGATION_VIDEO_TYPE], True),
}
track.update(song_info)
return track
Expand Down

0 comments on commit 9170197

Please sign in to comment.