|
|
@ -1,6 +1,10 @@
|
|
|
|
import logging
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import requests
|
|
|
|
from settings.config import GOOGLE_NEWS
|
|
|
|
from models.news import ViewedNews
|
|
|
|
|
|
|
|
from settings.config import GOOGLE_NEWS, SQLACHEMY
|
|
|
|
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
@ -9,7 +13,7 @@ class News:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Get the information in IBGE API.
|
|
|
|
Get the information in IBGE API.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
_url = f'{GOOGLE_NEWS["url"]}top-headlines?'\
|
|
|
|
_url = f'{GOOGLE_NEWS["url"]}everything?'\
|
|
|
|
f'sources={",".join(GOOGLE_NEWS["sources"])}'\
|
|
|
|
f'sources={",".join(GOOGLE_NEWS["sources"])}'\
|
|
|
|
f'&apiKey={GOOGLE_NEWS["token"]}'
|
|
|
|
f'&apiKey={GOOGLE_NEWS["token"]}'
|
|
|
|
|
|
|
|
|
|
|
@ -19,6 +23,46 @@ class News:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
self.quantity = int(quantity)
|
|
|
|
self.quantity = int(quantity)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _date_convert(self, _date) -> datetime:
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Convert new datetime.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
_date = datetime.fromisoformat(_date)
|
|
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
|
|
_date = datetime.strptime(_date, '%Y-%m-%dT%H:%M:%Sz')
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
logger.error(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return _date
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _remove_viewed_news(self, articles: list) -> list:
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Remove the viewed news.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
with Session(SQLACHEMY) as session:
|
|
|
|
|
|
|
|
for idx, new in enumerate(articles):
|
|
|
|
|
|
|
|
viewed = session.query(ViewedNews).filter(
|
|
|
|
|
|
|
|
ViewedNews.title==new['title'],
|
|
|
|
|
|
|
|
ViewedNews.published_at==self._date_convert(new['publishedAt'])
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if list(viewed):
|
|
|
|
|
|
|
|
articles.pop(idx)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return articles
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _set_viewed_news(self, articles: list) -> None:
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Set viewed news in data base.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
with Session(SQLACHEMY) as session:
|
|
|
|
|
|
|
|
for article in articles:
|
|
|
|
|
|
|
|
_date = self._date_convert(article['publishedAt'])
|
|
|
|
|
|
|
|
new = ViewedNews(title=article['title'], published_at=_date.date())
|
|
|
|
|
|
|
|
session.add(new)
|
|
|
|
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
def news(self) -> list:
|
|
|
|
def news(self) -> list:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Get the information based in self.quantity attribute.
|
|
|
|
Get the information based in self.quantity attribute.
|
|
|
@ -30,7 +74,12 @@ class News:
|
|
|
|
logger.error(content)
|
|
|
|
logger.error(content)
|
|
|
|
raise Exception(content)
|
|
|
|
raise Exception(content)
|
|
|
|
|
|
|
|
|
|
|
|
return content['articles'][:self.quantity]
|
|
|
|
# Remove viewed news.
|
|
|
|
|
|
|
|
response = self._remove_viewed_news(content['articles'])[:self.quantity]
|
|
|
|
|
|
|
|
# Save news into a blacklist.
|
|
|
|
|
|
|
|
self._set_viewed_news(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
def filter(self, phrase: str) -> list:
|
|
|
|
def filter(self, phrase: str) -> list:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|