16
16
from requests .adapters import HTTPAdapter
17
17
from urllib3 .util .retry import Retry
18
18
19
+ # Get the current working directory
19
20
CWD = os .path .dirname (os .path .abspath (__file__ ))
21
+ # Load environment variables
20
22
dotenv_path = os .path .join (os .path .dirname (CWD ), ".env" )
21
23
load_dotenv (dotenv_path )
22
24
25
+ # Get the current date
23
26
today = dt .datetime .today ()
27
+ # Get the YouTube API key
24
28
API_KEY = os .getenv ("YOUTUBE_API_KEY" )
29
+ # Set up file path for CSV report
25
30
DATA_WRITE_FILE = (
26
31
f"{ CWD } " f"/data_youtube_{ today .year } _{ today .month } _{ today .day } .csv"
27
32
)
31
36
32
37
33
38
def get_next_time_search_interval ():
34
- """Provides the next searching interval of time for Creative Commons
39
+ """
40
+ Provides the next searching interval of time for Creative Commons
35
41
licensed video.
36
42
37
43
Yields:
38
- tuple: A tuple representing the time search interval currently dealt
39
- via 2 RFC 3339 formatted date-time values (by YouTube API Standards),
40
- and the current starting year and month of the interval.
44
+ - tuple: A tuple representing the time search interval currently dealt
45
+ via 2 RFC 3339 formatted date-time values (by YouTube API Standards),
46
+ and the current starting year and month of the interval.
41
47
"""
42
48
cur_year , cur_month = 2009 , 1
43
49
while cur_year * 100 + cur_month <= today .year * 100 + today .month :
@@ -66,17 +72,18 @@ def get_next_time_search_interval():
66
72
67
73
68
74
def get_request_url (time = None ):
69
- """Provides the API Endpoint URL for specified parameter combinations.
75
+ """
76
+ Provides the API Endpoint URL for specified parameter combinations.
70
77
71
78
Args:
72
- time: A tuple indicating whether this query is related to video time
73
- occurrence , and the time interval which it would like to investigate.
74
- Defaults to None to indicate the query is not related to video time
75
- occurrence.
79
+ - time: A tuple indicating whether this query is related to video time
80
+ occerrence , and the time interval which it would like to investigate.
81
+ Defaults to None to indicate the query is not related to video time
82
+ occurrence.
76
83
77
84
Returns:
78
- string: A string representing the API Endpoint URL for the query
79
- specified by this function's parameters.
85
+ - string: A string representing the API Endpoint URL for the query
86
+ specified by this function's parameters.
80
87
"""
81
88
base_url = (
82
89
r"https://youtube.googleapis.com/youtube/v3/search?part=snippet"
@@ -92,17 +99,18 @@ def get_request_url(time=None):
92
99
93
100
94
101
def get_response_elems (time = None ):
95
- """Provides the metadata for query of specified parameters
102
+ """
103
+ Provides the metadata for query of specified parameters
96
104
97
105
Args:
98
- time: A tuple indicating whether this query is related to video time
99
- occurrence, and the time interval which it would like to investigate.
100
- Defaults to None to indicate the query is not related to video time
101
- occurrence.
106
+ - time: A tuple indicating whether this query is related to video time
107
+ occurrence, and the time interval which it would like to investigate.
108
+ Defaults to None to indicate the query is not related to video time
109
+ occurrence.
102
110
103
111
Returns:
104
- dict: A dictionary mapping metadata to its value provided from the API
105
- query of specified parameters.
112
+ - dict: A dictionary mapping metadata to its value provided from the API
113
+ query of specified parameters.
106
114
"""
107
115
search_data = None
108
116
try :
@@ -114,6 +122,7 @@ def get_response_elems(time=None):
114
122
)
115
123
session = requests .Session ()
116
124
session .mount ("https://" , HTTPAdapter (max_retries = max_retries ))
125
+ # Send GET request to YouTube API
117
126
with session .get (request_url ) as response :
118
127
response .raise_for_status ()
119
128
search_data = response .json ()
0 commit comments