forked from LukePetruzzi/DailyHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getFromImgur.py
45 lines (32 loc) · 1.31 KB
/
getFromImgur.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
import requests
import json
import my_config as mc
def getFromImgur():
headers = {'Content-Type': 'application/json', 'Authorization': 'Client-ID:' + mc.imgur_clientId}
r = requests.get('https://api.imgur.com/3/gallery/hot/viral/0.json', headers=headers)
# print("imgurRESPONSE_CODE:", r.status_code)
# only continue if got the stuff successfully
if r.status_code != 200:
return None
results = r.json()['data']
# 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 post
singleDict['title'] = result['title']
# get the thumbnail
if 'cover' not in result:
singleDict['thumbnail'] = 'https://i.imgur.com/' + result['id'] + 'b.jpg'
else:
singleDict['thumbnail'] = 'https://i.imgur.com/' + result['cover'] + 'b.jpg'
# get the imgur url
singleDict['url'] = result['link']
# 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['Imgur'] = resultList
return outerDict