xinah3131 commited on
Commit
e46e65a
·
1 Parent(s): c095d8d

Update apiSearch.py

Browse files
Files changed (1) hide show
  1. apiSearch.py +91 -9
apiSearch.py CHANGED
@@ -4,8 +4,9 @@ from urllib.parse import urlparse, parse_qs
4
  from preprocessText import preprocess
5
  from googleapiclient.discovery import build
6
  import isodate
 
7
 
8
- api_keys = ['AIzaSyC4hp-RHBw5uY4NcthYw-A2fqYyrG22kaE',
9
  'AIzaSyC7KzwigUsNJ4KNvqGfPqXVK9QcDBsKU78',
10
  'AIzaSyDEPBCb1PhEaYHuBgzW6D5-ldTHUCowuq4',
11
  'AIzaSyD-LN8Z7xG8OHtMQ89GRDvIaRQwkVHzfEo',
@@ -16,9 +17,30 @@ api_keys = ['AIzaSyC4hp-RHBw5uY4NcthYw-A2fqYyrG22kaE',
16
  'AIzaSyDC744JL3Xa3eORSxORoxKpunKFPPMGb3Y',
17
  'AIzaSyD74KqDih_2AyOIJV-HaIvU9DdUOIyRONs',
18
  'AIzaSyALgq5vR27iGsuFuLiz-Ry4NGy6E-L1PUY',
19
- 'AIzaSyC4hp-RHBw5uY4NcthYw-A2fqYyrG22kaE']
 
 
 
 
 
 
20
 
21
- current_key_index = 0 # Declare current_key_index as a global variable
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  def get_video_id(url):
24
  video_id = None
@@ -32,10 +54,6 @@ def get_video_id(url):
32
  video_id = query_params['v'][0]
33
  return video_id
34
 
35
- def get_next_api_key():
36
- global current_key_index
37
- current_key_index = (current_key_index + 1) % len(api_keys)
38
- return api_keys[current_key_index]
39
 
40
  def get_video_metadata(video_id):
41
  try:
@@ -54,6 +72,10 @@ def get_video_metadata(video_id):
54
  # Extract the relevant metadata
55
  if 'items' in response and len(response['items']) > 0:
56
  video = response['items'][0]
 
 
 
 
57
  metadata = {
58
  'title': video['snippet']['title'],
59
  'description': video['snippet']['description'],
@@ -62,7 +84,7 @@ def get_video_metadata(video_id):
62
  'duration': video['contentDetails']['duration'],
63
  'views': video['statistics']['viewCount'],
64
  'likes': video['statistics']['likeCount'],
65
- 'comments': video['statistics']['commentCount'],
66
  'category_id': video['snippet']['categoryId'],
67
  'thumbnail_link': video['snippet']['thumbnails']['default']['url']
68
  }
@@ -77,6 +99,7 @@ def get_metadata(url):
77
  # Set up the YouTube Data API client
78
  video_id = get_video_id(url)
79
  metadata = get_video_metadata(video_id)
 
80
  if metadata is not None:
81
  # Create a DataFrame from the metadata
82
  df = pd.DataFrame([metadata])
@@ -85,8 +108,67 @@ def get_metadata(url):
85
  df['cleanTitle'] = df['cleanTitle'].apply(lambda x: ' '.join(x))
86
  df['titleLength'] = df['title'].apply(lambda x: len(x))
87
  df['descriptionLength'] = df['description'].apply(lambda x: len(x))
88
-
89
  return df
90
  else:
91
  return 0
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  from preprocessText import preprocess
5
  from googleapiclient.discovery import build
6
  import isodate
7
+ import os
8
 
9
+ apiKeys = [
10
  'AIzaSyC7KzwigUsNJ4KNvqGfPqXVK9QcDBsKU78',
11
  'AIzaSyDEPBCb1PhEaYHuBgzW6D5-ldTHUCowuq4',
12
  'AIzaSyD-LN8Z7xG8OHtMQ89GRDvIaRQwkVHzfEo',
 
17
  'AIzaSyDC744JL3Xa3eORSxORoxKpunKFPPMGb3Y',
18
  'AIzaSyD74KqDih_2AyOIJV-HaIvU9DdUOIyRONs',
19
  'AIzaSyALgq5vR27iGsuFuLiz-Ry4NGy6E-L1PUY',
20
+ 'AIzaSyC4hp-RHBw5uY4NcthYw-A2fqYyrG22kaE'
21
+ ]
22
+
23
+ class YouTubeService:
24
+ def __init__(self, api_key):
25
+ self.api_key = api_key
26
+ self.service = build('youtube', 'v3', developerKey=api_key)
27
 
28
+ def switch_api_key(self):
29
+ current_key_index = apiKeys.index(self.api_key)
30
+ next_key_index = (current_key_index + 1) % len(apiKeys)
31
+ self.api_key = apiKeys[next_key_index]
32
+ self.service = build('youtube', 'v3', developerKey=self.api_key)
33
+
34
+ # Initialize the YouTube service with the first API key
35
+ youtube = YouTubeService(apiKeys[0])
36
+
37
+ def get_next_api_key():
38
+ current_key_index = apiKeys.index(youtube.api_key)
39
+ next_key_index = (current_key_index + 1) % len(apiKeys)
40
+ youtube.switch_api_key()
41
+ return apiKeys[next_key_index]
42
+
43
+
44
 
45
  def get_video_id(url):
46
  video_id = None
 
54
  video_id = query_params['v'][0]
55
  return video_id
56
 
 
 
 
 
57
 
58
  def get_video_metadata(video_id):
59
  try:
 
72
  # Extract the relevant metadata
73
  if 'items' in response and len(response['items']) > 0:
74
  video = response['items'][0]
75
+ try:
76
+ comments = video['statistics']['commentCount']
77
+ except KeyError:
78
+ comments = 0
79
  metadata = {
80
  'title': video['snippet']['title'],
81
  'description': video['snippet']['description'],
 
84
  'duration': video['contentDetails']['duration'],
85
  'views': video['statistics']['viewCount'],
86
  'likes': video['statistics']['likeCount'],
87
+ 'comments': comments,
88
  'category_id': video['snippet']['categoryId'],
89
  'thumbnail_link': video['snippet']['thumbnails']['default']['url']
90
  }
 
99
  # Set up the YouTube Data API client
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])
 
108
  df['cleanTitle'] = df['cleanTitle'].apply(lambda x: ' '.join(x))
109
  df['titleLength'] = df['title'].apply(lambda x: len(x))
110
  df['descriptionLength'] = df['description'].apply(lambda x: len(x))
111
+ df['thumbnail_link'] = df['thumbnail_link'].str.replace('default.jpg', 'maxresdefault.jpg')
112
  return df
113
  else:
114
  return 0
115
 
116
+ def get_trending_videos(country_code):
117
+ try:
118
+ api_key = get_next_api_key() # Replace with your own YouTube Data API key
119
+ youtube = build('youtube', 'v3', developerKey=api_key)
120
+
121
+ try:
122
+ response = youtube.videos().list(
123
+ part='snippet,contentDetails,statistics',
124
+ chart='mostPopular',
125
+ regionCode=country_code,
126
+ maxResults=10 # Adjust the number of videos you want to retrieve
127
+ ).execute()
128
+
129
+ trending_videos = []
130
+ for item in response['items']:
131
+ title = item['snippet']['title']
132
+ description = item['snippet']['description'],
133
+ channel_title = item['snippet']['channelTitle']
134
+ publish_date = item['snippet']['publishedAt']
135
+ duration = item['contentDetails']['duration']
136
+ views = item['statistics']['viewCount']
137
+ try:
138
+ likes = item['statistics']['likeCount']
139
+ except KeyError:
140
+ likes = "Hidden!"
141
+ try:
142
+ comments = item['statistics']['commentCount']
143
+ except KeyError:
144
+ comments = "Hidden!"
145
+ category_id = item['snippet']['categoryId']
146
+ thumbnail_link = item['snippet']['thumbnails']['default']['url']
147
+ duration = isodate.parse_duration(duration)
148
+ duration = duration.total_seconds()
149
+ trending_videos.append({
150
+ 'title': title,
151
+ 'description':description,
152
+ 'channel_title': channel_title,
153
+ 'publish_date': publish_date,
154
+ 'duration': duration,
155
+ 'views': views,
156
+ 'likes': likes,
157
+ 'comments': comments,
158
+ 'category_id': category_id,
159
+ 'thumbnail_link': thumbnail_link
160
+ })
161
+ df = pd.DataFrame(trending_videos)
162
+ df['views'] = df['views'].astype(int)
163
+ df['likes'] = df['likes'].astype(str)
164
+ df['comments'] = df['comments'].astype(str)
165
+ df['category_id'] = df['category_id'].astype(int)
166
+ df['thumbnail_link'] = df['thumbnail_link'].str.replace('default.jpg', 'maxresdefault.jpg')
167
+ return df
168
+
169
+ except Exception as e:
170
+ print('An error occurred:', str(e))
171
+ return None
172
+
173
+ except Exception as e:
174
+ print("An error occurred:", str(e))