Created
November 19, 2017 18:39
-
-
Save haron/6d969a4199a9431a30aae6a269ee3d98 to your computer and use it in GitHub Desktop.
This file contains 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
""" | |
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