Skip to content

Instantly share code, notes, and snippets.

@haron
Created November 19, 2017 18:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haron/6d969a4199a9431a30aae6a269ee3d98 to your computer and use it in GitHub Desktop.
Save haron/6d969a4199a9431a30aae6a269ee3d98 to your computer and use it in GitHub Desktop.
"""
feeds.py - minimalistic console RSS reader
Installation: pip install -U feedparser
Usage:
python3 feeds.py https://hnrss.org/frontpage https://www.reddit.com/r/Python/new.rss?limit=10
or, to get 10 entries from all feeds, shuffled:
SAMPLE=10 python3 feeds.py https://hnrss.org/frontpage https://www.reddit.com/r/Python/new.rss?limit=10
"""
import os, sys, feedparser, functools, operator, time, random
from concurrent.futures import ThreadPoolExecutor as executor
def parse(url):
return feedparser.parse(url).entries
def sample(entries):
size = os.environ.get("SAMPLE")
return random.sample(entries, int(size)) if size else entries
def get():
return functools.reduce(operator.add, executor().map(parse, sys.argv[1:]), [])
sort_key=lambda x: x.get("updated_parsed", x.get("published_parsed", time.localtime()))
sorted_feed = functools.partial(sorted, key=sort_key)
[print(f"{e.title}\n {e.link}") for e in sample(sorted_feed(get()))]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment