YaakovY commited on
Commit
6253914
1 Parent(s): 0d9571b
Files changed (1) hide show
  1. main.py +110 -4
main.py CHANGED
@@ -1,6 +1,22 @@
1
  from fastapi import FastAPI
 
 
 
 
 
 
 
2
 
3
- # from typing import Union
 
 
 
 
 
 
 
 
 
4
 
5
  app = FastAPI()
6
 
@@ -10,6 +26,96 @@ def read_root():
10
  return {"Hello": "World!!!!"}
11
 
12
 
13
- @app.get("/ticker/{item_id}")
14
- def read_item(item_id: str):
15
- return {"item_id": item_id}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI
2
+ import requests
3
+ from telegram import ChatAction
4
+ import os
5
+ from urllib.request import urlopen, Request
6
+ from bs4 import BeautifulSoup
7
+ import pandas as pd
8
+ import json # for graph plotting in website
9
 
10
+ # NLTK VADER for sentiment analysis
11
+ import nltk
12
+
13
+ nltk.downloader.download("vader_lexicon")
14
+ from nltk.sentiment.vader import SentimentIntensityAnalyzer
15
+
16
+ import subprocess
17
+ import os
18
+
19
+ import datetime
20
 
21
  app = FastAPI()
22
 
 
26
  return {"Hello": "World!!!!"}
27
 
28
 
29
+ def get_news(ticker):
30
+ url = finviz_url + ticker
31
+ req = Request(
32
+ url=url,
33
+ headers={
34
+ "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0"
35
+ },
36
+ )
37
+ response = urlopen(req)
38
+ # Read the contents of the file into 'html'
39
+ html = BeautifulSoup(response)
40
+ # Find 'news-table' in the Soup and load it into 'news_table'
41
+ news_table = html.find(id="news-table")
42
+ return news_table
43
+
44
+
45
+ # parse news into dataframe
46
+ def parse_news(news_table):
47
+ parsed_news = []
48
+ today_string = datetime.datetime.today().strftime("%Y-%m-%d")
49
+
50
+ for x in news_table.findAll("tr"):
51
+ try:
52
+ # read the text from each tr tag into text
53
+ # get text from a only
54
+ text = x.a.get_text()
55
+ # splite text in the td tag into a list
56
+ date_scrape = x.td.text.split()
57
+ # if the length of 'date_scrape' is 1, load 'time' as the only element
58
+
59
+ if len(date_scrape) == 1:
60
+ time = date_scrape[0]
61
+
62
+ # else load 'date' as the 1st element and 'time' as the second
63
+ else:
64
+ date = date_scrape[0]
65
+ time = date_scrape[1]
66
+
67
+ # Append ticker, date, time and headline as a list to the 'parsed_news' list
68
+ parsed_news.append([date, time, text])
69
+ except:
70
+ pass
71
+
72
+ # Set column names
73
+ columns = ["date", "time", "headline"]
74
+ # Convert the parsed_news list into a DataFrame called 'parsed_and_scored_news'
75
+ parsed_news_df = pd.DataFrame(parsed_news, columns=columns)
76
+ # Create a pandas datetime object from the strings in 'date' and 'time' column
77
+ parsed_news_df["date"] = parsed_news_df["date"].replace("Today", today_string)
78
+ # parsed_news_df["datetime"] = pd.to_datetime(
79
+ # parsed_news_df["date"] + " " + parsed_news_df["time"],
80
+ # format="%Y-%m-%d %H:%M",
81
+ # )
82
+
83
+ return parsed_news_df
84
+
85
+
86
+ def score_news(parsed_news_df):
87
+ # Instantiate the sentiment intensity analyzer
88
+ vader = SentimentIntensityAnalyzer()
89
+
90
+ # Iterate through the headlines and get the polarity scores using vader
91
+ scores = parsed_news_df["headline"].apply(vader.polarity_scores).tolist()
92
+
93
+ # Convert the 'scores' list of dicts into a DataFrame
94
+ scores_df = pd.DataFrame(scores)
95
+
96
+ # Join the DataFrames of the news and the list of dicts
97
+ parsed_and_scored_news = parsed_news_df.join(scores_df, rsuffix="_right")
98
+ # parsed_and_scored_news = parsed_and_scored_news.set_index("datetime")
99
+ parsed_and_scored_news = parsed_and_scored_news.drop(["date", "time"], axis=1)
100
+ parsed_and_scored_news = parsed_and_scored_news.rename(
101
+ columns={"compound": "sentiment_score"}
102
+ )
103
+ return parsed_and_scored_news
104
+
105
+
106
+ # for extracting data from finviz
107
+ finviz_url = "https://finviz.com/quote.ashx?t="
108
+
109
+
110
+ def get_stock_data(ticker):
111
+ news_table = get_news(ticker)
112
+ parsed_news_df = parse_news(news_table)
113
+ parsed_and_scored_news = score_news(parsed_news_df)
114
+ return parsed_and_scored_news
115
+
116
+
117
+ @app.get("/ticker/{ticker}")
118
+ def read_item(ticker: str):
119
+ stock_data = get_stock_data(ticker)
120
+ result = stock_data.to_json(orient="columns")
121
+ return {"result": result}