Add stalker command to stocktwits sentiment menu

This commit is contained in:
didier
2021-02-07 02:27:18 +00:00
parent a2683078a2
commit ba08a67939
2 changed files with 36 additions and 3 deletions

View File

@@ -23,6 +23,7 @@ def print_sentiment():
print(" sentiment estimate quick sentiment from last 30 messages on board")
print(" messages output up to the 30 last messages on the board")
print(" trending trending stocks")
print(" stalker stalk stocktwits user's last messages")
print("")
return
@@ -35,7 +36,7 @@ def sen_menu(s_ticker):
sen_parser = argparse.ArgumentParser(prog='discovery', add_help=False)
sen_parser.add_argument('cmd', choices=['help', 'q', 'quit',
'watchlist', 'spac', 'spac_c', 'wsb', 'popular',
'sentiment', 'messages', 'trending'])
'sentiment', 'messages', 'trending', 'stalker'])
print_sentiment()
@@ -89,6 +90,9 @@ def sen_menu(s_ticker):
elif ns_known_args.cmd == 'trending':
stocktwits_api.trending(l_args)
elif ns_known_args.cmd == 'stalker':
stocktwits_api.stalker(l_args)
# ------------------------------------------------------------------------------------------------------------
else:
print("Command not recognized!")

View File

@@ -44,7 +44,7 @@ def sentiment(l_args, s_ticker):
# -------------------------------------------------------------------------------------------------------------------
def messages(l_args, s_ticker):
parser = argparse.ArgumentParser(prog='messages',
description="""Gather last 30 messages on the board [stocktwits] """)
description="""Print up to 30 of the last messages on the board [stocktwits] """)
parser.add_argument('-t', "--ticker", action="store", dest="s_ticker", type=str, default=s_ticker, help='Ticker to get board messages')
parser.add_argument('-l', "--limit", action="store", dest="n_lim", type=check_positive, default=30, help='Limit messages shown')
@@ -65,7 +65,6 @@ def messages(l_args, s_ticker):
break
else:
print("Invalid symbol")
print("")
except:
@@ -101,3 +100,33 @@ def trending(l_args):
print("")
# -------------------------------------------------------------------------------------------------------------------
def stalker(l_args):
parser = argparse.ArgumentParser(prog='stalker',
description="""Print up to the last 30 messages of a user [stocktwits] """)
parser.add_argument('-u', "--user", action="store", dest="s_user", type=str, required=True, help='Username')
parser.add_argument('-l', "--limit", action="store", dest="n_lim", type=check_positive, default=30, help='Limit messages shown')
try:
(ns_parser, l_unknown_args) = parser.parse_known_args(l_args)
if l_unknown_args:
print(f"The following args couldn't be interpreted: {l_unknown_args}\n")
return
result = requests.get(f"https://api.stocktwits.com/api/2/streams/user/{ns_parser.s_user}.json")
if result.status_code == 200:
for idx, message in enumerate(result.json()['messages']):
print(message['created_at'].replace('T', ' ').replace('Z', ''))
print(message['body'])
print("")
if idx > ns_parser.n_lim-1:
break
else:
print("Invalid user")
print("")
except:
print("")