63 lines
2.0 KiB
Python
63 lines
2.0 KiB
Python
"""YouTube API code for Youtube2FreshRSS."""
|
|
|
|
import googleapiclient.discovery
|
|
|
|
|
|
class YouTube:
|
|
"""YouTube API helper class."""
|
|
api_service_name = "youtube"
|
|
api_version = "v3"
|
|
|
|
def __init__(self, api_key, channel_id, rss_bridge_base_url):
|
|
self.api_key = api_key
|
|
self.channel_id = channel_id
|
|
self.rss_bridge_base_url = rss_bridge_base_url
|
|
|
|
self.api_client = googleapiclient.discovery.build(
|
|
self.api_service_name,
|
|
self.api_version,
|
|
developerKey=self.api_key
|
|
)
|
|
|
|
def get_subscription_list_page(self, page_token):
|
|
"""Retrieve subscription list page from YouTube."""
|
|
request = self.api_client.subscriptions().list(
|
|
part="snippet",
|
|
order="alphabetical",
|
|
maxResults=50,
|
|
pageToken=page_token,
|
|
channelId=self.channel_id
|
|
)
|
|
|
|
return request.execute()
|
|
|
|
def get_subscriptions(self):
|
|
"""Retrieve subscriptions list page from YouTube."""
|
|
next_page_token = ""
|
|
subscriptions = []
|
|
|
|
while True:
|
|
response = self.get_subscription_list_page(next_page_token)
|
|
subscriptions.extend(response['items'])
|
|
if 'nextPageToken' not in response:
|
|
break
|
|
next_page_token = response['nextPageToken']
|
|
|
|
return subscriptions
|
|
|
|
def parse_subscriptions(self, subscriptions):
|
|
"""Build feed URLs from YouTube subscriptions."""
|
|
base_feed_url = f"{self.rss_bridge_base_url}?action=display&bridge=Youtube&context=By+channel+id&duration_min=&duration_max=&format=Atom&c="
|
|
urls = []
|
|
parsed = []
|
|
|
|
for subscription in subscriptions:
|
|
channel_id = subscription['snippet']['resourceId']['channelId']
|
|
urls.append(base_feed_url + channel_id)
|
|
parsed.append({
|
|
"url": base_feed_url + channel_id,
|
|
"title": subscription['snippet']['title']
|
|
})
|
|
|
|
return urls, parsed
|