Skip to content

Instantly share code, notes, and snippets.

@EZonTheEyes
Last active June 23, 2024 18:25
Show Gist options
  • Save EZonTheEyes/1ee6f12383c2a160159174dfc0b0a707 to your computer and use it in GitHub Desktop.
Save EZonTheEyes/1ee6f12383c2a160159174dfc0b0a707 to your computer and use it in GitHub Desktop.
PyRSS WIP July 16 2023
# RSS Specification: https://www.rssboard.org/rss-draft-1
# Sample RSS XML file: https://www.rssboard.org/files/rss-2.0-sample.xml
# Python XML documentation: https://docs.python.org/3/library/xml.etree.elementtree.html
# Juicy xpath thingy for looking up elements: https://docs.python.org/3/library/xml.etree.elementtree.html#xpath-support
# The plan:
# Plan (real (real v reall)) (extremely real) (non-fake)
# - call on headlines & print (specify ammount)) <item, title> - DONE?
# - print text preview (body of article) (specify characters) <item, description> - DONE
# - Print URL Link <link> - DONE
# - ...
# - Do not die when encountering errors, but continue showing other feeds - DONE
# - Image Preview (covert to Ascii with shady libs)
# - Random article Function...?
# - Remove Non XML RSS feed Links, maybe print a warning - error?
# - Gui????? maybe
# - ???
# - Profit.
# - DAY 1 === Almost Totally Done.
# - Nerd Plan
# - Downlaod XML from RSS feed (download manually first for conveneince) === DONE
# - Parse the properties we need from the downloaded XML file === DONE
# - Print them bad boys out === DONE
# - Profit. === CLOSE ENOUGH
# - DAY 2
# - Date / Time === DONE
# - Random === NOT DONE
# - Added the Entry Limit === DONE
import sys
import random
import xml.etree.ElementTree as ET
from urllib.request import urlopen
print("💜 testie line for my bestie line 💜")
def get_element(article, name):
item_list = article.findall(name)
if len(item_list) > 0:
return item_list[0].text
else:
return "Error: No Entry."
def print_rss_article(article):
item_title = get_element(article, "./title")
print(f"Title: {item_title}")
item_desc = get_element(article, "./description")
print(f"Description: {item_desc}")
item_date = get_element(article, "./pubDate")
print(f"Date: {item_date}")
item_link = get_element(article, "./link")
print(f"Link: {item_link}")
def get_article_list(rss_xml):
try:
root = ET.fromstring(rss_xml)
except:
print("Oops! XML Library Error.")
return []
article_list = root.findall("./channel/item")
news_source = root.findall("./channel/title")
# Give me 1 second
#I am getting a bit tired + hungry + i gotta shower later, i have another like 20 min left in me, if we can try to finish up a section by then
return article_list
def print_rss_feed(rss_xml):
article_list = get_article_list(rss_xml)
amount_of_articles_printed = 0
for article in article_list:
# if amount of articles printed > 10 then stop
amount_of_articles_printed += 1
if amount_of_articles_printed > 10:
break
print("-----")
print_rss_article(article)
# print(f"News source: {news_source}")
urls = ["https://media.rss.com/gaypropaganda/feed.xml", "http://feeds.feedburner.com/ign/all", "https://anchor.fm/s/597282c/podcast/rss", "https://steamcommunity.com/groups/freegamesfinders/rss/", "https://kotaku.com/rss", "https://joeroganexp.libsyn.com/rss", "http://rss.cnn.com/rss/cnn_us.rss", "https://englishcode.wordpress.com/feed", "https://media.rss.com/welovethestate/feed.xml", "https://moxie.foxnews.com/google-publisher/politics.xml"]
def RandomArticle():
random_index = random.randrange(0, len(urls))
random_url = urls[random_index]
with urlopen(random_url) as response:
response_rss = response.read()
article_list = get_article_list(response_rss)
if len(article_list) == 0:
print(f"Oh no, no rss found please give a better url my great friend, {random_url} is broken")
return
article_index = random.randrange(0, len(article_list))
random_article = article_list[article_index]
print_rss_article(random_article)
print("======")
# Output: "Hello there bestie my great friend the dearest best person i love so much, <some name> 💜"
# Loop over all of the urls from the array, and print the feed for each one
def AllArticles():
for url in urls:
# Download the RSS feed from the URL
with urlopen(url) as response:
response_rss = response.read()
# Parse and print the feed :)
print_rss_feed(response_rss)
print("======")
if len(sys.argv) > 1 and sys.argv[1] == "random":
RandomArticle()
else:
AllArticles()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment