Merge pull request 'FIX news command.' (#19) from vinke into develop

Reviewed-on: http://vps41059.publiccloud.com.br:3000/neeble-club/neeble/pulls/19
pull/3/head
Kevin Caires 3 years ago
commit 1cf7b7c442

@ -5,7 +5,7 @@
█ ▄ █ ▄▄▄█ ▄▄▄█ ▄ ██ █▄▄▄█ ▄▄▄█
█ █ █ █ █▄▄▄█ █▄▄▄█ █▄█ █ █ █▄▄▄
█▄█ █▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█
Neeble version: 2022.09.01
Neeble version: 2022.09.06
You can help the development of neeble in
http://vps41059.publiccloud.com.br:3000/neeble-club/neeble
Thank you!

@ -100,5 +100,5 @@ IMAGE_TYPES = (
GOOGLE_NEWS = {
'url': os.environ.get('GOOGLE_NEWS_API'),
'token': os.environ.get('GOOGLE_NEWS_TOKEN'),
'source': 'google-news-br'
'sources': ['google-news-br', 'info-money', 'globo']
}

@ -314,22 +314,51 @@ async def machine_info(bot: object, *args: str) -> str:
@client.command(aliases=['nw'])
async def news(bot: object) -> None:
f"""
async def news(bot: object, *options: str) -> None:
"""
Return some news from Google.
options:
quantity: int
search: str
"""
_news = News(quantity=5)
news = _news.news()
embed = Embed(type='rich')
filter = {}
news = None
if not options:
_news = News(quantity=5)
news = _news.news()
else:
# Validate option operation.
if not all(['=' in op for op in options]):
return await bot.send('Blabla')
for op in options:
key, value = op.split('=')
filter[key] = value
_news = News(quantity=filter.get('quantity', 5))
news = _news.filter(phrase=filter.get('search'))
if not news:
return
for new in news:
# TODO: Descomentar o código do match case
dt = datetime.fromisoformat(
new['publishedAt']
).astimezone(pytz.timezone('America/Sao_Paulo'))
try:
dt = datetime.fromisoformat(
new['publishedAt']
).astimezone(pytz.timezone('America/Sao_Paulo'))
except ValueError:
dt = datetime.strptime(new['publishedAt'], '%Y-%m-%dT%H:%M:%Sz')
except Exception as e:
logger.error(e)
continue
embed.add_field(name='Font', value=new['source']['name'], inline=False)
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='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)
return await bot.send(f'**`News`**', embed=embed)

@ -10,16 +10,16 @@ class News:
Get the information in IBGE API.
"""
_url = f'{GOOGLE_NEWS["url"]}top-headlines?'\
f'sources={GOOGLE_NEWS["source"]}'\
f'sources={",".join(GOOGLE_NEWS["sources"])}'\
f'&apiKey={GOOGLE_NEWS["token"]}'
def __init__(self, quantity: int=5) -> None:
"""
Constructor.
"""
self.quantity = quantity
self.quantity = int(quantity)
def _get_and_resolve_news(self) -> list:
def news(self) -> list:
"""
Get the information based in self.quantity attribute.
"""
@ -32,8 +32,16 @@ class News:
return content['articles'][:self.quantity]
def news(self) -> list:
def filter(self, phrase: str) -> list:
"""
Get news in target based.
Filter content by keywords.
"""
return self._get_and_resolve_news()
_url = f'{self._url}&q={phrase if phrase else ""}'
_response = requests.get(url=_url)
content, status = _response.json(), _response.status_code
if not status == 200:
logger.error(content)
raise Exception(content)
return content['articles'][:self.quantity]

Loading…
Cancel
Save