-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
173 lines (155 loc) · 6.93 KB
/
script.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
import sys
import json
import time
import requests
import yfinance as yf
import secret as botAuth
import urllib.parse
from datetime import date
def retrieveChatId():
url = "https://api.telegram.org/bot" + botAuth.token + "/getUpdates"
response = requests.get(url).text
chatId = json.loads(response)["result"][-1]["message"]["chat"]["id"]
#print(response)
chatId = str(chatId)
return chatId
def getStock(stockCom):
#Validity checker for token
if len(botAuth.token) > 40:
botToken = botAuth.token
else:
print("Invalid Credential: Error in Token")
quit()
botChatId = retrieveChatId()
currency = ''
currPrice = 0
prevPrice = 0
#Getting the currency symbol for the company
stockCompany = yf.Ticker(stockCom)
#print(stockCompany)
try:
stockCompany_dict = stockCompany.info
except:
telegramMessage = "Wrong Keyword!\nTry Again with the proper keyword :("
telegramMessage = urllib.parse.quote_plus(telegramMessage)
url = "https://api.telegram.org/bot" + botToken + "/sendMessage?chat_id=" + botChatId + "&text=" + telegramMessage
#print(url)
response = requests.get(url)
print("Try Again with the proper keyword")
return
#print(stockCompany_dict)
stockCompany_json = json.dumps(stockCompany_dict)
#print(stockCompany_json)
stockCompany_json = json.loads(stockCompany_json)
#print(stockCompany_json)
company = stockCompany_json["longName"]
fiftyTwoWeekHigh = stockCompany_json["fiftyTwoWeekHigh"]
fiftyTwoWeekLow = stockCompany_json["fiftyTwoWeekLow"]
marketCap = stockCompany_json["marketCap"]
logoUrl = stockCompany_json["logo_url"]
#print(marketCap)
if stockCompany_json["currency"] != '':
#print(currency)
currency = stockCompany_json["currency"]
#downloading the daily stock data
stockCompany_dailyData = yf.download(stockCom,period="2d")
#print(stockCompany_dailyData["Close"][1])
if prevPrice == 0:
prevPrice = (stockCompany_dailyData["Close"][0]).round(2)
else:
prevPrice = currPrice
currPrice = (stockCompany_dailyData["Close"][1]).round(2)
if currPrice == prevPrice or prevPrice == 0:
priceChangeStatus = ''
priceChangeValue_Description = ''
elif currPrice > prevPrice:
priceChangeStatus = "UP🔺"
priceChangeValue = (currPrice - prevPrice).round(2)
priceChangePercentage = (priceChangeValue/prevPrice)*100
#print(priceChangeValue)
priceChangeValue_Description = "\n▫ *Status:* " + priceChangeStatus + "\n▫ *Value:* {} +".format(currency) + str("{0:.2f}".format(priceChangeValue)).replace(',', '\'') + "\n▫ *Percentage:* +{0:.2f}%".format(priceChangePercentage)
#print(priceChangeValue_Description)
else:
priceChangeStatus = "DOWN🔻"
priceChangeValue = (prevPrice - currPrice).round(2)
priceChangePercentage = (priceChangeValue/prevPrice)*100
#print(priceChangeValue)
priceChangeValue_Description = "\n▫ *Status:* " + priceChangeStatus + "\n▫ *Value:* {} -".format(currency) + str("{0:.2f}".format(priceChangeValue)).replace(',', '\'') + "\n▫ *Percentage:* -{0:.2f}%".format(priceChangePercentage)
#print(priceChangeValue_Description)
#Formatting MESSAGE
telegramMessage = "*Current status of requested stock for {0}({1}):*\n\n".format(company,stockCom)
telegramMessage += "▫ *Current Price:* " + currency + " " + str("{0:.2f}".format(currPrice)).replace(',', '\'') + " " + priceChangeValue_Description
telegramMessage += "\n▫ *Market Capital:* " + currency + " {0:,}".format(marketCap)
telegramMessage += "\n▫ *Fifty Two Week High:* {1} {0:.2f}".format(fiftyTwoWeekHigh,currency)
telegramMessage += "\n▫ *Fifty Two Week Low:* {1} {0:.2f}".format(fiftyTwoWeekLow,currency)
telegramMessage += "\n\n\n▫ *Logo:* " + logoUrl
#print(telegramMessage)
telegramMessage = urllib.parse.quote_plus(telegramMessage)
#print(telegramMessage)
url = "https://api.telegram.org/bot" + botToken + "/sendMessage?chat_id=" + botChatId + "&parse_mode=markdown&text=" + telegramMessage
#print(url)
response = requests.get(url)
#print(response)
def retrieveInput(botChatId):
"""
Pulls out the user input array
"""
retrieveUrl = "https://api.telegram.org/bot" + botAuth.token + "/getUpdates?timeout=5"
resIp = requests.get(retrieveUrl).text
resIp = json.loads(resIp)
resIp = resIp["result"][-1]["message"]
if(str(resIp["chat"]["id"])==botChatId):
try:
reqCom = resIp["text"][1:]
except:
reqCom = 'st'
if(reqCom.lower() == "start"):
reqCom = 'st'
elif(reqCom.lower() == "pause"):
reqCom = 'pause'
elif(reqCom.lower() == "end"):
reqCom = 'end'
return reqCom
def wait(botChatId):
checkStaticUrl = "https://api.telegram.org/bot" + botAuth.token + "/getUpdates"
lastMessage = requests.get(checkStaticUrl).text
lastMessage = json.loads(lastMessage)
lastMessage = lastMessage["result"][-1]["message"]
if(str(lastMessage["chat"]["id"])==botChatId):
msg = lastMessage["text"][1:]
if(msg.lower() == "pause"):
msg = 'pause'
elif(msg.lower() == "end"):
print("Exit request received.")
message = "Exit request received.\nBot has successfully shut down!"
url = "https://api.telegram.org/bot" + botAuth.token + "/sendMessage?chat_id=" + botChatId + "&disable_notification=true&text=" + message
#print(url)
response = requests.get(url)
quit()
return msg
while(True):
botChatId = retrieveChatId()
reqCom = retrieveInput(botChatId)
time.sleep(2)
if(reqCom == 'st'):
message = "Please enter the Symbol of the Company according to NYSE in format: */SYMBOL* where SYMBOL is symbol of the company!"
url = "https://api.telegram.org/bot" + botAuth.token + "/sendMessage?chat_id=" + botChatId + "&parse_mode=markdown&text=" + message
#print(url)
response = requests.get(url)
elif(reqCom == 'pause'):
message = "You have successfully stopped streaming updates!\nTo start streaming again, enter command /START"
url = "https://api.telegram.org/bot" + botAuth.token + "/sendMessage?chat_id=" + botChatId + "&disable_notification=true&text=" + message
#print(url)
response = requests.get(url)
while(reqCom == 'pause'):
reqCom = wait(botChatId)
elif(reqCom == 'end'):
print("Exit request received.")
message = "Exit request received.\nBot has successfully shut down!"
url = "https://api.telegram.org/bot" + botAuth.token + "/sendMessage?chat_id=" + botChatId + "&disable_notification=true&text=" + message
#print(url)
response = requests.get(url)
quit()
else:
getStock(reqCom)
time.sleep(2)