-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockService.py
145 lines (124 loc) · 4.29 KB
/
StockService.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
#Service.py
import time
import yfinance as yf
from datetime import datetime, timedelta, date
import time
from cryptocmd import CmcScraper
import traceback
class StockService(object):
id = "Stock"
name = "💎 Stocks 💎"
welcome = "Welcome to Stocks 💎 Service \nWe get you stock information. Send us a stock to get info."
help = "send a message to get it back"
imageurl = "https://i.pinimg.com/564x/0e/98/e6/0e98e6518fa8b33dada3ed4104d04da3.jpg"
shortDescription = "Stock Stock Stock"
share = None
examples = {"AAPL":{"text":"","thumbnail":None}, "TSLA":{"text":"","thumbnail":None},"TSLA":{"btc":"","thumbnail":None}}
def __init__(self,db, api):
StockService.share = self
# print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
# print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
# print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
# print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Stock",StockService.share)
self.db = db
self.api = api
if "upcoming" not in self.db:
self.db["upcoming"] = []
if "users" not in self.db:
self.db["users"] = {}
def go(self):
while(True):
if "upcoming" not in self.db:
self.db["upcoming"] = []
if "users" not in self.db:
self.db["users"] = {}
while len(self.db["upcoming"]) > 0:
item = self.db["upcoming"].pop(0)
origin, content = item
self.api.send(origin, content, thumnail = "test")
# self.api.backup(self.db)
time.sleep(1)
def process(self, info):
origin, user, content = None, None, None
if "origin" in info:
origin = info["origin"]
if "user" in info:
user = info["user"]
if "content" in info:
content = info["content"]
if "users" not in self.db:
self.db["users"] = {}
self.api.send(origin, "Fetching Stock for: *"+content+"*")
# content = "AAPL"
today = date.today()
yesterday = datetime.now() - timedelta(1)
scraper = CmcScraper(content, yesterday.strftime("%d-%m-%Y"),today.strftime("%d-%m-%Y"))
# scraper = CmcScraper("btc", "28-1-2021","29-1-2021")
# headers, data = scraper.get_data()
# scraper.get_data("json")[1:-1]
# self.api.send(origin, "Could not fetch info for: "+content)
t = None
try:
t = yf.Ticker(content).info
except :
return self.api.send(origin, "Could not fetch info for: "+content)
res = {"dayHigh":"","dayLow":"", "open":"", "regularMarketPrice":"","volume":"","averageVolume10days":""}
if t is not None and content.lower() not in ["eth"]:
name = t["longName"]
for k in res:
res[k] = t[k]
res["price"] = res.pop("regularMarketPrice")
sendBack = str(res).replace("{","").replace("}","").replace(", ","\n").replace("'","")
logoURL = t["logo_url"]
sendBack+="\n"+logoURL
print (sendBack)
self.api.send(origin, "*"+name+"*\n"+sendBack,thumnail = {"imageurl":logoURL,"title":name+" Stock","desc":"Get More Stock Info","link":logoURL})
else:
data = None
try:
try:
headers, data = scraper.get_data()
except:
headers, data = scraper.get_data()
except:
traceback.print_exc()
if data is not None:
res = {}
ch = 0
for h in headers:
res[h] = data[-1][ch]
ch+=1
name = content.upper()
res.pop("Date")
sendBack = str(res).replace("{","").replace("}","").replace(", ","\n").replace("'","")
# self.api.send(origin, "*"+name+"*\n"+sendBack,thumnail = {"imageurl":logoURL,"title":name+" Crypto","desc":"Get More Stock Info","link":logoURL})
return self.api.send(origin, "*"+name+"*\n"+sendBack,thumnail = None)
else:
self.api.send(origin, "Could not fetch info for: "+content)
#
#
# sendBack = content
#
# withLink = True
# if withLink:
# answer = ":answerid:555"
# myLink = self.api.genLink(origin, answer)
# sendBack += "\n\n"+answer+":\n"+myLink
#
# self.db["upcoming"].append([origin, sendBack])
def backup(self):
self.api.backup(self.db)
def updateDB(self, db):
self.db = db
# self.db = User.jsonUsersToUsers(db)
def welcomeUser(self, origin):
print("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW")
print("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW")
print("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW")
print("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW")
if "users" not in self.db:
self.db["users"] = {}
if origin not in self.db["users"]:
self.db["users"][origin] = origin
self.backup()