-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
191 lines (156 loc) · 6.69 KB
/
app.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
import tweepy
import requests
import plotly.graph_objects as go
from PIL import Image, ImageDraw, ImageFont
import json
import schedule
import time
import urllib.request
def tweet_tweet(text, path):
print('----------------tweet_tweet------------ start ')
consumer_key = "AIz32xSHvAE81cdhkJWvWNGLx"
consumer_secret = "XHDCuCiAJHwEiCoaqvWTBG9R6Kf9cuKpyjvyWjI1ep5P2DKX0k"
access_key = "1446841040162283526-7yWa67GwRhKHf8Ak5cA6zrc3TI6o9d"
access_secret = "Lz8tMPuWrV3lxhQ9ZKzXZX6MjOtzhfLb9ToUqq8LkbJUD"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
tweet = text
image_path = path
# to attach the media file
status = api.update_with_media(image_path, tweet)
try:
api.update_status(status=status)
except ValueError:
print("tweet_tweet exception")
print('----------------tweet_tweet------------ end ')
def fear_greed_today():
print('----------------fear_greed_today------------ start ')
url = "https://fear-and-greed-index.p.rapidapi.com/v1/fgi"
headers = {
'x-rapidapi-host': "fear-and-greed-index.p.rapidapi.com",
'x-rapidapi-key': "ba520daa9cmshd7ba898f17af955p186046jsn44a6770d4b89"
}
response = requests.request("GET", url, headers=headers)
res = json.loads(response.text)
print(res)
title = res["fgi"]["now"]["valueText"]
value = res["fgi"]["now"]["value"]
if value > 25 and value <= 50:
color = "darkred"
elif value > 50 and value <= 75:
color = "lightgreen"
elif value > 75 and value <= 100:
color = "green"
else:
color = "yellow"
fig = go.Figure(go.Indicator(
mode="gauge+number",
value=value,
domain={'x': [0, 1], 'y': [0, 1]},
title={'text': title},
gauge={'axis': {'range': [None, 100]},
'bar': {'color': color}}))
fig.update_layout(title="https://twitter.com/MarketzToday", )
fig.write_image("fig1.png")
print('----------------fear_greed_today------------ end ')
tweet_tweet('Equities Fear/Greed today #MarketzToday', 'fig1.png')
def markets_today_us():
print('----------------markets_today_us------------ start ')
region = "US"
url = "https://yfapi.net/v6/finance/quote/marketSummary?lang=en®ion="+region
headers = {'x-api-key': "l7FKFiZ86C7kTcBoszU1c9sAKjeq2uLX4rK0jtuq"}
response = requests.request("GET", url, headers=headers)
res = json.loads(response.text)
print(res)
sp = res['marketSummaryResponse']['result'][0]
dji = res['marketSummaryResponse']['result'][1]
nasdaq = res['marketSummaryResponse']['result'][2]
data = [sp, dji, nasdaq]
new = Image.new("RGB", (900, 450)) # , color=(255,255,255,0))
up = Image.open('up.png')
down = Image.open('down.png')
font = ImageFont.truetype("arial.ttf", 32)
draw = ImageDraw.Draw(new)
count = 1
for i in data:
name = i['shortName']
current = i['regularMarketPrice']['fmt']
change = i['regularMarketChange']['fmt']
change_percent = i['regularMarketChangePercent']['fmt']
if float(change) >= 0:
new.paste(up, (150, count*100))
else:
new.paste(down, (150, count*100))
draw.text((200, count*100), name, (255, 255, 255), font=font)
draw.text((400, count*100), current, (255, 255, 255), font=font)
draw.text((650, count*100), change_percent, (255, 255, 255), font=font)
count = count + 1
font = ImageFont.truetype("arial.ttf", 16)
draw.text((550, 400), 'https://twitter.com/MarketzToday',
(255, 255, 255), font=font)
new.save('fig2.png')
print('----------------markets_today_us------------ end ')
tweet_tweet('US market indices today #MarketzToday', 'fig2.png')
def markets_today_in():
print('----------------markets_today_in------------ start ')
region = "IN"
url = "https://yfapi.net/v6/finance/quote/marketSummary?lang=en®ion="+region
headers = {'x-api-key': "l7FKFiZ86C7kTcBoszU1c9sAKjeq2uLX4rK0jtuq"}
response = requests.request("GET", url, headers=headers)
res = json.loads(response.text)
print(res)
bse = res['marketSummaryResponse']['result'][0]
nse = res['marketSummaryResponse']['result'][1]
data = [bse, nse]
new = Image.new("RGB", (900, 450)) # , color=(255,255,255,0))
up = Image.open('up.png')
down = Image.open('down.png')
font = ImageFont.truetype("arial.ttf", 32)
draw = ImageDraw.Draw(new)
count = 1
for i in data:
name = i['fullExchangeName']
current = i['regularMarketPrice']['fmt']
change = i['regularMarketChange']['fmt']
change_percent = i['regularMarketChangePercent']['fmt']
if float(change) >= 0:
new.paste(up, (150, count*120))
else:
new.paste(down, (150, count*120))
draw.text((200, count*120), name, (255, 255, 255), font=font)
draw.text((400, count*120), current, (255, 255, 255), font=font)
draw.text((650, count*120), change_percent, (255, 255, 255), font=font)
count = count + 1
font = ImageFont.truetype("arial.ttf", 16)
draw.text((550, 400), 'https://twitter.com/MarketzToday',
(255, 255, 255), font=font)
new.save('fig2.png')
print('----------------markets_today_in------------ end ')
tweet_tweet('Indian market indices today #MarketzToday', 'fig2.png')
def bitcoin_today():
print('----------------bitcoin_today------------ start ')
urllib.request.urlretrieve(
"https://alternative.me/crypto/fear-and-greed-index.png", "fig3.png")
print('----------------bitcoin_today------------ end ')
tweet_tweet('Bitcoin Fear/Greed today #MarketzToday', 'fig3.png')
schedule.every().day.at("00:00").do(bitcoin_today)
schedule.every().monday.at("10:30").do(markets_today_in)
schedule.every().monday.at("13:00").do(fear_greed_today)
schedule.every().monday.at("20:30").do(markets_today_us)
schedule.every().tuesday.at("10:30").do(markets_today_in)
schedule.every().tuesday.at("13:00").do(fear_greed_today)
schedule.every().tuesday.at("20:30").do(markets_today_us)
schedule.every().wednesday.at("10:30").do(markets_today_in)
schedule.every().wednesday.at("13:00").do(fear_greed_today)
schedule.every().wednesday.at("20:30").do(markets_today_us)
schedule.every().thursday.at("10:30").do(markets_today_in)
schedule.every().thursday.at("13:00").do(fear_greed_today)
schedule.every().thursday.at("20:30").do(markets_today_us)
schedule.every().friday.at("10:30").do(markets_today_in)
schedule.every().friday.at("13:00").do(fear_greed_today)
schedule.every().friday.at("20:30").do(markets_today_us)
while 1:
print('running')
schedule.run_pending()
time.sleep(1)