-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathrss.py
63 lines (51 loc) · 2.2 KB
/
rss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import sys
import feedparser
from sql import db
from time import sleep, time
from dotenv import load_dotenv
from pyrogram import Client, filters
from pyrogram.errors import FloodWait
from apscheduler.schedulers.background import BackgroundScheduler
if os.path.exists("config.env"):
load_dotenv("config.env")
try:
api_id = int(os.environ.get("API_ID")) # Get it from my.telegram.org
api_hash = os.environ.get("API_HASH") # Get it from my.telegram.org
feed_urls = list(set(i for i in os.environ.get("FEED_URLS").split("|"))) # RSS Feed URL of the site.
bot_token = os.environ.get("BOT_TOKEN") # Get it by creating a bot on https://t.me/botfather
log_channel = int(os.environ.get("LOG_CHANNEL")) # Telegram Channel ID where the bot is added and have write permission. You can use group ID too.
check_interval = int(os.environ.get("INTERVAL", 10)) # Check Interval in seconds.
max_instances = int(os.environ.get("MAX_INSTANCES", 3)) # Max parallel instance to be used.
except Exception as e:
print(e)
print("One or more variables missing. Exiting !")
sys.exit(1)
for feed_url in feed_urls:
if db.get_link(feed_url) == None:
db.update_link(feed_url, "*")
app = Client(":memory:", api_id=api_id, api_hash=api_hash, bot_token=bot_token)
def create_feed_checker(feed_url):
def check_feed():
FEED = feedparser.parse(feed_url)
entry = FEED.entries[0]
if entry.id != db.get_link(feed_url).link:
# ↓ Edit this message as your needs.
message = f"**{entry.title}**\n```{entry.link}```"
try:
app.send_message(log_channel, message)
db.update_link(feed_url, entry.id)
except FloodWait as e:
print(f"FloodWait: {e.x} seconds")
sleep(e.x)
except Exception as e:
print(e)
else:
print(f"Checked RSS FEED: {entry.id}")
return check_feed
scheduler = BackgroundScheduler()
for feed_url in feed_urls:
feed_checker = create_feed_checker(feed_url)
scheduler.add_job(feed_checker, "interval", seconds=check_interval, max_instances=max_instances)
scheduler.start()
app.run()