This repository was archived by the owner on Apr 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathget_video_urls.py
More file actions
68 lines (53 loc) · 2.21 KB
/
Copy pathget_video_urls.py
File metadata and controls
68 lines (53 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
from django.core.management.base import BaseCommand, CommandError
import googleapiclient.discovery
import googleapiclient.errors
class Command(BaseCommand):
"""
Get details of the videos corresponding to the provided playlist.
"""
def add_arguments(self, parser):
parser.add_argument('--playlist-id', type=str, required=True)
def handle(self, *args, **options):
# Setup the API client
playlist_id = options['playlist_id']
api_service_name = "youtube"
api_version = "v3"
api_key = os.getenv("GOOGLE_API_KEY")
youtube_client = googleapiclient.discovery.build(
serviceName=api_service_name,
version=api_version,
developerKey=api_key,
)
# Fetch the video data from the API
playlist_items = []
items_per_page = 50 # max allowed by the api
default_request_args = dict(
part="snippet,contentDetails",
maxResults=items_per_page,
playlistId=playlist_id,
)
request = youtube_client.playlistItems().list(**default_request_args)
response = request.execute()
page_count = 1
total_results = response["pageInfo"]["totalResults"]
playlist_items += response["items"]
while page_count * items_per_page < total_results:
next_page_token = response["nextPageToken"]
request = youtube_client.playlistItems().list(**default_request_args, pageToken=next_page_token)
response = request.execute()
page_count += 1
playlist_items += response["items"]
# Process the results
processed_items = []
for item in playlist_items:
video_id = item['contentDetails']['videoId']
youtube_title = item['snippet']['title']
title_separator = ' - '
title_separator_ix = youtube_title.find(title_separator)
processed_items.append({
'speaker': youtube_title[:title_separator_ix],
'title': youtube_title[title_separator_ix + len(title_separator):],
'url': f'https://www.youtube.com/watch?v={video_id}',
})
print(processed_items)