[youtube] Support for playlists and channels in search results.

This commit is contained in:
Fabio Manganiello 2024-07-11 23:35:10 +02:00
parent c9daa037a7
commit 5e905db0f5
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
2 changed files with 78 additions and 2 deletions

View file

@ -108,6 +108,19 @@ class YoutubePlugin(Plugin):
return id_or_url return id_or_url
@staticmethod
def _dump_item(item: dict) -> dict:
if item.get('type') == 'stream':
return dict(PipedVideoSchema().dump(item))
if item.get('type') == 'channel':
return dict(PipedChannelSchema().dump(item))
if item.get('type') == 'playlist':
return dict(PipedPlaylistSchema().dump(item))
return item
@action @action
def search(self, query: str, **_) -> List[dict]: def search(self, query: str, **_) -> List[dict]:
""" """
@ -118,9 +131,9 @@ class YoutubePlugin(Plugin):
""" """
self.logger.info('Searching YouTube for "%s"', query) self.logger.info('Searching YouTube for "%s"', query)
rs = self._request('search', auth=False, params={'q': query, 'filter': 'all'}) rs = self._request('search', auth=False, params={'q': query, 'filter': 'all'})
results = PipedVideoSchema(many=True).dump(rs.get("items", [])) or [] results = [self._dump_item(item) for item in rs.get('items', [])]
self.logger.info( self.logger.info(
'%d YouTube video results for the search query "%s"', '%d YouTube results for the search query "%s"',
len(results), len(results),
query, query,
) )

View file

@ -19,6 +19,14 @@ class PipedVideoSchema(Schema):
unknown = EXCLUDE unknown = EXCLUDE
item_type = fields.Constant(
'video',
metadata={
'description': 'Item type',
'example': 'video',
},
)
url = fields.Url( url = fields.Url(
required=True, required=True,
metadata={ metadata={
@ -119,6 +127,14 @@ class PipedPlaylistSchema(Schema):
unknown = EXCLUDE unknown = EXCLUDE
item_type = fields.Constant(
'playlist',
metadata={
'description': 'Item type',
'example': 'playlist',
},
)
id = fields.UUID( id = fields.UUID(
required=True, required=True,
metadata={ metadata={
@ -159,6 +175,45 @@ class PipedPlaylistSchema(Schema):
}, },
) )
channel = fields.String(
attribute='uploaderName',
metadata={
'description': 'Channel name',
'example': 'My Channel',
},
)
channel_url = fields.Url(
attribute='uploaderUrl',
metadata={
'description': 'Channel URL',
'example': 'https://youtube.com/channel/1234567890',
},
)
channel_image = fields.Url(
attribute='uploaderAvatar',
metadata={
'description': 'Channel image URL',
'example': 'https://i.ytimg.com/vi/1234567890/hqdefault.jpg',
},
)
@pre_dump
def fill_urls(self, data: dict, **_):
for attr in ('url', 'uploaderUrl'):
if data.get(attr) and not data[attr].startswith('https://'):
data[attr] = f'https://youtube.com{data[attr]}'
return data
@pre_dump
def normalize_timestamps(self, data: dict, **_):
if data.get('uploaded') and isinstance(data['uploaded'], int):
data['uploaded'] = datetime.fromtimestamp(data["uploaded"] / 1000)
return data
class PipedChannelSchema(Schema): class PipedChannelSchema(Schema):
""" """
@ -180,6 +235,14 @@ class PipedChannelSchema(Schema):
}, },
) )
item_type = fields.Constant(
'channel',
metadata={
'description': 'Item type',
'example': 'channel',
},
)
url = fields.String( url = fields.String(
required=True, required=True,
metadata={ metadata={