├── .gitignore ├── requirements.txt ├── README.md └── rss.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | 3 | rss.sqlite 4 | venv 5 | 6 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | certifi==2024.8.30 2 | charset-normalizer==3.3.2 3 | feedparser==6.0.11 4 | idna==3.10 5 | requests==2.32.3 6 | sgmllib3k==1.0.0 7 | urllib3==2.2.3 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### RSS feed reader 2 | 1. Register new bot with Telegram BotFather 3 | 2. Take TOKEN and Chat ID and put to the bot_token, bot_chatID variables 4 | 3. Run ./rss.py 5 | 6 | ### Venv usage 7 | 8 | Activate `venv`: 9 | ```python 10 | source venv/bin/activate 11 | ``` 12 | 13 | Install modules from `requirements.txt`: 14 | 15 | ```python 16 | pip install -r requirements.txt 17 | ``` 18 | 19 | Run rss.py: 20 | 21 | ```python 22 | python rss.py 23 | ``` 24 | ## How to update packages 25 | 26 | ```shell 27 | pip3 install pip-upgrader 28 | ``` 29 | 30 | Upgrade all packages: 31 | 32 | ```shell 33 | pip-upgrade 34 | ``` 35 | -------------------------------------------------------------------------------- /rss.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Created by Yevgeniy Goncharov, https://sys-adm.in 3 | # Script for reading and forwarding to Telegram, rss feeds 4 | 5 | # Imports 6 | import sqlite3 7 | import requests 8 | import feedparser 9 | import os 10 | import urllib 11 | import random 12 | 13 | # Bot creds 14 | bot_token = 'TOKEN' 15 | bot_chatID = 'CHAT_ID' 16 | 17 | # Feeds 18 | myfeeds = [ 19 | 'https://forum.sys-adm.in/latest.rss', 20 | ] 21 | 22 | # User agents 23 | uags = [ 24 | 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15', 25 | 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0', 26 | 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36', 27 | 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:77.0) Gecko/20100101 Firefox/77.0', 28 | 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36', 29 | ] 30 | 31 | # Random User Agent (from uags list) 32 | ua = random.choice(uags) 33 | 34 | # Header 35 | headers = { 36 | "Connection" : "close", # another way to cover tracks 37 | "User-Agent" : ua 38 | } 39 | 40 | # Proxies 41 | proxies = { 42 | } 43 | 44 | # DB 45 | scriptDir = os.path.dirname(os.path.realpath(__file__)) 46 | db_connection = sqlite3.connect(scriptDir + '/rss.sqlite') 47 | db = db_connection.cursor() 48 | db.execute('CREATE TABLE IF NOT EXISTS myrss (title TEXT, date TEXT)') 49 | 50 | # Get posts from DB and print 51 | def get_posts(): 52 | with db_connection: 53 | db.execute("SELECT * FROM myrss") 54 | print(db.fetchall()) 55 | 56 | # Check post in DB 57 | def article_is_not_db(article_title, article_date): 58 | db.execute("SELECT * from myrss WHERE title=? AND date=?", (article_title, article_date)) 59 | if not db.fetchall(): 60 | return True 61 | else: 62 | return False 63 | 64 | # Add post to DB 65 | def add_article_to_db(article_title, article_date): 66 | db.execute("INSERT INTO myrss VALUES (?,?)", (article_title, article_date)) 67 | db_connection.commit() 68 | 69 | # Send notify to Telegram bot 70 | def bot_sendtext(bot_message): 71 | bot_message = urllib.parse.quote(bot_message) 72 | send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message 73 | requests.get(send_text, proxies=proxies, headers=headers) 74 | print(send_text) 75 | 76 | # Check, read articles 77 | def read_article_feed(feed): 78 | """ Get articles from RSS feed """ 79 | feedparser.USER_AGENT = ua 80 | feed = feedparser.parse(feed) 81 | print(feed) 82 | for article in feed['entries']: 83 | if article_is_not_db(article['title'], article['published']): 84 | add_article_to_db(article['title'], article['published']) 85 | bot_sendtext('New feed found ' + article['title'] +', ' + article['link'] + ', ' + article['description']) 86 | print(article) 87 | 88 | # Rotate feeds array 89 | def spin_feds(): 90 | for x in myfeeds: 91 | print(x) 92 | read_article_feed(x) 93 | 94 | # Runner :) 95 | if __name__ == '__main__': 96 | spin_feds() 97 | # get_posts() 98 | db_connection.close() 99 | 100 | --------------------------------------------------------------------------------