-
Notifications
You must be signed in to change notification settings - Fork 14
/
resource.py
163 lines (135 loc) · 6.11 KB
/
resource.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from flask import make_response, jsonify
from flask_restful import reqparse, Resource
import finviz
import stocktwits
import zacks
class API_Resource(Resource):
"""
This is the base class for all resources in the API which extends Resource class from flask_restful module.
It contains a few static utility methods, class variables as well as common instance variables.
"""
errors = {500: {"Error 500": "Could not get data"},
404: {"Error 404": "Could not find data"}
}
def __init__(self):
super().__init__()
self.parser = reqparse.RequestParser()
@staticmethod
def error(status_code=500):
"""
Utility method to return various error responses based on HTTP status code
:param status_code: HTTP status code. Default is 500
:return: a HTTP response of the error with a corresponding JSON message payload
"""
return make_response(jsonify(API_Resource.errors[status_code]), status_code)
@staticmethod
def json(response):
"""
Utility method to create JSON payloads of dictionaries and return them as a HTTP response
:param response:
:return:
"""
if type(response) is dict:
return make_response(jsonify(response), 200)
else:
return API_Resource.error()
class Finviz(API_Resource):
"""
API Resource for the finviz website. This uses the finviz scrapper python script.
"""
def __init__(self):
super().__init__()
self.parser.add_argument("fields")
def get(self, service, ticker_symbol):
"""
Handles all GET request for the RESTful API
:param service: the name of the interested service
:param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GOOG", "MSFT")
:return: returns a 200 HTTP response along with JSON payload, otherwise returns a 404 response
"""
args = self.parser.parse_args()
response = API_Resource.error(404)
if service == "statistics":
response = self.get_statistics(ticker_symbol, args)
return response
def get_statistics(self, ticker_symbol, args):
"""
Helper function for the statistics service
:param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GOOG", "MSFT")
:param args: Optional arguments in the REST call
:return: a 200 HTTP response with JSON statistics payload of statistics, other a 404 HTTP response
"""
if args["fields"]:
response = {}
for arg in args["fields"].split(","):
stat = finviz.get_statistic(ticker_symbol, arg)
if stat:
response.update({arg: stat})
return API_Resource.json(response) if response else API_Resource.error(404)
else:
response = finviz.get_all_statistics(ticker_symbol)
return API_Resource.json(response) if response else API_Resource.error(404)
class Stocktwits(API_Resource):
"""
API resource for the StockTwits website. This uses the stocktwits scrapper python script
"""
def __init__(self):
super().__init__()
def get(self, service, ticker_symbol):
"""
Handles all GET request for the RESTful API
:param service: the name of the interested service
:param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GOOG", "MSFT")
:return: returns a 200 HTTP response along with JSON payload, otherwise returns a 404 response
"""
response = API_Resource.error(404)
if service == "sentiment":
response = self.get_sentiment(ticker_symbol)
return response
def get_sentiment(self, ticker_symbol):
"""
Helper function for the sentiment service
:param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GOOG", "MSFT")
:return: returns a 200 HTTP response along with JSON sentiment payload, otherwise returns a 404 response
"""
response = {}
response["bullish"] = stocktwits.get_bullish_sentiment(ticker_symbol)
response["bearish"] = stocktwits.get_bearish_sentiment(ticker_symbol)
return API_Resource.json(response) if response["bullish"] and response["bearish"] else API_Resource.error(404)
class Zacks(API_Resource):
"""
API resource for the zacks website. This uses the zacks scrapper python script
"""
def __init__(self):
super().__init__()
def get(self, service, ticker_symbol):
"""
Handles all GET request for the RESTful API
:param service: the name of the interested service
:param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GOOG", "MSFT")
:return: returns a 200 HTTP response along with JSON payload, otherwise returns a 404 response
"""
response = API_Resource.error(404)
if service == "rating":
response = self.get_rating(ticker_symbol)
elif service == "peers":
response = self.get_peers(ticker_symbol)
return response
def get_rating(self, ticker_symbol):
"""
Helper function for the rating service
:param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GOOG", "MSFT")
:return: returns a 200 HTTP response along with JSON rating payload, otherwise returns a 404 response
"""
response = {}
response["rating"] = zacks.get_rating(ticker_symbol)
return API_Resource.json(response) if response["rating"] else API_Resource.error(404)
def get_peers(self, ticker_symbol):
"""
Helper function for the rating service
:param ticker_symbol: The ticker symbol of the interested stock (e.g., "AAPL", "GOOG", "MSFT")
:return: returns a 200 HTTP response along with JSON peers payload, otherwise returns a 404 response
"""
response = {}
response["peers"] = zacks.get_peers(ticker_symbol)
return API_Resource.json(response) if response["peers"] else API_Resource.error(404)