-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrateLimiter.py
160 lines (130 loc) · 4.91 KB
/
rateLimiter.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import time
import random
import config
from googleapiclient.discovery import build
# Set up the YouTube API client
api_key = config.api_key
youtube = build('youtube', 'v3', developerKey=api_key)
# Initialize variables
eagles_last_request_time = 0
eagles_video_id = None
eagles_video_ids = None
eagles_response = None
sixers_last_request_time = 0
sixers_video_id = None
sixers_video_ids = None
sixers_response = None
phillies_last_request_time = 0
phillies_video_id = None
phillies_video_ids = None
phillies_response = None
flyers_last_request_time = 0
flyers_video_id = None
flyers_video_ids = None
flyers_response = None
#Limiter functions for YouTube requests
def eagles_request():
global eagles_response
global eagles_video_ids
global eagles_last_request_time
global eagles_video_id
current_time = time.time()
# Only make the request if at least an hour has passed since the last request
if current_time - sixers_last_request_time >= 3600:
# Use the YouTube API to search for videos about the Philadelphia Eagles
request = youtube.search().list(
part="id",
type='video',
q='Philadelphia Eagles',
videoDefinition='high',
#videoLicense='creativeCommon',
videoEmbeddable='true',
maxResults=20,
fields="items(id(videoId))"
)
eagles_response = request.execute()
# Get a list of video IDs from the search results
eagles_video_ids = [item['id']['videoId'] for item in eagles_response['items']]
# Choose a random video from the list of video IDs
eagles_video_id = random.choice(eagles_video_ids)
eagles_last_request_time = current_time
# Return the sixers_video_id
return eagles_video_id
def sixers_request():
global sixers_response
global sixers_video_ids
global sixers_last_request_time
global sixers_video_id
current_time = time.time()
# Only make the request if at least an hour has passed since the last request
if current_time - sixers_last_request_time >= 3600:
# Use the YouTube API to search for videos about the Philadelphia Eagles
request = youtube.search().list(
part="id",
type='video',
q='Philadelphia 76ers',
videoDefinition='high',
videoEmbeddable='true',
maxResults=20,
fields="items(id(videoId))"
)
sixers_response = request.execute()
# Get a list of video IDs from the search results
sixers_video_ids = [item['id']['videoId'] for item in sixers_response['items']]
# Choose a random video from the list of video IDs
sixers_video_id = random.choice(sixers_video_ids)
sixers_last_request_time = current_time
# Return the sixers_video_id
return sixers_video_id
def phillies_request():
global phillies_response
global phillies_video_ids
global phillies_last_request_time
global phillies_video_id
current_time = time.time()
# Only make the request if at least an hour has passed since the last request
if current_time - phillies_last_request_time >= 3600:
# Use the YouTube API to search for videos about the Philadelphia Eagles
request = youtube.search().list(
part="id",
type='video',
q='Philadelphia Phillies',
videoDefinition='high',
videoEmbeddable='true',
maxResults=20,
fields="items(id(videoId))"
)
phillies_response = request.execute()
# Get a list of video IDs from the search results
phillies_video_ids = [item['id']['videoId'] for item in phillies_response['items']]
# Choose a random video from the list of video IDs
phillies_video_id = random.choice(phillies_video_ids)
phillies_last_request_time = current_time
# Return the phillies_video_id
return phillies_video_id
def flyers_request():
global flyers_response
global flyers_video_ids
global flyers_last_request_time
global flyers_video_id
current_time = time.time()
# Only make the request if at least an hour has passed since the last request
if current_time - flyers_last_request_time >= 3600:
# Use the YouTube API to search for videos about the Philadelphia Eagles
request = youtube.search().list(
part="id",
type='video',
q='Philadelphia Flyers',
videoDefinition='high',
videoEmbeddable='true',
maxResults=20,
fields="items(id(videoId))"
)
flyers_response = request.execute()
# Get a list of video IDs from the search results
flyers_video_ids = [item['id']['videoId'] for item in flyers_response['items']]
# Choose a random video from the list of video IDs
flyers_video_id = random.choice(flyers_video_ids)
flyers_last_request_time = current_time
# Return the flyers_video_id
return flyers_video_id