Last active
April 21, 2024 11:39
-
-
Save ljmocic/ec013451ff8bbb51f308d79975d7fdb4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Read more about setting it up | |
# https://medium.com/@ljmocic/make-telegram-bot-for-notifying-about-new-rss-feed-items-4cfbcc37f4fd | |
from datetime import timedelta, datetime | |
from dateutil import parser | |
from pprint import pprint | |
from time import sleep | |
import requests | |
import feedparser | |
BOT_TOKEN = '' | |
CHANNEL_ID = '' # @bot_channel_name | |
FEED_URL = '' # https://something.com/feeds/rss.xml | |
def send_message(message): | |
requests.get(f'https://api.telegram.org/bot{BOT_TOKEN}/sendMessage?chat_id={CHANNEL_ID}&text={message}') | |
def main(): | |
rss_feed = feedparser.parse(FEED_URL) | |
for entry in rss_feed.entries: | |
parsed_date = parser.parse(entry.published) | |
parsed_date = (parsed_date - timedelta(hours=8)).replace(tzinfo=None) # remove timezone offset | |
now_date = datetime.utcnow() | |
published_20_minutes_ago = now_date - parsed_date < timedelta(minutes=20) | |
if published_20_minutes_ago: | |
send_message(entry.links[0].href) | |
print(entry.links[0].href) | |
if __name__ == "__main__": | |
while(True): | |
main() | |
sleep(20 * 60) |
Hello. I would like to ask. How can parse multiple FEED_URL. Like your code, it only parse one FEED_URL ²
create function for each feed and call them when u need
Once it send feeds the first time , will it skip to send the same items again in the second round and only send the new ones?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello. I would like to ask. How can parse multiple FEED_URL. Like your code, it only parse one FEED_URL