├── README.md └── magazine.py /README.md: -------------------------------------------------------------------------------- 1 | # rss_feed_notifier 2 | Simple RSS feed notification system 3 | -------------------------------------------------------------------------------- /magazine.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | import sys 3 | import urllib3 4 | import certifi 5 | import sqlite3 6 | import smtplib 7 | import feedparser 8 | from email.mime.text import MIMEText 9 | 10 | 11 | db_connection = sqlite3.connect('magazine_rss.sqlite') 12 | db = db_connection.cursor() 13 | db.execute('CREATE TABLE IF NOT EXISTS magazine (title TEXT, date TEXT)') 14 | 15 | 16 | def article_is_not_db(article_title, article_date): 17 | """ Check if a given pair of article title and date 18 | is in the database. 19 | Args: 20 | article_title (str): The title of an article 21 | article_date (str): The publication date of an article 22 | Return: 23 | True if the article is not in the database 24 | False if the article is already present in the database 25 | """ 26 | db.execute("SELECT * from magazine WHERE title=? AND date=?", (article_title, article_date)) 27 | if not db.fetchall(): 28 | return True 29 | else: 30 | return False 31 | 32 | 33 | def add_article_to_db(article_title, article_date): 34 | """ Add a new article title and date to the database 35 | Args: 36 | article_title (str): The title of an article 37 | article_date (str): The publication date of an article 38 | """ 39 | db.execute("INSERT INTO magazine VALUES (?, ?)", (article_title, article_date)) 40 | db_connection.commit() 41 | 42 | 43 | def send_notification(article_title, article_url): 44 | """ Send a notification email 45 | Args: 46 | article_title (str): The title of an article 47 | article_url (str): The url to access the article 48 | """ 49 | 50 | smtp_server = smtplib.SMTP('smtp.gmail.com', 587) 51 | smtp_server.ehlo() 52 | smtp_server.starttls() 53 | smtp_server.login('your_email@gmail.com', '123your_password') 54 | msg = MIMEText(f'\nHi there is a new Fedora Magazine article : {article_title}. \nYou can read it here {article_url}') 55 | msg['Subject'] = 'New Fedora Magazine Article Available' 56 | msg['From'] = 'your_email@gmail.com' 57 | msg['To'] = 'destination_email@gmail.com' 58 | smtp_server.send_message(msg) 59 | smtp_server.quit() 60 | 61 | 62 | def send_telegram_notification(article_title, article_url): 63 | """ Send a notification message to a Telegram chat 64 | using your own bot 65 | Docs: https://core.telegram.org/bots 66 | Args: 67 | article_title (str): The title of an article 68 | article_url (str): The url to access the article 69 | """ 70 | bot_id = '' 71 | chat_id = '' 72 | 73 | try: 74 | https = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where()) 75 | r = https.request('GET', 'https://api.telegram.org/bot' 76 | + bot_id + '/sendMessage?chat_id=' 77 | + chat_id + '&text=' + article_title + article_url) 78 | except urllib3.exceptions.SSLError as err: 79 | print('[ERROR] Telegram SSL error', err) 80 | sys.exit() 81 | 82 | 83 | def read_article_feed(): 84 | """ Get articles from RSS feed """ 85 | feed = feedparser.parse('https://fedoramagazine.org/feed/') 86 | for article in feed['entries']: 87 | if article_is_not_db(article['title'], article['published']): 88 | ### Email notification 89 | send_notification(article['title'], article['link']) 90 | ### Telegram notification 91 | #send_telegram_notification(article['title'], article['link']) 92 | add_article_to_db(article['title'], article['published']) 93 | 94 | 95 | if __name__ == '__main__': 96 | read_article_feed() 97 | db_connection.close() 98 | --------------------------------------------------------------------------------