mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-27 12:40:35 +00:00
Add google api with mentions command to sentiment
This commit is contained in:
@@ -254,7 +254,7 @@ def main():
|
||||
|
||||
# SENTIMENT MARKET
|
||||
elif ns_known_args.opt == 'sen':
|
||||
b_quit = sm.sen_menu(s_ticker)
|
||||
b_quit = sm.sen_menu(s_ticker, s_start)
|
||||
|
||||
if b_quit:
|
||||
print("Hope you made money today. Good bye my lover, good bye my friend.\n")
|
||||
|
||||
101
sentiment/google_api.py
Normal file
101
sentiment/google_api.py
Normal file
@@ -0,0 +1,101 @@
|
||||
import pandas as pd
|
||||
from pytrends.request import TrendReq
|
||||
from datetime import datetime
|
||||
import matplotlib.pyplot as plt
|
||||
from helper_funcs import *
|
||||
import argparse
|
||||
|
||||
|
||||
# -------------------------------------------------------------------------------------------------------------------
|
||||
def mentions(l_args, s_ticker, s_start):
|
||||
parser = argparse.ArgumentParser(prog='mentions',
|
||||
description="""Plot weekly bars of stock's interest over time. other users watchlist.
|
||||
[Source: Google]""")
|
||||
|
||||
parser.add_argument('-s', "--start", type=valid_date, dest="s_start", default=s_start,
|
||||
help="starting date (format YYYY-MM-DD) from when we are interested in stock's mentions.")
|
||||
|
||||
#try:
|
||||
if 1:
|
||||
(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
|
||||
|
||||
pytrend = TrendReq()
|
||||
pytrend.build_payload(kw_list=[s_ticker])
|
||||
df_interest = pytrend.interest_over_time()
|
||||
|
||||
plt.title(f"Interest over time on {s_ticker}")
|
||||
if ns_parser.s_start:
|
||||
df_interest = df_interest[ns_parser.s_start:]
|
||||
plt.bar(df_interest.index, df_interest[s_ticker], width=2)
|
||||
plt.bar(df_interest.index[-1], df_interest[s_ticker].values[-1], color='tab:orange', width=2)
|
||||
else:
|
||||
plt.bar(df_interest.index, df_interest[s_ticker], width=1)
|
||||
plt.bar(df_interest.index[-1], df_interest[s_ticker].values[-1], color='tab:orange', width=1)
|
||||
|
||||
plt.grid(b=True, which='major', color='#666666', linestyle='-')
|
||||
plt.ylabel('Interest [%]')
|
||||
plt.xlabel("Time")
|
||||
plt.show()
|
||||
print("")
|
||||
|
||||
#except:
|
||||
# print("")
|
||||
|
||||
|
||||
# -------------------------------------------------------------------------------------------------------------------
|
||||
def regions(l_args):
|
||||
parser = argparse.ArgumentParser(prog='regions',
|
||||
description="""Print other users watchlist. [Source: Reddit]""")
|
||||
parser.add_argument('-l', "--limit", action="store", dest="n_limit", type=check_positive, default=5,
|
||||
help='limit of posts with watchlists retrieved.')
|
||||
|
||||
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
|
||||
|
||||
except:
|
||||
print("")
|
||||
|
||||
|
||||
# -------------------------------------------------------------------------------------------------------------------
|
||||
def queries(l_args):
|
||||
parser = argparse.ArgumentParser(prog='queries',
|
||||
description="""Print other users watchlist. [Source: Reddit]""")
|
||||
parser.add_argument('-l', "--limit", action="store", dest="n_limit", type=check_positive, default=5,
|
||||
help='limit of posts with watchlists retrieved.')
|
||||
|
||||
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
|
||||
|
||||
except:
|
||||
print("")
|
||||
|
||||
|
||||
# -------------------------------------------------------------------------------------------------------------------
|
||||
def rise(l_args):
|
||||
parser = argparse.ArgumentParser(prog='rise',
|
||||
description="""Print other users watchlist. [Source: Reddit]""")
|
||||
parser.add_argument('-l', "--limit", action="store", dest="n_limit", type=check_positive, default=5,
|
||||
help='limit of posts with watchlists retrieved.')
|
||||
|
||||
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
|
||||
|
||||
except:
|
||||
print("")
|
||||
|
||||
@@ -3,6 +3,7 @@ import argparse
|
||||
from sentiment import reddit_api
|
||||
from sentiment import stocktwits_api
|
||||
from sentiment import twitter_api
|
||||
from sentiment import google_api
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------------------------------------------------
|
||||
@@ -31,19 +32,22 @@ def print_sentiment():
|
||||
print(" infer infer about stock's sentiment from latest tweets")
|
||||
print(" sentiment in-depth sentiment prediction from tweets over time")
|
||||
print("")
|
||||
print("Google:")
|
||||
print(" mentions interest over time based on stock's mentions")
|
||||
print("")
|
||||
|
||||
return
|
||||
|
||||
|
||||
# ---------------------------------------------------- MENU ----------------------------------------------------
|
||||
def sen_menu(s_ticker):
|
||||
def sen_menu(s_ticker, s_start):
|
||||
|
||||
# Add list of arguments that the discovery parser accepts
|
||||
sen_parser = argparse.ArgumentParser(prog='sen', add_help=False)
|
||||
sen_parser.add_argument('cmd', choices=['help', 'q', 'quit',
|
||||
'watchlist', 'spac', 'spac_c', 'wsb', 'popular',
|
||||
'bullbear', 'messages', 'trending', 'stalker',
|
||||
'infer', 'sentiment'])
|
||||
'infer', 'sentiment', 'mentions'])
|
||||
|
||||
print_sentiment()
|
||||
|
||||
@@ -107,6 +111,10 @@ def sen_menu(s_ticker):
|
||||
elif ns_known_args.cmd == 'sentiment':
|
||||
twitter_api.sentiment(l_args, s_ticker)
|
||||
|
||||
# ----------------------------------------------------- GOOGLE ---------------------------------------------------
|
||||
elif ns_known_args.cmd == 'mentions':
|
||||
google_api.mentions(l_args, s_ticker, s_start)
|
||||
|
||||
# ------------------------------------------------------------------------------------------------------------
|
||||
else:
|
||||
print("Command not recognized!")
|
||||
Reference in New Issue
Block a user