Path: blob/master/ invest-robot-contest_tinvest_robot-master/tinvest_robot_perevalov/news_fetcher.py
5932 views
import feedparser12from tinvest_robot_perevalov import _db3from tinvest_robot_perevalov.sentiment_analyzer import SentimentAnalyzer45import csv6import urllib.request7import os89import logging1011logging.basicConfig(12format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO13)1415logger = logging.getLogger(__name__)1617def _init_sentiment_analyzer() -> SentimentAnalyzer:18"""Internal method for initializing sentiment analyzer. To be extended in the future.1920Returns:21SentimentAnalyzer: Initialized sentiment analyzer22"""23MODEL = os.getenv('SENTIMENT_MODEL') or "cardiffnlp/twitter-roberta-base-sentiment"24THRESHOLD = 0.12526logger.info("Downloading labels...")2728labels=[]29mapping_link = "https://raw.githubusercontent.com/cardiffnlp/tweeteval/main/datasets/sentiment/mapping.txt"30with urllib.request.urlopen(mapping_link) as f:31html = f.read().decode('utf-8').split("\n")32csvreader = csv.reader(html, delimiter='\t')33labels = [row[1] for row in csvreader if len(row) > 1]3435logger.info("Initializing sentiment analyzer...")3637return SentimentAnalyzer(MODEL, THRESHOLD, labels)3839def fetch_and_analyze(rss_feeds: list):40"""41Fetch news from RSS feeds, analyze sentiment, and save to database4243Args:44rss_feeds (list): list of RSS feeds URLs to fetch45"""46_db.init_db()47sentiment_analyzer = _init_sentiment_analyzer()4849for feed in rss_feeds:50entries = feedparser.parse(feed).entries51for entry in entries:52if not _db.check_if_exists(entry.title):53sentiment = sentiment_analyzer.predict_sentiment(entry.title)54_db.put_in_db(entry.title, sentiment)55logger.info(f"TEXT: {entry.title} // SENTIMENT: {sentiment}")565758