Skip to content

Programming / Python

Ordered Dictionary

from collections import OrderedDict

# NEWS FEED LINKS
news_feed_links = OrderedDict([
    ('Open-Source', 'https://www.google.com/alerts/feeds/07480943305324684294/8607219941662562161'),
    ('Renewable Energy', 'https://www.google.com/alerts/feeds/07480943305324684294/6721195691717171314'),
    ('Biotechnology', 'https://www.google.com/alerts/feeds/07480943305324684294/53244181703901167')
])

Read RSS

import feedparser

Classes

class Article:
    def __init__(self, url, title, publish_date=None, topics=None):
        self.url = url
        self.title = title
        self.publish_date = publish_date
        self.topics = []

    def __str__(self):
        return f"{self.title}\n"

new_article = Article(url=article.link, title=article_title, topics=topic_name)

Output / Printing

print("▶ Downloading Articles from News feeds...")

Looping / For with several variables

# Loop through News Feeds, and save the Articles
for topic_name, rss_link in news_feed_links.items():
        new_topic = Topic(topic_name)
        feed = feedparser.parse(rss_link)
        print("--------")
        print(f"Topic: {topic_name} ({len(feed.entries)} articles)")

Append items to a Collection

new_topic.articles.append(new_article)

Limiting a Loop

for article in topic.articles[:main_feed_article_limit]:
                    html_output += f'''
                    <a href="{article.url}" class="list-group-item">
                    {article.title}
                    </a>      
                    '''