#!/usr/bin/python # # Podsnort, version 0.2 # # Copyright (C) 2007 Paul Dwerryhouse # # This package is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2, as # published by the Free Software Foundation # # This package is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this package; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # import feedparser import os import dbm import getopt import sys import urllib import datetime def fetchurl(conf,url): print "Fetching: ", url date = datetime.date.today().strftime("%Y%m%d") dir = conf['downloaddir'] + "/" + date try: os.mkdir(dir) except OSError: pass filename = dir + "/" + os.path.basename(url) urllib.urlretrieve(url,filename) def readrss(conf,history,url): rss = feedparser.parse(url); first = 1 for entry in rss.entries: try: for enclosure in entry.enclosures: euid = entry.id + enclosure.href if history.get(euid) == None and (conf['justone'] != 1 or first == 1) and conf['catchup'] == 0: fetchurl(conf,enclosure.href) history[euid] = '1' first = 0 except AttributeError: pass def setup(): conf = dict() home = os.environ['HOME'] basedir = home + "/" + ".podsnort" downloaddir = basedir + "/downloads" history = basedir + "/history" if not os.path.exists(basedir): os.mkdir(basedir) if not os.path.exists(downloaddir): os.mkdir(downloaddir) conf['basedir'] = basedir conf['downloaddir'] = downloaddir conf['history'] = history return conf conf = setup() try: opts,args = getopt.getopt(sys.argv[1:], "jc", ["justone","catchup"]) except getopt.GetoptError: sys.exit(2) conf['justone'] = 0 conf['catchup'] = 0 for o,a in opts: if o=='-j': conf['justone'] = 1 if o=='-c': conf['catchup'] = 1 input = open(conf['basedir'] + "/feeds",'r') history = dbm.open(conf['history'],'c',0640) for url in input: if url[0] != '#': readrss(conf,history,url)