-
Notifications
You must be signed in to change notification settings - Fork 0
/
subjects.py
85 lines (60 loc) · 1.71 KB
/
subjects.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
import requests
import time
from collections import Counter
from datetime import datetime
from flask import Flask, render_template, request
request_http = requests
def getDataFromNYT(month,year,key):
url = "https://api.nytimes.com/svc/archive/v1/"+ year +"/" + month +".json?api-key="+key
getDataFromNYT = request_http.get(url)
jsonData = getDataFromNYT.json()
return jsonData
def filterData(data,count):
test=[]
keywords=[]
for object in data['response']['docs']:
keywords = object['keywords']
test.append(keywords)
return test
def read_all():
args = request.args
year = args.get('year')
month = args.get('month')
key = args.get('key')
count = 0
kw = []
data={}
dict=[]
final_json={}
year_keyword_list = []
f_json={}
count=count+1
data = getDataFromNYT(month,year,key)
#Apenas para evitar rate limit da api que fornece os dados
time.sleep(1)
obj = filterData(data,month)
o = []
o.append({
month:obj
})
for keyword in obj:
kw.append(keyword)
for k in kw:
for word in k:
if word['name']=='subject' and word['value'] != 'internal-sub-only-nl':
if 'value' in word:
year_keyword_list.append(word['value'])
c = Counter(year_keyword_list)
most_frequent_subjects = c.most_common(40)
i = 0
for item in most_frequent_subjects:
dict.append({
"subject":item[0],
"amount":item[1]
})
i=i+1
final_json={
month:dict
}
f_json = final_json
return list(f_json.values())