harden twitter rss forwarder with timeout, rotation, link-only posts

This commit is contained in:
esef committed 2026-07-16 12:26:32 +03:00
1 parent b7c11cf51a
commit 300f2fa8f0
4 files changed
+240 -104

No files matched your search

+47 -26
View File
@@ -3,39 +3,60 @@ import aiohttp
import feedparser
from discord.ext import commands
REQUEST_TIMEOUT = 15
class TwitterLeaks(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def leaks(self, ctx):
rss_url = os.getenv('TWITTER_RSS_URL')
if not rss_url:
primary = os.getenv('TWITTER_RSS_URL', '')
fallbacks_raw = os.getenv('TWITTER_RSS_FALLBACKS', '')
urls = [primary] if primary else []
if fallbacks_raw:
urls.extend(u.strip() for u in fallbacks_raw.split(',') if u.strip())
if not urls:
return await ctx.reply('rss url not configured.')
try:
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(rss_url) as resp:
text = await resp.text()
feed = feedparser.parse(text)
if not feed.entries:
return await ctx.reply('no tweets found.')
response = "**Latest 5 Leaks:**\n"
for entry in feed.entries[:5]:
title = entry.get('title', '')
link = entry.get('link', '')
if 'nitter.net' in link:
link = link.replace('nitter.net', 'twitter.com').replace('#m', '')
response += f"\n{title}\n{link}\n"
await ctx.reply(response[:2000])
except Exception as e:
await ctx.reply(f'failed to fetch leaks: {e}')
timeout = aiohttp.ClientTimeout(total=REQUEST_TIMEOUT)
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
async with aiohttp.ClientSession(timeout=timeout, headers=headers) as session:
entries = None
last_error = None
for url in urls:
try:
async with session.get(url) as resp:
resp.raise_for_status()
text = await resp.text()
feed = feedparser.parse(text)
if feed.entries:
entries = feed.entries
break
except Exception as e:
last_error = e
continue
if not entries:
msg = 'no tweets found.'
if last_error:
msg += f' (last error: {last_error})'
return await ctx.reply(msg[:2000])
response = "**Latest 5 Leaks:**\n"
for entry in entries[:5]:
link = entry.get('link', '')
if 'nitter.net' in link:
link = link.replace('nitter.net', 'twitter.com').replace('#m', '')
response += f'\n{link}\n'
await ctx.reply(response[:2000])
async def setup(bot):
await bot.add_cog(TwitterLeaks(bot))