-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
92 lines (62 loc) · 2.22 KB
/
main.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os, json
from dotenv import load_dotenv
load_dotenv()
key = os.getenv("API_KEY")
directory_files = os.listdir(os.getenv("DOWNLOAD_DIR"))
print(directory_files)
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
def get_playlist_items(playlist_id, page_id=None):
api_service_name = "youtube"
api_version = "v3"
youtube = googleapiclient.discovery.build(api_service_name, api_version, developerKey=key)
request = youtube.playlistItems().list(
part = "snippet, contentDetails",
maxResults = 30,
playlistId = playlist_id,
pageToken = page_id,
)
return request.execute()
saved_playlists = {}
try:
with open("saved_playlists.json", "r") as json_file:
saved_playlists = json.load(json_file)
json_file.close()
except FileNotFoundError:
json_file = open("saved_playlists.json", "w")
json.dump({}, json_file)
json_file.close()
def main():
playlist_ids = ["PLCvPrFTfR1h4ZRcYgRSEdo7Rx9fq5GzMI", "PLCvPrFTfR1h6ARHTeh6AeWrChhPSad3-w"]
for playlist_id in playlist_ids:
page_id = ""
items = []
while page_id is not None:
response = get_playlist_items(playlist_id, page_id)
for item in response["items"]:
title = item["snippet"]["title"]
if title != "Deleted video":
id = item["contentDetails"]["videoId"]
url = f"https://www.youtube.com/watch?v={id}"
item_dict = {
"id" : id,
"url" : url,
"title" : title
}
items.append(item_dict)
try:
page_id = response["nextPageToken"]
except KeyError:
page_id = None
break
saved_playlists[playlist_id] = {
"item_count" : len(items),
"items" : []
}
saved_playlists[playlist_id]["items"] = items
with open("saved_playlists.json", "w") as json_file:
json.dump(saved_playlists, json_file)
json_file.close()
if __name__ == "__main__":
main()