Skip to content

Commit

Permalink
Merge pull request #82 from lit26/fix/news-date
Browse files Browse the repository at this point in the history
Fix date
  • Loading branch information
lit26 authored Sep 11, 2023
2 parents d8e42cd + 7ce9ef8 commit cad5ecd
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
7 changes: 6 additions & 1 deletion finvizfinance/news.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ def _get_news_helper(self, rows):
source = link.split("/")[2]
if source == "feedproxy.google.com":
source = link.split("/")[4]
info_dict = {"Date": date, "Title": title, "Source": source, "Link": link}
info_dict = {
"Date": date,
"Title": title,
"Source": source,
"Link": link,
}
table.append(info_dict)
except TypeError:
# Empty news line
Expand Down
50 changes: 31 additions & 19 deletions finvizfinance/quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
.. moduleauthor:: Tianning Li <[email protected]>
"""
from datetime import datetime
from datetime import datetime, date
import json
import pandas as pd
import requests
from finvizfinance.util import web_scrap, image_scrap, number_covert, headers
from finvizfinance.util import (
web_scrap,
image_scrap,
number_covert,
headers,
format_datetime,
)

QUOTE_URL = "https://finviz.com/quote.ashx?t={ticker}"
NUM_COL = [
Expand Down Expand Up @@ -137,22 +143,22 @@ def ticker_fundament(self, raw=True, output_format="dict"):
fundament_info["Company"] = rows[1].text
row_split = rows[2].text.split(" | ")
fundament_info["Sector"] = row_split[0]
fundament_info['Industry'] = row_split[1]
fundament_info['Country'] = row_split[2]
fundament_info["Industry"] = row_split[1]
fundament_info["Country"] = row_split[2]
except IndexError:
try:
row_split = rows[0].text.split(' | ')
row_split = rows[0].text.split(" | ")
fundament_info["Company"] = row_split[1]
row_split = rows[1].text.split(" | ")
fundament_info["Sector"] = row_split[0]
fundament_info['Industry'] = row_split[1]
fundament_info['Country'] = row_split[2]
fundament_info["Industry"] = row_split[1]
fundament_info["Country"] = row_split[2]
except IndexError:
print('Cannot parse Company, Sector, Industry and Country')
fundament_info["Company"] = ''
fundament_info["Sector"] = ''
fundament_info["Industry"] = ''
fundament_info["Country"] = ''
print("Cannot parse Company, Sector, Industry and Country")
fundament_info["Company"] = ""
fundament_info["Sector"] = ""
fundament_info["Industry"] = ""
fundament_info["Country"] = ""

fundament_table = self.soup.find("table", class_="snapshot-table2")
rows = fundament_table.findAll("tr")
Expand Down Expand Up @@ -247,20 +253,24 @@ def ticker_outer_ratings(self):
try:
rows = fullview_ratings_outer.findAll("td", class_="fullview-ratings-inner")
if len(rows) == 0:
rows = fullview_ratings_outer.findAll('tr')[1:]
rows = fullview_ratings_outer.findAll("tr")[1:]
for row in rows:
each_row = row.find("tr")
if not each_row:
each_row = row
cols = each_row.findAll("td")
date = cols[0].text
date = datetime.strptime(date, "%b-%d-%y")
rating_date = cols[0].text
if rating_date.lower().startswith("today"):
rating_date = date.today()
else:
rating_date = datetime.strptime(rating_date, "%b-%d-%y")

status = cols[1].text
outer = cols[2].text
rating = cols[3].text
price = cols[4].text
info_dict = {
"Date": date,
"Date": rating_date,
"Status": status,
"Outer": outer,
"Rating": rating,
Expand All @@ -287,16 +297,18 @@ def ticker_news(self):
for row in rows:
try:
cols = row.findAll("td")
date = cols[0].text
news_date = cols[0].text
title = cols[1].a.text
link = cols[1].a["href"]
news_time = date.split()
news_time = news_date.split()
if len(news_time) == 2:
last_date = news_time[0]
news_time = " ".join(news_time)
else:
news_time = last_date + " " + news_time[0]
news_time = datetime.strptime(news_time, "%b-%d-%y %I:%M%p")

news_time = format_datetime(news_time)

info_dict = {"Date": news_time, "Title": title, "Link": link}
frame.append(info_dict)
except AttributeError:
Expand Down
16 changes: 16 additions & 0 deletions finvizfinance/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import requests
import pandas as pd
from bs4 import BeautifulSoup
from datetime import datetime, date

headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) \
Expand Down Expand Up @@ -213,6 +214,21 @@ def number_covert(num):
return float("".join(num.split(",")))


def format_datetime(date_str):
if date_str.lower().startswith("today"):
today = date.today()
time_str = date_str.split()[1]

hour, minute = map(int, time_str[:-2].split(":"))
ampm = time_str[-2:]

if ampm.lower() == "pm" and hour != 12:
hour += 12
return datetime(today.year, today.month, today.day, hour, minute)
else:
return datetime.strptime(date_str, "%b-%d-%y %I:%M%p")


def progress_bar(page, total):
bar_len = 30
filled_len = int(round(bar_len * page / float(total)))
Expand Down

0 comments on commit cad5ecd

Please sign in to comment.