-
Notifications
You must be signed in to change notification settings - Fork 0
/
restAPIService.py
112 lines (87 loc) · 3.64 KB
/
restAPIService.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Required libraries - Gerekli kütüphaneler
#import os
#os.system("pip install pytrends")
#os.system("pip install flask")
#os.system("pip install flask_restful")
#os.system("pip install pandas")
from pytrends.request import TrendReq
from flask import Flask, request
from flask_restful import Api, Resource
import pandas as pd
import json
import mySparkSession
app = Flask(__name__)
api = Api(app)
class Trends(Resource):
def get(self):
f = open("default_parameters.json", "r")
default_parameters = json.loads(f.read())
f.close()
# hl: Makine dilini belirtir (tr-TR şeklinde) - host language
# tz: zaman dilimi - time zone
# pn: lokasyon - location (p-n)
pytrends = TrendReq(hl=default_parameters["hl"], tz=default_parameters["tz"])
data = pytrends.trending_searches(pn=default_parameters["pn"])
data.rename(columns={0:'text'}, inplace=True)
data = mySparkSession.sess(data)
# Parse DataFrame (data) - data'yı stringe dönüştür
dt = '{'
for data_id in data.index:
dt += '"{}": "{}",'.format(data_id, data.loc[data_id][0] + " - " + str(data.loc[data_id][1]))
dt = dt[:-1] + '}'
return json.loads(dt), 200
#dt = {}
#for data_id in data.index:
# dt[data_id] = data.loc[data_id][0] + " - " + data.loc[data_id][1]
#return json.dumps(dt), 200
# dt += '"{}": ("{}","{}"),'.format(data_id, data.loc[data_id][0], data[data_id][1])
#dt = dt[:-1] + '}'
# String convert to json and return - string veriyi json formatına çevir geri döndür
class RealtimeTrends(Resource):
def get(self):
f = open("default_parameters.json", "r")
default_parameters = json.loads(f.read())
f.close()
# hl: Makine dilini belirtir (tr-TR şeklinde) - host language
# tz: zaman dilimi - time zone
# pn: lokasyon - location (p-n)
pytrends = TrendReq(hl=default_parameters["hl"], tz=default_parameters["tz"])
data = pytrends.realtime_trending_searches(pn=default_parameters["hl"].split("-")[1])
# Parse DataFrame (data) - data'yı stringe dönüştür
dt = '{'
for data_id in data.index:
dt += '"{}": "{}",'.format(data_id, data.loc[data_id][0])
dt = dt[:-1] + '}'
# String convert to json and return - string veriyi json formatına çevir geri döndür
return json.loads(dt), 200
class DefaultParameters(Resource):
def get(self):
f = open("default_parameters.json", "r")
default_parameters = json.loads(f.read())
f.close()
return default_parameters, 200
def post(self):
f = open("default_parameters.json", "r")
default_parameters = json.loads(f.read())
f.close()
hl = request.args.get('hl')
tz = request.args.get('tz')
pn = request.args.get('pn')
if hl == null:
hl = default_parameters['hl']
if tz == null:
tz = default_parameters['tz']
if pn == null:
pn = default_parameters['pn']
default_parameters['hl'] = hl
default_parameters['tz'] = tz
default_parameters['pn'] = pn
with open("default_parameters.json", "w") as outfile:
json.dump(default_parameters, outfile)
outfile.close()
return default_parameters, 200
api.add_resource(Trends, '/trends')
api.add_resource(RealtimeTrends, '/realtime_trends')
api.add_resource(DefaultParameters, '/default_parameters')
if __name__ == '__main__':
app.run()