-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrating.py
executable file
·95 lines (86 loc) · 2.86 KB
/
rating.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
#!/usr/bin/env python3
import json, re, sys
from lib.config import *
from lib import sharesight
from lib import webhook
from lib import util
from lib import yahoo
def lambda_handler(chat_id=config_telegramChatID, specific_stock=None, service=None, interactive=False):
def prepare_rating_payload(service, market_data):
def get_emoji(old, new):
if old > new:
return '📈'
if old < new:
return '📉'
else:
return '▪️'
payload = []
new = {}
old = util.json_load('finbot_rating.json', persist=True)
for ticker in tickers:
old_action=None
old_index = float()
try:
action = market_data[ticker]['recommend'].replace('_', ' ')
index = round(float(market_data[ticker]['recommend_index']), 1)
analysts = market_data[ticker]['recommend_analysts']
except (KeyError, ValueError):
continue
title = market_data[ticker]['profile_title']
ticker_link = util.finance_link(ticker, market_data[ticker]['profile_exchange'], service, brief=False)
new[ticker] = [action, index]
try:
old_action = old[ticker][0]
old_index = old[ticker][1]
except (KeyError, TypeError):
pass
if old_action and action != old_action:
emoji = get_emoji(old_index, index)
message = f"{webhook.bold(f'{old_index} {old_action}', service)} to {webhook.bold(f'{index} {action}', service)} ({analysts} analysts)"
payload.append(f"{emoji} {title} ({ticker_link}) changed from {message}")
payload.sort()
if payload:
if not specific_stock:
message = f'Consensus rating changes:'
message = webhook.bold(message, service)
payload.insert(0, message)
else:
if interactive:
if specific_stock:
payload = [f"{emoji}No rating changes found for {tickers[0]}"]
else:
payload = [f"{emoji}No rating changes found"]
return payload, new
# MAIN #
market_data = {}
if specific_stock:
ticker = util.transform_to_yahoo(specific_stock)
tickers = [ticker]
else:
tickers = util.get_holdings_and_watchlist()
for ticker in tickers:
market_data = market_data | yahoo.fetch_detail(ticker)
# Prep and send payloads
if not webhooks:
print("Error: no services enabled in .env", file=sys.stderr)
sys.exit(1)
if interactive:
payload, new = prepare_rating_payload(service, market_data)
url = webhooks[service]
if service == "slack":
url = 'https://slack.com/api/chat.postMessage'
elif service == "telegram":
url = url + "sendMessage?chat_id=" + str(chat_id)
webhook.payload_wrapper(service, url, payload, chat_id)
else:
for service, url in webhooks.items():
payload, new = prepare_rating_payload(service, market_data)
if service == "telegram":
url = url + "sendMessage?chat_id=" + str(chat_id)
webhook.payload_wrapper(service, url, payload, chat_id)
if new:
util.json_write('finbot_rating.json', new, persist=True)
# make google cloud happy
return True
if __name__ == "__main__":
lambda_handler()