diff --git a/ytmusicapi/parsers/browsing.py b/ytmusicapi/parsers/browsing.py index 4d12bdf..99ad040 100644 --- a/ytmusicapi/parsers/browsing.py +++ b/ytmusicapi/parsers/browsing.py @@ -1,6 +1,6 @@ -import re from typing import List, Dict from .utils import * +from .songs import * from ytmusicapi.helpers import i18n diff --git a/ytmusicapi/parsers/playlists.py b/ytmusicapi/parsers/playlists.py index dae866d..d0d564d 100644 --- a/ytmusicapi/parsers/playlists.py +++ b/ytmusicapi/parsers/playlists.py @@ -1,4 +1,5 @@ from .utils import * +from .songs import * from typing import List diff --git a/ytmusicapi/parsers/songs.py b/ytmusicapi/parsers/songs.py new file mode 100644 index 0000000..b8aabf8 --- /dev/null +++ b/ytmusicapi/parsers/songs.py @@ -0,0 +1,68 @@ +from .utils import * +import re + + +def parse_song_artists(data, index): + flex_item = get_flex_column_item(data, index) + if not flex_item: + return None + else: + runs = flex_item['text']['runs'] + artists = [] + for j in range(int(len(runs) / 2) + 1): + artists.append({ + 'name': runs[j * 2]['text'], + 'id': nav(runs[j * 2], NAVIGATION_BROWSE_ID, True) + }) + return artists + + +def parse_song_runs(runs): + parsed = {'artists': []} + for run in runs: + text = run['text'] + if 'navigationEndpoint' in run: # artist or album + item = {'name': text, 'id': nav(run, NAVIGATION_BROWSE_ID, True)} + + if item['id'] and item['id'].startswith('MPRE'): # album + parsed['album'] = item + else: # artist + parsed['artists'].append(item) + + else: + # note: YT uses non-breaking space \xa0 to separate number and magnitude + if re.match(r"^\d([^ ])* [^ ]*$", text): + parsed['views'] = text.split(' ')[0] + + elif re.match(r"^(\d+:)*\d+:\d+$", text): + parsed['duration'] = text + + elif re.match(r"^\d{4}$", text): + parsed['year'] = text + + return parsed + + +def parse_song_album(data, index): + flex_item = get_flex_column_item(data, index) + return None if not flex_item else { + 'name': get_item_text(data, index), + 'id': get_browse_id(flex_item, 0) + } + + +def parse_song_menu_tokens(item): + library_add_token = library_remove_token = None + toggle_menu = item['toggleMenuServiceItemRenderer'] + service_type = toggle_menu['defaultIcon']['iconType'] + if service_type == "LIBRARY_ADD": + library_add_token = nav(toggle_menu, ['defaultServiceEndpoint'] + FEEDBACK_TOKEN) + library_remove_token = nav(toggle_menu, ['toggledServiceEndpoint'] + FEEDBACK_TOKEN) + + elif service_type == "LIBRARY_REMOVE": # swap if already in library + library_add_token, library_remove_token = library_remove_token, library_add_token + + return {'add': library_add_token, 'remove': library_remove_token} + + + diff --git a/ytmusicapi/parsers/uploads.py b/ytmusicapi/parsers/uploads.py index 4baeada..1ea1e5f 100644 --- a/ytmusicapi/parsers/uploads.py +++ b/ytmusicapi/parsers/uploads.py @@ -1,4 +1,5 @@ -from ytmusicapi.parsers.utils import * +from .utils import * +from .songs import * def parse_uploaded_items(results): diff --git a/ytmusicapi/parsers/utils.py b/ytmusicapi/parsers/utils.py index 1600b10..539fe20 100644 --- a/ytmusicapi/parsers/utils.py +++ b/ytmusicapi/parsers/utils.py @@ -1,68 +1,4 @@ from . import * -import re - - -def parse_song_artists(data, index): - flex_item = get_flex_column_item(data, index) - if not flex_item: - return None - else: - runs = flex_item['text']['runs'] - artists = [] - for j in range(int(len(runs) / 2) + 1): - artists.append({ - 'name': runs[j * 2]['text'], - 'id': nav(runs[j * 2], NAVIGATION_BROWSE_ID, True) - }) - return artists - - -def parse_song_runs(runs): - parsed = {'artists': []} - for run in runs: - text = run['text'] - if 'navigationEndpoint' in run: # artist or album - item = {'name': text, 'id': nav(run, NAVIGATION_BROWSE_ID, True)} - - if item['id'] and item['id'].startswith('MPRE'): # album - parsed['album'] = item - else: # artist - parsed['artists'].append(item) - - else: - # note: YT uses non-breaking space \xa0 to separate number and magnitude - if re.match(r"^\d([^ ])* [^ ]*$", text): - parsed['views'] = text.split(' ')[0] - - elif re.match(r"^(\d+:)*\d+:\d+$", text): - parsed['duration'] = text - - elif re.match(r"^\d{4}$", text): - parsed['year'] = text - - return parsed - - -def parse_song_album(data, index): - flex_item = get_flex_column_item(data, index) - return None if not flex_item else { - 'name': get_item_text(data, index), - 'id': get_browse_id(flex_item, 0) - } - - -def parse_song_menu_tokens(item): - library_add_token = library_remove_token = None - toggle_menu = item['toggleMenuServiceItemRenderer'] - service_type = toggle_menu['defaultIcon']['iconType'] - if service_type == "LIBRARY_ADD": - library_add_token = nav(toggle_menu, ['defaultServiceEndpoint'] + FEEDBACK_TOKEN) - library_remove_token = nav(toggle_menu, ['toggledServiceEndpoint'] + FEEDBACK_TOKEN) - - elif service_type == "LIBRARY_REMOVE": # swap if already in library - library_add_token, library_remove_token = library_remove_token, library_add_token - - return {'add': library_add_token, 'remove': library_remove_token} def parse_menu_playlists(data, result): diff --git a/ytmusicapi/parsers/watch.py b/ytmusicapi/parsers/watch.py index 999dfe0..faae840 100644 --- a/ytmusicapi/parsers/watch.py +++ b/ytmusicapi/parsers/watch.py @@ -1,4 +1,5 @@ from .utils import * +from .songs import * def parse_watch_playlist(results):