//depot/projects/podgrab/podgrab.py#5 – edit change 118 (xtext) ·revision history
| 1 | #!/usr/bin/env python |
| 2 | """podgrab - fetch podcast enclosures and drop them on the iPod. |
| 3 | |
| 4 | usage: podgrab.py [feeds.opml] |
| 5 | |
| 6 | ksimpson, oct 2004. needs Python 2.3. |
| 7 | """ |
| 8 | |
| 9 | __version__ = "1.0" |
| 10 | |
| 11 | import os |
| 12 | import shutil |
| 13 | import sys |
| 14 | import urllib2 |
| 15 | from xml.dom import minidom |
| 16 | |
| 17 | IPOD = "/Volumes/IPOD" |
| 18 | CACHE = os.path.expanduser("~/.podgrab") |
| 19 | SEEN = os.path.join(CACHE, "seen") |
| 20 | UA = "podgrab/%s (+http://ttul.org/)" % __version__ |
| 21 | MAX_PER_FEED = 3 |
| 22 | |
| 23 | |
| 24 | def fetch(url): |
| 25 | return urllib2.urlopen(urllib2.Request(url, headers={"User-Agent": UA})) |
| 26 | |
| 27 | |
| 28 | def read_opml(path): |
| 29 | dom = minidom.parse(path) |
| 30 | feeds = [] |
| 31 | for node in dom.getElementsByTagName("outline"): |
| 32 | url = node.getAttribute("xmlUrl") |
| 33 | if url: |
| 34 | feeds.append((node.getAttribute("text"), url)) |
| 35 | return feeds |
| 36 | |
| 37 | |
| 38 | def enclosures(feed_url): |
| 39 | dom = minidom.parse(fetch(feed_url)) |
| 40 | for item in dom.getElementsByTagName("item"): |
| 41 | for enc in item.getElementsByTagName("enclosure"): |
| 42 | yield enc.getAttribute("url") |
| 43 | |
| 44 | |
| 45 | def load_seen(): |
| 46 | seen = {} |
| 47 | if not os.path.exists(SEEN): |
| 48 | return seen |
| 49 | for line in open(SEEN): |
| 50 | seen[line.strip()] = 1 |
| 51 | return seen |
| 52 | |
| 53 | |
| 54 | def main(argv): |
| 55 | opml = len(argv) > 1 and argv[1] or os.path.join(CACHE, "feeds.opml") |
| 56 | if not os.path.isdir(CACHE): |
| 57 | os.makedirs(CACHE) |
| 58 | seen = load_seen() |
| 59 | out = open(SEEN, "a") |
| 60 | for title, url in read_opml(opml): |
| 61 | print "==>", title |
| 62 | try: |
| 63 | encs = list(enclosures(url))[:MAX_PER_FEED] |
| 64 | except Exception, e: |
| 65 | print " feed broken:", e |
| 66 | continue |
| 67 | for enc in encs: |
| 68 | if enc in seen: |
| 69 | continue |
| 70 | fname = os.path.join(CACHE, os.path.basename(enc)) |
| 71 | print " ", enc |
| 72 | f = open(fname, "wb") |
| 73 | shutil.copyfileobj(fetch(enc), f) |
| 74 | f.close() |
| 75 | out.write(enc + "\n") |
| 76 | if os.path.isdir(IPOD): |
| 77 | # NB: this does not update the iTunesDB. use gtkpod, or |
| 78 | # just play it from the notes folder like a caveman. |
| 79 | shutil.copy(fname, os.path.join(IPOD, "Podcasts")) |
| 80 | out.close() |
| 81 | |
| 82 | |
| 83 | if __name__ == "__main__": |
| 84 | main(sys.argv) |