From a7261fab75a5cc5ea7f72a1aca1d00f96353c48e Mon Sep 17 00:00:00 2001 From: kevincaires Date: Sat, 3 Sep 2022 13:29:29 -0300 Subject: [PATCH 1/4] CHANGE google news sources. --- settings/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/settings/config.py b/settings/config.py index ab5aaf8..62bf23d 100644 --- a/settings/config.py +++ b/settings/config.py @@ -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'] } From 90a88c74c1ee895d16a682187475da514cd3dfe1 Mon Sep 17 00:00:00 2001 From: kevincaires Date: Sat, 3 Sep 2022 13:30:16 -0300 Subject: [PATCH 2/4] ADD filter for phrase method . --- utils/news_paper.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/utils/news_paper.py b/utils/news_paper.py index db6258b..a0c0297 100644 --- a/utils/news_paper.py +++ b/utils/news_paper.py @@ -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] From 148712f9a69adacc156469c5ea968e6fb1dd09f4 Mon Sep 17 00:00:00 2001 From: kevincaires Date: Sat, 3 Sep 2022 13:52:07 -0300 Subject: [PATCH 3/4] ADD options in news command. --- utils/commands.py | 49 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/utils/commands.py b/utils/commands.py index da7286d..3261b8a 100644 --- a/utils/commands.py +++ b/utils/commands.py @@ -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) From 5c59bcfadda23c35559bbae1a0235c36ea6e5dd7 Mon Sep 17 00:00:00 2001 From: "kevin.caires" Date: Tue, 6 Sep 2022 10:29:39 -0300 Subject: [PATCH 4/4] CHANGE version. --- motd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/motd b/motd index 2a1c95f..fa4498f 100644 --- a/motd +++ b/motd @@ -5,7 +5,7 @@ █ ▄ █ ▄▄▄█ ▄▄▄█ ▄ ██ █▄▄▄█ ▄▄▄█ █ █ █ █ █▄▄▄█ █▄▄▄█ █▄█ █ █ █▄▄▄ █▄█ █▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█ -Neeble version: 2022.09.01 +Neeble version: 2022.09.06 You can help the development of neeble in https://github.com/KevinCaires/neeble Thank you!