Skip to content

Commit

Permalink
cleaning code
Browse files Browse the repository at this point in the history
  • Loading branch information
wilberh committed Aug 9, 2023
1 parent a9a6f50 commit 23da36b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
60 changes: 30 additions & 30 deletions core/api/trending.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import sys
import traceback
import requests
import os
import pandas as pd
import numpy as np
import nltk
nltk.download('vader_lexicon')
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import yfinance as yf

from datetime import date

from newsapi import NewsApiClient

from pytrends.request import TrendReq
from google.cloud import bigquery

import pandas as pd
import numpy as np

import yfinance as yf

from datetime import date
import sys, traceback
import requests
import os
nltk.download('vader_lexicon')


NEWSAPI_KEY = os.getenv('NEWSAPI_KEY')
Expand All @@ -42,11 +42,11 @@ def get_stock_price(stock, strt_dt, end_dt):
# stock = ['^GSPC','AAPL','MSFT','INTC']
"""
try:
SP = yf.download(stock,start=strt_dt,end=end_dt,interval='1d')
SP = yf.download(stock, start=strt_dt, end=end_dt, interval='1d')

if not isinstance(SP, pd.DataFrame):
return {"error": "Bad response from API"}

SP.index = SP.index.strftime('%Y%m%d')
SP_close = SP.Close

Expand All @@ -73,8 +73,8 @@ def trending_by_country(company, country, strt_dt, end_dt):
Download end date string (YYYY-MM-DD) or _datetime, exclusive.
Default is now
https://github.com/cjhutto/vaderSentiment
VADER (Valence Aware Dictionary and sEntiment Reasoner) is a lexicon and
rule-based sentiment analysis tool that is specifically attuned to sentiments
VADER (Valence Aware Dictionary and sEntiment Reasoner) is a lexicon and
rule-based sentiment analysis tool that is specifically attuned to sentiments
expressed in social media. It is fully open-sourced.
NLTK provides us with a VADER implementation for easy use -
Expand All @@ -92,22 +92,23 @@ def trending_by_country(company, country, strt_dt, end_dt):
sources = newsapi.get_sources()
sources_records = sources['sources']
sources_df = pd.DataFrame.from_records(sources_records)
sources_country = sources_df[(sources_df['language'] == language)
& (sources_df['category'] == 'business')
& (sources_df['country'] == country)]
sources_country = sources_df[(sources_df['language'] == language) &
(sources_df['category'] == 'business') &
(sources_df['country'] == country)]
# 20 max sources allowed by api
max_sources_api = ','.join(list(sources_country['id'].iloc[:20]))

page = 1
params = {'q': company,
'sources': max_sources_api,
'from': strt_dt,
'to': end_dt,
'language': language,
'searchIn': 'title',
'sortBy': 'relevancy',
'page': page
}
params = {
'q': company,
'sources': max_sources_api,
'from': strt_dt,
'to': end_dt,
'language': language,
'searchIn': 'title',
'sortBy': 'relevancy',
'page': page
}
headers = {'Authorization': f'Bearer {NEWSAPI_KEY}'}
total_results = 1
total_per_pages = 0
Expand Down Expand Up @@ -138,11 +139,10 @@ def trending_by_country(company, country, strt_dt, end_dt):
'title': title,
'compound': score['compound']})
titles_sentiment = pd.concat([titles_sentiment,
new_row.to_frame().T],
ignore_index=True)
new_row.to_frame().T], ignore_index=True)

titles_sentiment.drop_duplicates(subset=['date', 'source', 'title'])
return titles_sentiment.to_dict('records')
except Exception as ex:
formatted_lines = traceback.format_exc().splitlines()
return {"error": formatted_lines[0]+' \n'+formatted_lines[-1]}
return {"error": formatted_lines[0]+' \n'+formatted_lines[-1]}
16 changes: 9 additions & 7 deletions core/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@
class GoogleTrendApiDetail(APIView):
def post(self, request):
data = request.data
# Example of API body/payload:
# {"task": "quote", "ticker": "AAPL" [OPTIONAL: ,"strt_dt": "2023-06-29", "end_dt": "2023-07-28"]}
# {"task": "trend", "company": "Apple", "country": "us" [OPTIONAL: ,"strt_dt", "2023-06-29", "end_dt": "2023-07-28"]}

"""
Example of API body/payload:
{"task": "quote", "ticker": "AAPL"
[OPTIONAL: ,"strt_dt": "2023-06-29", "end_dt": "2023-07-28"]}
{"task": "trend", "company": "Apple", "country": "us"
[OPTIONAL: ,"strt_dt", "2023-06-29", "end_dt": "2023-07-28"]}
"""
task = data["task"]

# NOTE: Hardcoded start and end dates to qualify for free-tier NewsAPI access (last 30 days)
end_dt = date.today()
strt_dt = end_dt - timedelta(days=29)
strt_dt = data.get("strt_dt", strt_dt.isoformat())
end_dt = data.get("end_dt", end_dt.isoformat())
end_dt = data.get("end_dt", end_dt.isoformat())

if task == "quote":
sp_close = get_stock_price(data["ticker"], strt_dt, end_dt)
Expand All @@ -41,7 +44,6 @@ def post(self, request):
else:
return Response({"message": "Something went wrong"}, status=status.HTTP_400_BAD_REQUEST)


# def Index(request):
# return HttpResponse("AIFinance backend app page")
# # raise Http404("Poll does not exist")
# # raise Http404("Poll does not exist")

0 comments on commit 23da36b

Please sign in to comment.