-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathneetcode.py
87 lines (74 loc) · 3.04 KB
/
neetcode.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
#!/usr/bin/env python
# encoding: utf-8
# Utility script to generate a mapping between leetcode question IDs to their respective Neetcode video.
# The output will be stored in a file named youtube.json, and this script assumes that the environmental
# variable YOUTUBE_API_KEY is set to a valid Google API key with the YouTube API enabled. You can also
# optionally pass in the API key as a command line argument.
import json
import os
import re
import sys
from typing import Union
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
def main(youtube_api_key: Union[str, None] = None):
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
if not youtube_api_key:
youtube_api_key = os.environ.get("YOUTUBE_API_KEY", None)
if not youtube_api_key:
print(
"Please set the environmental variable YOUTUBE_API_KEY to a"
" valid Google API key with the YouTube API enabled."
)
sys.exit(1)
api_service_name = "youtube"
api_version = "v3"
youtube = googleapiclient.discovery.build(
api_service_name, api_version, developerKey=youtube_api_key
)
# Obtain all videos from NeetCode channel
all_videos = []
channel_ids = ["UC_mYaQAE6-71rjSN6CeCA-g", "UCevUmOfLTUX9MNGJQKsPdIA"]
for channel in channel_ids:
next_page_token = None
while True:
if next_page_token:
request = youtube.search().list(
part="snippet",
channelId=channel,
maxResults=50,
pageToken=next_page_token,
)
else:
request = youtube.search().list(
part="snippet",
channelId=channel,
maxResults=50,
)
response = request.execute()
all_videos.extend(response["items"])
if response.get("nextPageToken", False):
next_page_token = response["nextPageToken"]
else:
break
print(f"Found {len(all_videos)} videos from NeetCode channel(s).")
# Create dictionary mapping Leetcode ID to each video with metadata
leetcode_id_to_video = {}
for video in all_videos:
match = re.search("Leetcode \d+", video["snippet"]["title"])
if match:
leetcode_id = match.group(0).split(" ")[1]
leetcode_id_to_video[leetcode_id] = {
"title": video["snippet"]["title"],
"url": "https://youtube.com/watch?v=" + video["id"]["videoId"],
}
print(f"Found {len(leetcode_id_to_video)} videos related to Leetcode problems.")
with open("youtube.json", "w") as f:
json.dump(leetcode_id_to_video, f, indent=4, sort_keys=True)
if __name__ == "__main__":
if sys.argv[1]:
youtube_api_key = sys.argv[1]
main(youtube_api_key)