-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFRequest.py
More file actions
61 lines (49 loc) · 1.71 KB
/
BFRequest.py
File metadata and controls
61 lines (49 loc) · 1.71 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
# @Author: Gao Bo
# @Date: 2016-10-29T19:53:25-04:00
# @Last modified by: Gao Bo
# @Last modified time: 2016-10-30T12:11:43-04:00
import json
import requests
from collections import OrderedDict
import sys
urlprefix = 'https://www.buzzfeed.com/api/v2/feeds/'
def getBFTrends(sectionName, maxN=20, dumpHuman=False):
'''
parameters:
sectionName the name of the section you want
maxN the max number of trends you want
dumpHuman whether to dump the buzzfeed to a human readable file
'''
BFTrends = requests.get(urlprefix+sectionName).json(object_pairs_hook=OrderedDict)
if dumpHuman:
outfile = open('bfTrendsOutput.txt', 'w')
outfile.write(json.dumps(BFTrends, sort_keys=True, indent=4, separators=(',', ': ')))
outfile.close()
parsedBFTrends = []
buzzesList = BFTrends['buzzes']
for buzz in buzzesList:
tbuzz = {}
tbuzz['title'] = buzz['title']
tbuzz['category'] = buzz['category']
tbuzz['id'] = buzz['id']
tbuzz['published_date'] = buzz['published_date']
tbuzz['tags'] = buzz['tags']
parsedBFTrends.append(tbuzz)
if len(parsedBFTrends) >= maxN: break
return parsedBFTrends
if __name__ == '__main__':
argc = len(sys.argv)
# print(argc)
if argc == 1 or argc > 3:
print ('''
Usage:
python BFRequest.py [sectionNum, maxN]
''')
parsedBFTrends = getBFTrends('trending')
# parsedBFTrends = getBFTrends('life')
elif argc == 2:
parsedBFTrends = getBFTrends(sys.argv[1])
elif argc == 3:
parsedBFTrends = getBFTrends(sys.argv[1], int(sys.argv[2]))
for buzz in parsedBFTrends:
print(buzz['title'])