Compare commits

..

4 Commits

Author SHA1 Message Date
sepro
45853a13b8
Merge 9c32f15b7b into f2a4983df7 2024-11-15 04:22:19 +00:00
sepro
9c32f15b7b Add _call_api method 2024-11-15 05:22:17 +01:00
sepro
7f88140dbe Add media_type 2024-11-15 05:10:37 +01:00
sepro
2d0ff76574 Set extractor/url for parse functions 2024-11-15 05:08:11 +01:00

View File

@ -3,26 +3,38 @@ from .common import InfoExtractor
from ..utils import (
ExtractorError,
float_or_none,
format_field,
int_or_none,
parse_iso8601,
parse_qs,
truncate_string,
url_or_none,
)
from ..utils.traversal import traverse_obj
from ..utils.traversal import traverse_obj, value
class BandlabBaseIE(InfoExtractor):
_API_HEADERS = {
'accept': 'application/json',
'referer': 'https://www.bandlab.com/',
'x-client-id': 'BandLab-Web',
'x-client-version': '10.1.123',
'x-client-version': '10.1.124',
}
def _parse_revision(self, revision_data):
def _call_api(self, endpoint, asset_id, **kwargs):
return self._download_json(
f'https://www.bandlab.com/api/v1.3/{endpoint}/{asset_id}',
asset_id, headers=self._API_HEADERS, **kwargs)
def _parse_revision(self, revision_data, url=None):
return {
'vcodec': 'none',
'media_type': 'revision',
'extractor_key': BandlabIE.ie_key(),
'extractor': BandlabIE.IE_NAME,
**traverse_obj(revision_data, {
'webpage_url': (
'id', ({value(url)}, {format_field(template='https://www.bandlab.com/revision/%s')}), filter, any),
'id': (('revisionId', 'id'), {str}, any),
'title': ('song', 'name', {str}),
'track': ('song', 'name', {str}),
@ -40,10 +52,15 @@ class BandlabBaseIE(InfoExtractor):
}),
}
def _parse_track(self, track_data):
def _parse_track(self, track_data, url=None):
return {
'vcodec': 'none',
'media_type': 'track',
'extractor_key': BandlabIE.ie_key(),
'extractor': BandlabIE.IE_NAME,
**traverse_obj(track_data, {
'webpage_url': (
'id', ({value(url)}, {format_field(template='https://www.bandlab.com/post/%s')}), filter, any),
'id': (('revisionId', 'id'), {str}, any),
'url': ('track', 'sample', 'audioUrl', {url_or_none}),
'title': ('track', 'name', {str}),
@ -60,20 +77,27 @@ class BandlabBaseIE(InfoExtractor):
}),
}
def _parse_video(self, video_data):
return traverse_obj(video_data, {
'id': ('id', {str}),
'url': ('video', 'url', {url_or_none}),
'title': ('caption', {lambda x: x.replace('\n', ' ')}, {truncate_string(left=50)}),
'description': ('caption', {str}),
'thumbnail': ('video', 'picture', 'url', {url_or_none}),
'view_count': ('video', 'counters', 'plays', {int_or_none}),
'like_count': ('video', 'counters', 'likes', {int_or_none}),
'comment_count': ('counters', 'comments', {int_or_none}),
'duration': ('video', 'duration', {float_or_none}),
'uploader': ('creator', 'name', {str}),
'uploader_id': ('creator', 'username', {str}),
})
def _parse_video(self, video_data, url=None):
return {
'media_type': 'video',
'extractor_key': BandlabIE.ie_key(),
'extractor': BandlabIE.IE_NAME,
**traverse_obj(video_data, {
'id': ('id', {str}),
'webpage_url': (
'id', ({value(url)}, {format_field(template='https://www.bandlab.com/post/%s')}), filter, any),
'url': ('video', 'url', {url_or_none}),
'title': ('caption', {lambda x: x.replace('\n', ' ')}, {truncate_string(left=50)}),
'description': ('caption', {str}),
'thumbnail': ('video', 'picture', 'url', {url_or_none}),
'view_count': ('video', 'counters', 'plays', {int_or_none}),
'like_count': ('video', 'counters', 'likes', {int_or_none}),
'comment_count': ('counters', 'comments', {int_or_none}),
'duration': ('video', 'duration', {float_or_none}),
'uploader': ('creator', 'name', {str}),
'uploader_id': ('creator', 'username', {str}),
}),
}
class BandlabIE(BandlabBaseIE):
@ -101,6 +125,7 @@ class BandlabIE(BandlabBaseIE):
'genres': ['Lofi'],
'uploader': 'ender milze',
'comment_count': int,
'media_type': 'revision',
},
}, {
# Same track as above but post URL
@ -122,6 +147,7 @@ class BandlabIE(BandlabBaseIE):
'genres': ['Lofi'],
'uploader': 'ender milze',
'comment_count': int,
'media_type': 'revision',
},
}, {
# SharedKey Example
@ -143,6 +169,7 @@ class BandlabIE(BandlabBaseIE):
'upload_date': '20240305',
'uploader': 'Erna Wageneder',
'title': 'PodcastMaerchen4b',
'media_type': 'revision',
},
}, {
# Different Revision selected
@ -164,6 +191,7 @@ class BandlabIE(BandlabBaseIE):
'upload_date': '20200430',
'like_count': int,
'duration': 279.43,
'media_type': 'revision',
},
}, {
# Video
@ -180,6 +208,7 @@ class BandlabIE(BandlabBaseIE):
'uploader': 'auraa',
'like_count': int,
'description': 'backing vocals',
'media_type': 'video',
},
}, {
# Embed Example
@ -201,6 +230,7 @@ class BandlabIE(BandlabBaseIE):
'uploader_id': 'microfreaks',
'title': 'Positronic Meltdown',
'like_count': int,
'media_type': 'revision',
},
}, {
# Track without revisions available
@ -220,6 +250,7 @@ class BandlabIE(BandlabBaseIE):
'thumbnail': 'https://bandlabimages.azureedge.net/v1.0/users/572a351a-0f3a-4c6a-ac39-1a5defdeeb1c/',
'timestamp': 1691162128,
'upload_date': '20230804',
'media_type': 'track',
},
}, {
'url': 'https://www.bandlab.com/revision/014de0a4-7d82-ea11-a94c-0003ffd19c0f',
@ -243,6 +274,7 @@ class BandlabIE(BandlabBaseIE):
'title': 'Vermilion Pt. 2 (Cover)',
'track': 'Vermilion Pt. 2 (Cover)',
'thumbnail': 'https://bandlabimages.azureedge.net/v1.0/songs/62b10750-7aef-4f42-ad08-1af52f577e97/',
'media_type': 'revision',
},
}]
@ -256,9 +288,8 @@ class BandlabIE(BandlabBaseIE):
revision_data = None
if not revision_id:
post_data = self._download_json(
f'https://www.bandlab.com/api/v1.3/posts/{display_id}', display_id,
note='Downloading post data', headers=self._API_HEADERS,
post_data = self._call_api(
'posts', display_id, note='Downloading post data',
query=traverse_obj(qs, {'sharedKey': ('sharedKey', 0)}))
revision_id = traverse_obj(post_data, (('revisionId', ('revision', 'id')), {str}, any))
@ -267,17 +298,16 @@ class BandlabIE(BandlabBaseIE):
if not revision_data and not revision_id:
post_type = post_data.get('type')
if post_type == 'Video':
return self._parse_video(post_data)
return self._parse_video(post_data, url=url)
if post_type == 'Track':
return self._parse_track(post_data)
return self._parse_track(post_data, url=url)
raise ExtractorError('Could not extract data')
if not revision_data:
revision_data = self._download_json(
f'https://www.bandlab.com/api/v1.3/revisions/{revision_id}', revision_id,
note='Downloading revision data', headers=self._API_HEADERS, query={'edit': 'false'})
revision_data = self._call_api(
'revisions', revision_id, note='Downloading revision data', query={'edit': 'false'})
return self._parse_revision(revision_data)
return self._parse_revision(revision_data, url=url)
class BandlabPlaylistIE(BandlabBaseIE):
@ -379,9 +409,8 @@ class BandlabPlaylistIE(BandlabBaseIE):
'embed': ['collections', 'albums'],
}.get(playlist_type)
for endpoint in endpoints:
playlist_data = self._download_json(
f'https://www.bandlab.com/api/v1.3/{endpoint}/{playlist_id}', playlist_id,
note=f'Downloading {endpoint[:-1]} data', headers=self._API_HEADERS,
playlist_data = self._call_api(
endpoint, playlist_id, note=f'Downloading {endpoint[:-1]} data',
fatal=False, expected_status=404)
if not playlist_data.get('errorCode'):
playlist_type = endpoint