-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
295 lines (246 loc) · 12.9 KB
/
main.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen
# from kivy.uix.widget import Widget
# from kivy.properties import ObjectProperty
from kivy.network.urlrequest import UrlRequest
# https://kivy.org/doc/stable/api-kivy.network.urlrequest.html
# import warnings
# warnings.filterwarnings("ignore")
# from ezekial import yahooprophet as yfp
# from matplotlib import pyplot as plt
# import time
############################################################
# import requests
# from dotenv import load_dotenv
import os
# import base64
import pybase64 as base64
# from pathlib import Path
from pathlib2 import Path
from PIL import Image as PILImage
from PIL import ImageFont, ImageDraw
# from PIL import ImageFont
# from PIL import ImageDraw
############################################################
# class save_ticker_image:
# def __init__(self, url, img_path, ticker, time):
# self.url = url
# self.img_path = img_path
# self.ticker = ticker
# self.time = time
# def django_get(self):
# response = requests.get(self.url)
# response_json = response.json()
# return response_json
# def select_ticker(self):
# django_api_dict = self.django_get()
# for dictionary in django_api_dict:
# try:
# if dictionary['ticker'] == self.ticker and dictionary['time'] == self.time:
# encoded_plot = dictionary['encoded_string']
# except: return print("Ticker Not Found in Forecast Database")
# # Decode string and save as a .png
# try:
# decodeit = open(self.img_path, 'wb')
# decodeit.write(base64.b64decode(encoded_plot[2:].encode()))
# decodeit.close()
# print("Image Saved Successfully")
# except: return print("Could Not Save Plot Ticker Not Found")
###############################################################################################
class save_ticker_image:
def __init__(self, url, img_path, ticker):
self.url = url
self.img_path = img_path
self.ticker = ticker
if self.ticker == '':
self.ticker = 'AMZN'
# DEPRICATED FROM kivy.network.urlrequest import UrlRequest
# def django_get(self):
# response = requests.get(self.url)
# response_json = response.json()
# return response_json
# def get_image_paths(path1, path2):
# return [path1, path2]
def select_ticker(req, result):
# def chart_generation(self, req, result):
try:
django_api_dict = result
# django_api_dict = self.django_get()
# except: print("URL GET FAIL")
for dictionary in django_api_dict:
try:
if dictionary['ticker'] == self.ticker:
encoded_plot = dictionary['encoded_string']
emoticons = dictionary['emoticons']
emoticons = emoticons.replace("'",'').replace(' 00:00:00', '').replace('[[', '').split('],')
emoticons = [emoticons[0].split(', '), emoticons[1].replace(' [', '').replace(']]', '').split(', ')]
# self.emoticons = emoticons
sentiment = dictionary['sentiment']
# self.sentiment = sentiment
except: print("Ticker Not Found in Forecast Database")
# pass
except: print("Kivy Request Fails")
# pass
# Decode string and save as a .png
try:
decodeit = open(self.img_path, 'wb')
decodeit.write(base64.b64decode(encoded_plot[2:].encode()))
decodeit.close()
print("Image Saved Successfully")
# forecast_image = Image(source='images/forecast_temp/decoded.png')
forecast_image = 'images/forecast_temp/decoded.png'
self.forecast_image = forecast_image
except:
print('Error saving plot')
# forecast_image = Image(source='images/forecast_temp/image_render_error.png')
forecast_image = 'images/forecast_temp/image_render_error.png'
self.forecast_image = forecast_image
# def emoticon_generation(self):
try:
# if sentiment != "NULL": sentiment = round(float(sentiment), 3)
# print(f'\n\tSENTIMENT_SCORE: {sentiment}\n{self.ticker} WEATHER FORECAST')
# for day in range(len(emoticons[1])):
# if emoticons[1][day] == '0':
# print(f'🌪Thunderstorms on {emoticons[0][day]}')
# elif emoticons[1][day] == '1':
# print(f'🌨Rainy on {emoticons[0][day]}')
# elif emoticons[1][day] == '1':
# print(f'🌞Sunny on {emoticons[0][day]}')
# else:
# print(f'🌈 on {emoticons[0][day]}')
emoticon_output_string = f'\n __________________________\n ||{ticker} WEATHER FORECAST ||\n ||Sentiment Score: {sentiment}||\n ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯'
emoticon_output_string += '\n ____________________________'
for day in range(len(emoticons[1])):
if emoticons[1][day] == '0':
emoticon_output_string += f'\n|\U0001f32a Stormy | {emoticons[0][day]}|'
emoticon_output_string += '\n ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯'
elif emoticons[1][day] == '1':
emoticon_output_string += f'\n|\U0001f326 Rainy | {emoticons[0][day]}|'
emoticon_output_string += '\n ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯'
elif emoticons[1][day] == '1':
emoticon_output_string += f'\n|\U0001f324 Overcast | {emoticons[0][day]}|'
emoticon_output_string += '\n ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯'
else:
emoticon_output_string += f'\n|\U0001f31e Sunny | {emoticons[0][day]}|'
emoticon_output_string += '\n ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯'
img = PILImage.new('RGB', (235, 500))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("images/forecast_temp/Symbola.ttf", 16)
draw.text((0, 0),emoticon_output_string,font=font)
img.save('images/forecast_temp/final_emoticon_report.png')
# emos = Image(source='images/forecast_temp/final_emoticon_report.png')
emos = 'images/forecast_temp/final_emoticon_report.png'
self.emos = emos
# print(f'\n\tEMOTICON VALUE: {type(emoticons)}, \n{emoticons[0]}\n\n{emoticons[1]}\n\nSENTIMENT_SCORE{sentiment}')
# return f'\n\tEMOTICON VALUE: {emoticons}, SENTIMENT_SCORE{sentiment}'
except:
print("Could Not Save Emoticon Report Ticker Not Found")
# emos = Image(source='images/forecast_temp/emoticon_render_error.png')
emos = 'images/forecast_temp/emoticon_render_error.png'
self.emos = emos
# get_image_paths(forecast_image, emos)
# return [forecast_image, emos]
req = UrlRequest(self.url, select_ticker)#.wait() # CHECK TO SEE IF IT NEEDS TO WAIT IF IT HAS A CONNECTIONS ERROR
###############################################################################################
class LoginScreen(GridLayout):
# class LoginScreen(Screen):
# class Floatlayout():
# class LoginScreen(Widget):
# # self.inside = Widget()
# ticker = ObjectProperty(None)
# start_date = ObjectProperty(None)
# days_to_forecast = ObjectProperty(None)
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.cols = 1
self.inside = GridLayout()
self.inside.cols = 2
wimg = Image(source='images/312fintech_ver04.png')
self.add_widget(wimg)
self.inside.add_widget(Label(text='Ticker: '))
self.ticker = TextInput(multiline=False)
self.inside.add_widget(self.ticker)
# self.inside.add_widget(Label(text='Time (Day, 60min): '))
# self.time = TextInput(multiline=False)
# self.inside.add_widget(self.time)
# self.inside.add_widget(Label(text='Start Data From: \n(YYYY-MM-DD)'))
# self.start_date = TextInput(multiline=False)
# self.inside.add_widget(self.start_date)
# self.inside.add_widget(Label(text='Days to Forecast: '))
# self.days_to_forecast = TextInput(multiline=False)
# self.inside.add_widget(self.days_to_forecast)
# Add inside as row 1/2
self.add_widget(self.inside)
self.submit = Button(text='Forecast', font_size=40)
self.submit.bind(on_press=self.press_forecast_button)
self.add_widget(self.submit)
#################################################################################################
def press_forecast_button(self, instance):
"""
When Forecast button is pressed, YahooProphet is called and returns forecast to terminal.
"""
############################################################################
### DJANGO API CALL ###
############################################################################
# USES .ENV FILE
# load_dotenv()
# url = os.getenv("DJANGO_SERVER_URL")
url = 'http://45.19.28.81:3555/ticker/'
forecast_decoded_img_path = Path('images/forecast_temp/decoded.png')
# save_ticker_image(url=url, img_path=forecast_decoded_img_path, ticker=self.ticker.text, time=self.time.text).select_ticker()
pimg = save_ticker_image(url=url, img_path=forecast_decoded_img_path, ticker=self.ticker.text.upper())#.select_ticker()
# time.sleep(.45)
# print(pimg)
# print(pimg.forecast_image, pimg.emos)
# pimg = Image(source='images/forecast_temp/decoded.png')
# self.add_widget(pimg)
try:self.add_widget(Image(source=pimg.forecast_image))
except:self.add_widget(Image(source='images/forecast_temp/image_render_error.png'))
try:self.add_widget(Image(source=pimg.emos))
except:self.add_widget(Image(source='images/forecast_temp/emoticon_render_error.png'))
################################################################################################
##ORIGINAL##
# print("Pressed Forecast Button")
# yfp_obj = yfp.YahooProphet(self.ticker.text, self.start_date.text, int(self.days_to_forecast.text))
# # print(yfp_obj.forecast_df().tail(int(self.days_to_forecast.text)))
# pimg = yfp_obj.plot()
# pimg.savefig('images/forecast.png')
# # time.sleep(3)
# pimg = Image(source='images/forecast.png')
# self.add_widget(pimg)
# ######################################
# ## CHECKING EZEKIAL DECODING METHOD ##
# ### TO BE REPLAECD WITH API CALL ###
# ######################################
# print("Pressed Forecast Button")
# yfp_obj = yfp.YahooProphet(self.ticker.text, self.start_date.text, int(self.days_to_forecast.text))
# ####################################################################
# yfp_obj.encode_plot()
# yfp_obj.decode_plot()
# ## Load decoded image
# # forecast_decoded_img_path = Path('images/forecast_temp/decoded.png')
# pimg = Image(source='images/forecast_temp/decoded.png')
# self.add_widget(pimg)
##################################################################################################
# class ForecastWindow(Screen):
# # def __init__(self, **kwargs):
# # super(LoginScreen, self).__init__(**kwargs)
# # wimg = Image(source='images/312fintech_ver04.png')
# # self.add_widget(wimg)
# pass
##################################################################################################
class MyApp(App):
def build(self):
# self.icon = 'myicon.png'
return LoginScreen()
# sm = ScreenManager()
# sm.add_widget(LoginScreen())
# sm.add_widget(ForecastWindow(name='forecast'))
# return sm
if __name__ == '__main__':
MyApp().run()