Last active
May 8, 2021 15:00
-
-
Save pybites/8055afa99cc626eb3c50265036211768 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
"""Script to retrieve new titles from O'Reilly Media (formerly Safari Books Online)""" | |
from collections import namedtuple | |
from pathlib import Path | |
from datetime import datetime, timedelta | |
from urllib.request import urlretrieve | |
from xml.etree.ElementTree import parse | |
RSS_FEED = "https://www.oreilly.com/feeds/recently-added.rss" | |
NOW = datetime.now() | |
DT_FMT = "%a, %d %b %Y %H:%M:%S" | |
Entry = namedtuple('Entry', 'title link') | |
def _parse_entries(doc, look_back): | |
for item in doc.iterfind('channel/item'): | |
title = item.findtext('title') | |
link = item.findtext('link') | |
pub_date = datetime.strptime( | |
item.findtext('pubDate')[:-6], DT_FMT) | |
if (NOW - pub_date) > look_back: | |
continue | |
yield Entry(title, link) | |
def get_new_titles(feed=RSS_FEED, peek_back_days=1): | |
filename = Path(RSS_FEED).name | |
urlretrieve(feed, filename) | |
with open(filename) as f: | |
look_back = timedelta(days=peek_back_days) | |
doc = parse(f) | |
yield from _parse_entries(doc, look_back) | |
if __name__ == '__main__': | |
for entry in get_new_titles(): | |
print(entry.title) | |
print(entry.link) | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment