Spaces:
Sleeping
Sleeping
Update apiSearch.py
Browse files- apiSearch.py +5 -20
apiSearch.py
CHANGED
@@ -8,16 +8,8 @@ import os
|
|
8 |
|
9 |
apiKeys = [
|
10 |
'AIzaSyC7KzwigUsNJ4KNvqGfPqXVK9QcDBsKU78',
|
11 |
-
'
|
12 |
-
'
|
13 |
-
'AIzaSyCW5J_uI37UPmq3mJVAhVdWNdGSMAMg5tI',
|
14 |
-
'AIzaSyC8VVO0DhDY91lfyqqaUW85VKriqBiahBA',
|
15 |
-
'AIzaSyDC744JL3Xa3eORSxORoxKpunKFPPMGb3Y',
|
16 |
-
'AIzaSyA-DwJmtgWFO-I-Dwv1hcISJKXGDjbpZok',
|
17 |
-
'AIzaSyDC744JL3Xa3eORSxORoxKpunKFPPMGb3Y',
|
18 |
-
'AIzaSyD74KqDih_2AyOIJV-HaIvU9DdUOIyRONs',
|
19 |
-
'AIzaSyALgq5vR27iGsuFuLiz-Ry4NGy6E-L1PUY',
|
20 |
-
'AIzaSyC4hp-RHBw5uY4NcthYw-A2fqYyrG22kaE'
|
21 |
]
|
22 |
|
23 |
class YouTubeService:
|
@@ -40,8 +32,6 @@ def get_next_api_key():
|
|
40 |
youtube.switch_api_key()
|
41 |
return apiKeys[next_key_index]
|
42 |
|
43 |
-
|
44 |
-
|
45 |
def get_video_id(url):
|
46 |
video_id = None
|
47 |
parsed_url = urlparse(url)
|
@@ -57,19 +47,15 @@ def get_video_id(url):
|
|
57 |
|
58 |
def get_video_metadata(video_id):
|
59 |
try:
|
60 |
-
# Get the next API key
|
61 |
api_key = get_next_api_key()
|
62 |
|
63 |
-
# Set up the YouTube Data API client
|
64 |
youtube = build('youtube', 'v3', developerKey=api_key)
|
65 |
|
66 |
-
# Call the API to retrieve video metadata
|
67 |
response = youtube.videos().list(
|
68 |
part='snippet,contentDetails,statistics',
|
69 |
id=video_id
|
70 |
).execute()
|
71 |
|
72 |
-
# Extract the relevant metadata
|
73 |
if 'items' in response and len(response['items']) > 0:
|
74 |
video = response['items'][0]
|
75 |
try:
|
@@ -96,12 +82,11 @@ def get_video_metadata(video_id):
|
|
96 |
return None
|
97 |
|
98 |
def get_metadata(url):
|
99 |
-
|
100 |
video_id = get_video_id(url)
|
101 |
metadata = get_video_metadata(video_id)
|
102 |
|
103 |
if metadata is not None:
|
104 |
-
# Create a DataFrame from the metadata
|
105 |
df = pd.DataFrame([metadata])
|
106 |
df['duration'] = df['duration'].apply(lambda x: isodate.parse_duration(x).total_seconds())
|
107 |
df['cleanTitle'] = df['title'].apply(preprocess)
|
@@ -115,7 +100,7 @@ def get_metadata(url):
|
|
115 |
|
116 |
def get_trending_videos(country_code):
|
117 |
try:
|
118 |
-
api_key = get_next_api_key()
|
119 |
youtube = build('youtube', 'v3', developerKey=api_key)
|
120 |
|
121 |
try:
|
@@ -123,7 +108,7 @@ def get_trending_videos(country_code):
|
|
123 |
part='snippet,contentDetails,statistics',
|
124 |
chart='mostPopular',
|
125 |
regionCode=country_code,
|
126 |
-
maxResults=10
|
127 |
).execute()
|
128 |
|
129 |
trending_videos = []
|
|
|
8 |
|
9 |
apiKeys = [
|
10 |
'AIzaSyC7KzwigUsNJ4KNvqGfPqXVK9QcDBsKU78',
|
11 |
+
'AIzaSyC7KzwigUsNJ4KNvqGfPqXVK9QcDBsKU78',
|
12 |
+
'AIzaSyC7KzwigUsNJ4KNvqGfPqXVK9QcDBsKU78',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
]
|
14 |
|
15 |
class YouTubeService:
|
|
|
32 |
youtube.switch_api_key()
|
33 |
return apiKeys[next_key_index]
|
34 |
|
|
|
|
|
35 |
def get_video_id(url):
|
36 |
video_id = None
|
37 |
parsed_url = urlparse(url)
|
|
|
47 |
|
48 |
def get_video_metadata(video_id):
|
49 |
try:
|
|
|
50 |
api_key = get_next_api_key()
|
51 |
|
|
|
52 |
youtube = build('youtube', 'v3', developerKey=api_key)
|
53 |
|
|
|
54 |
response = youtube.videos().list(
|
55 |
part='snippet,contentDetails,statistics',
|
56 |
id=video_id
|
57 |
).execute()
|
58 |
|
|
|
59 |
if 'items' in response and len(response['items']) > 0:
|
60 |
video = response['items'][0]
|
61 |
try:
|
|
|
82 |
return None
|
83 |
|
84 |
def get_metadata(url):
|
85 |
+
|
86 |
video_id = get_video_id(url)
|
87 |
metadata = get_video_metadata(video_id)
|
88 |
|
89 |
if metadata is not None:
|
|
|
90 |
df = pd.DataFrame([metadata])
|
91 |
df['duration'] = df['duration'].apply(lambda x: isodate.parse_duration(x).total_seconds())
|
92 |
df['cleanTitle'] = df['title'].apply(preprocess)
|
|
|
100 |
|
101 |
def get_trending_videos(country_code):
|
102 |
try:
|
103 |
+
api_key = get_next_api_key()
|
104 |
youtube = build('youtube', 'v3', developerKey=api_key)
|
105 |
|
106 |
try:
|
|
|
108 |
part='snippet,contentDetails,statistics',
|
109 |
chart='mostPopular',
|
110 |
regionCode=country_code,
|
111 |
+
maxResults=10
|
112 |
).execute()
|
113 |
|
114 |
trending_videos = []
|