Merge pull request #18 from KevinCaires/vinke

ADD command for bring 5 last google news.
pull/3/head
Kevin Caires 3 years ago committed by GitHub
commit 86986d8ff5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,6 +4,8 @@
export DISCORD_BOT_TOKEN=<>
export OPENWEATHER_API_TOKEN=<>
export GOOGLE_NEWS_API=<>
export GOOGLE_NEWS_TOKEN=<>
export MYSQL_HOST=<>
export MYSQL_PORT=<>
export MYSQL_DATABASE=<>

@ -85,8 +85,6 @@ OW_API_CONFIG = {
'wh_url' : 'https://api.openweathermap.org/data/2.5/weather?q=<CITY>&units=metric&appid=<API_ID>'
}
logger.dictConfig(LOGGING_CONFIG)
# Tuple of image type on image links.
# e.g: https://cdn.discordapp.com/attachments/720808802340962357/988542480981061702/cat.jpeg
# e.g: https://cdn.discordapp.com/attachments/720808802340962357/988542480981061702/unknow.png
@ -97,3 +95,10 @@ IMAGE_TYPES = (
'.mp4',
'.gif',
)
# FREE news API from GOOGLE.
GOOGLE_NEWS = {
'url': os.environ.get('GOOGLE_NEWS_API'),
'token': os.environ.get('GOOGLE_NEWS_TOKEN'),
'source': 'google-news-br'
}

@ -2,8 +2,10 @@
Bot commands.
"""
import logging
from datetime import datetime
from random import choice
import pytz
from discord import Embed, Intents
from discord.ext import commands
from settings.config import IMAGE_TYPES, OW_API_CONFIG, PERMISSIONS
@ -11,6 +13,7 @@ from settings.config import IMAGE_TYPES, OW_API_CONFIG, PERMISSIONS
from utils.database import (count_quotes, get_by_id, get_quote_contains,
get_quotes, remove_quote, set_quote)
from utils.machine_monitor import Monitor
from utils.news_paper import News
from utils.tools import kbytes_to_gbytes
from utils.weather import displayweather, getweatherdata
@ -308,3 +311,25 @@ async def machine_info(bot: object, *args: str) -> str:
await bot.send(f'**`{io}`**', embed=embed)
return
@client.command(aliases=['nw'])
async def news(bot: object) -> None:
f"""
Return some news from Google.
"""
_news = News(quantity=5)
news = _news.news()
embed = Embed(type='rich')
for new in news:
# TODO: Descomentar o código do match case
dt = datetime.fromisoformat(
new['publishedAt']
).astimezone(pytz.timezone('America/Sao_Paulo'))
embed.add_field(name='Published at', value=dt.isoformat(), inline=False)
embed.add_field(name='link', value=new['url'], inline=False)
embed.add_field(name=new['title'], value=new['description'], inline=False)
embed.add_field(name='---', value='---')
return await bot.send(f'**`{new["source"]["name"]}`**', embed=embed)

@ -0,0 +1,39 @@
import logging
import requests
from settings.config import GOOGLE_NEWS
logger = logging.getLogger(__name__)
class News:
"""
Get the information in IBGE API.
"""
_url = f'{GOOGLE_NEWS["url"]}top-headlines?'\
f'sources={GOOGLE_NEWS["source"]}'\
f'&apiKey={GOOGLE_NEWS["token"]}'
def __init__(self, quantity: int=5) -> None:
"""
Constructor.
"""
self.quantity = quantity
def _get_and_resolve_news(self) -> list:
"""
Get the information based in self.quantity attribute.
"""
_response = requests.get(url=self._url)
content, status = _response.json(), _response.status_code
if not status == 200:
logger.error(content)
raise Exception(content)
return content['articles'][:self.quantity]
def news(self) -> list:
"""
Get news in target based.
"""
return self._get_and_resolve_news()
Loading…
Cancel
Save