The RSSHub feed isn't strictly chronological, so 'post everything above the last guid' could skip tweets. Now a single poller fans out to both webhooks, each remembering posted guids (data/twitter_seen.json, atomic writes): new tweets anywhere above the oldest known one are posted oldest-first, old tweets sliding in after a deletion are ignored, failed posts retry next poll and Discord 429s are honoured. Links go out as fxtwitter.com for real embeds. The owner gets a DM after 90 min of broken feed and again on recovery. Night polling is now 1-2 h. Co-Authored-By: Claude Opus 5.5 <[email protected]>
62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
import os
|
|
import aiohttp
|
|
import feedparser
|
|
from discord.ext import commands
|
|
|
|
from twitterbridge.rss import embed_link
|
|
|
|
REQUEST_TIMEOUT = 15
|
|
|
|
|
|
class TwitterLeaks(commands.Cog):
|
|
def __init__(self, bot):
|
|
self.bot = bot
|
|
|
|
@commands.command()
|
|
async def leaks(self, ctx):
|
|
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.')
|
|
|
|
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]:
|
|
response += f"\n{embed_link(entry.get('link', ''))}\n"
|
|
|
|
await ctx.reply(response[:2000])
|
|
|
|
|
|
async def setup(bot):
|
|
await bot.add_cog(TwitterLeaks(bot))
|