FilesSubmittedBranchesLabelsClientsUsersJobsHelp
Tree viewShow file textShow revision historyShow/hide recent activityInformation
//depot/projects/podgrab/podgrab.py#5

//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
4usage: podgrab.py [feeds.opml]
5
6ksimpson, oct 2004. needs Python 2.3.
7"""
8
9__version__ = "1.0"
10
11import os
12import shutil
13import sys
14import urllib2
15from xml.dom import minidom
16
17IPOD = "/Volumes/IPOD"
18CACHE = os.path.expanduser("~/.podgrab")
19SEEN = os.path.join(CACHE, "seen")
20UA = "podgrab/%s (+http://ttul.org/)" % __version__
21MAX_PER_FEED = 3
22
23
24def fetch(url):
25 return urllib2.urlopen(urllib2.Request(url, headers={"User-Agent": UA}))
26
27
28def 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
38def 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
45def 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
54def 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
83if __name__ == "__main__":
84 main(sys.argv)

P4Web/LINUX26X86/2005.1/82295 · Copyright © 1995-2005 Perforce Software · ttul.org