Repository navigation
Expand file tree
/
Copy pathgetFromYouTube.py
More file actions
64 lines (48 loc) · 2.21 KB
/
Copy pathgetFromYouTube.py
File metadata and controls
64 lines (48 loc) · 2.21 KB
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
import requests
import datetime
import json
import my_config as mc
def getFromYouTube():
# get the top trending vids
parameters = {'part': 'snippet', 'chart': 'mostPopular', 'regionCode': 'US', 'videoCategoryId': '0',
'maxResults': '10', 'key': mc.youtube_apiKey}
headers = {'Content-Type': 'application/json'}
r = requests.get('https://www.googleapis.com/youtube/v3/videos', params=parameters, headers=headers)
#print("youtubeRESPONSE_CODE:", r.status_code)
# only continue if got the stuff successfully
if r.status_code != 200:
return None
results = r.json()['items']
#print(json.dumps(results, indent=4, sort_keys=True))
# create a list for each item that is a dict of data
resultList = list()
for result in results:
# create a dictionary for the current result
singleDict = {}
# get the title of the video
singleDict['title'] = result['snippet']['title']
# get the poster's channel name
singleDict['channelName'] = result['snippet']['channelTitle']
# get the thumbnail image of the video (this is the lowest res)
singleDict['thumbnail'] = result['snippet']['thumbnails']['default']['url']
# get the youtube url
singleDict['url'] = 'https://www.youtube.com/watch?v=' + result['id']
# add the result to the list of all results
resultList.append(singleDict)
# add the full dictionary with title of the website pulling from
outerDict = {}
outerDict['YouTube'] = resultList
return outerDict
#print(json.dumps(getFromYouTube(), indent=4, sort_keys=True))
#this stuff gets the highest viewCount vids
# # get the rfc time of 24 hour ago
# thePast = datetime.datetime.utcnow() - datetime.timedelta(hours=24)
# thePastRFC = strict_rfc3339.timestamp_to_rfc3339_utcoffset(int(thePast.timestamp()))
#
#
# parameters = {'part': 'snippet', 'maxResults': '10', 'order': 'viewCount', 'publishedAfter': thePastRFC,
# 'relevanceLanguage': 'en', 'regionCode': 'US', 'key': apiKey}
# r = requests.get('https://www.googleapis.com/youtube/v3/search', params=parameters)
#
# print("RESPONSE_CODE:", r.status_code)
# print(json.dumps(r.json(), indent=4, sort_keys=True))