-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: fetch telegram channel msg concurrently
- Loading branch information
Showing
2 changed files
with
76 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import json | ||
|
||
import aiohttp | ||
from loguru import logger | ||
|
||
from openagent.conf.env import settings | ||
|
||
|
||
async def fetch_tg_msgs(channel: str, limit: int = 10): | ||
""" | ||
Fetch recent content from a specific Telegram channel using RSS3 DATA API. | ||
:param channel: The Telegram channel to fetch content from | ||
:param limit: Number of recent items to fetch | ||
:return: A string containing the fetched items | ||
""" | ||
|
||
url = f"{settings.RSS3_DATA_API}/rss/telegram/channel/{channel}" | ||
logger.info(f"Fetching content from {url}") | ||
|
||
async with aiohttp.ClientSession() as session: # noqa | ||
async with session.get(url) as resp: | ||
if resp.status == 200: | ||
content = await resp.text() | ||
data = json.loads(content) | ||
return data["data"][:limit] | ||
else: | ||
logger.error(f"Failed to fetch from {url}. Status: {resp.status}") | ||
|
||
|
||
if __name__ == "__main__": | ||
import asyncio | ||
|
||
loop = asyncio.get_event_loop() | ||
entries = loop.run_until_complete(fetch_tg_msgs("ChannelPANews", 5)) | ||
print(entries) |