2005-11-21 19:21:25 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# gPodder
|
|
|
|
# Copyright (c) 2005 Thomas Perl <thp@perli.net>
|
|
|
|
# Released under the GNU General Public License (GPL)
|
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
# libgpodder.py -- gpodder configuration
|
|
|
|
# thomas perl <thp@perli.net> 20051030
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import gtk
|
2006-03-19 15:21:48 +01:00
|
|
|
import thread
|
2005-11-21 19:21:25 +01:00
|
|
|
|
|
|
|
from xml.sax.saxutils import DefaultHandler
|
|
|
|
from xml.sax import make_parser
|
|
|
|
from string import strip
|
|
|
|
from os.path import expanduser
|
|
|
|
from os.path import basename
|
|
|
|
from os.path import exists
|
|
|
|
from os.path import dirname
|
|
|
|
from os import mkdir
|
2006-01-09 00:52:40 +01:00
|
|
|
from os import environ
|
2006-02-04 18:29:17 +01:00
|
|
|
from os import system
|
2005-11-21 19:21:25 +01:00
|
|
|
|
|
|
|
from librssreader import rssReader
|
2006-03-03 21:04:25 +01:00
|
|
|
from libpodcasts import podcastChannel
|
2005-11-21 19:21:25 +01:00
|
|
|
|
2005-11-21 22:17:54 +01:00
|
|
|
# global debugging variable, set to False on release
|
2006-02-04 18:29:17 +01:00
|
|
|
# TODO: while developing a new version, set this to "True"
|
2006-02-05 20:57:26 +01:00
|
|
|
debugging = True
|
2005-11-21 22:17:54 +01:00
|
|
|
|
2006-03-19 15:21:48 +01:00
|
|
|
# global lock for thread exclusion
|
|
|
|
globalLock = thread.allocate_lock()
|
|
|
|
|
2005-11-21 22:17:54 +01:00
|
|
|
def isDebugging():
|
|
|
|
return debugging
|
|
|
|
|
2006-03-19 15:21:48 +01:00
|
|
|
def getLock():
|
|
|
|
globalLock.acquire()
|
|
|
|
|
|
|
|
def releaseLock():
|
|
|
|
globalLock.release()
|
|
|
|
|
2005-11-21 19:21:25 +01:00
|
|
|
class gPodderLib( object):
|
|
|
|
gpodderdir = ""
|
|
|
|
downloaddir = ""
|
|
|
|
cachedir = ""
|
2006-01-09 00:52:40 +01:00
|
|
|
http_proxy = ""
|
|
|
|
ftp_proxy = ""
|
2006-02-04 18:29:17 +01:00
|
|
|
open_app = ""
|
2005-11-21 19:21:25 +01:00
|
|
|
|
|
|
|
def __init__( self):
|
|
|
|
self.gpodderdir = expanduser( "~/.config/gpodder/")
|
|
|
|
self.createIfNecessary( self.gpodderdir)
|
|
|
|
self.downloaddir = self.gpodderdir + "downloads/"
|
|
|
|
self.createIfNecessary( self.downloaddir)
|
|
|
|
self.cachedir = self.gpodderdir + "cache/"
|
|
|
|
self.createIfNecessary( self.cachedir)
|
2006-01-09 00:52:40 +01:00
|
|
|
try:
|
|
|
|
self.http_proxy = environ['http_proxy']
|
|
|
|
except:
|
|
|
|
self.http_proxy = ''
|
|
|
|
try:
|
|
|
|
self.ftp_proxy = environ['ftp_proxy']
|
|
|
|
except:
|
|
|
|
self.ftp_proxy = ''
|
|
|
|
self.loadConfig()
|
2005-11-21 19:21:25 +01:00
|
|
|
|
|
|
|
def createIfNecessary( self, path):
|
|
|
|
#TODO: recursive mkdir all parent directories
|
|
|
|
|
|
|
|
if path.endswith('/'):
|
|
|
|
path = path[:-1]
|
|
|
|
|
|
|
|
if not exists(dirname(path)):
|
|
|
|
mkdir(dirname(path))
|
|
|
|
|
|
|
|
if not exists( path):
|
|
|
|
mkdir( path)
|
|
|
|
|
|
|
|
def getConfigFilename( self):
|
2006-01-09 00:52:40 +01:00
|
|
|
return self.gpodderdir + "gpodder.conf"
|
2005-11-21 19:21:25 +01:00
|
|
|
|
|
|
|
def getChannelsFilename( self):
|
|
|
|
return self.gpodderdir + "channels.xml"
|
2006-02-04 18:29:17 +01:00
|
|
|
|
2006-01-09 00:52:40 +01:00
|
|
|
def propertiesChanged( self):
|
|
|
|
# set new environment variables for subprocesses to use
|
|
|
|
environ['http_proxy'] = self.http_proxy
|
|
|
|
environ['ftp_proxy'] = self.ftp_proxy
|
|
|
|
# save settings for next startup
|
|
|
|
self.saveConfig()
|
|
|
|
|
|
|
|
def saveConfig( self):
|
|
|
|
fn = self.getConfigFilename()
|
|
|
|
fp = open( fn, "w")
|
|
|
|
fp.write( self.http_proxy + "\n")
|
|
|
|
fp.write( self.ftp_proxy + "\n")
|
2006-02-04 18:29:17 +01:00
|
|
|
fp.write( self.open_app + "\n")
|
2006-01-09 00:52:40 +01:00
|
|
|
fp.close()
|
|
|
|
|
|
|
|
def loadConfig( self):
|
|
|
|
try:
|
|
|
|
fn = self.getConfigFilename()
|
|
|
|
fp = open( fn, "r")
|
|
|
|
http = fp.readline()
|
|
|
|
ftp = fp.readline()
|
2006-02-04 18:29:17 +01:00
|
|
|
app = fp.readline()
|
2006-01-09 00:52:40 +01:00
|
|
|
if http != "" and ftp != "":
|
|
|
|
self.http_proxy = strip( http)
|
|
|
|
self.ftp_proxy = strip( ftp)
|
2006-02-04 18:29:17 +01:00
|
|
|
if app != "":
|
|
|
|
self.open_app = strip( app)
|
|
|
|
else:
|
|
|
|
self.open_app = "gnome-open"
|
2006-01-09 00:52:40 +01:00
|
|
|
fp.close()
|
|
|
|
except:
|
2006-02-04 18:29:17 +01:00
|
|
|
# TODO: well, well.. (http + ftp?)
|
|
|
|
self.open_app = "gnome-open"
|
|
|
|
|
|
|
|
def openFilename( self, filename):
|
|
|
|
if isDebugging():
|
|
|
|
print "open " + filename + " with " + self.open_app
|
|
|
|
system( self.open_app + " " + filename + " &")
|
2006-03-03 21:04:25 +01:00
|
|
|
|
2005-11-21 19:21:25 +01:00
|
|
|
def getPodcastFilename( self, channel, url):
|
2005-11-23 20:53:18 +01:00
|
|
|
# strip question mark (and everything behind it), fix %20 errors
|
|
|
|
filename = basename( url).replace( "%20", " ")
|
|
|
|
indexOfQuestionMark = filename.rfind( "?")
|
|
|
|
if indexOfQuestionMark != -1:
|
|
|
|
filename = filename[:indexOfQuestionMark]
|
|
|
|
# end strip questionmark
|
2006-03-03 21:04:25 +01:00
|
|
|
channel.download_dir
|
2006-03-04 21:45:01 +01:00
|
|
|
return channel.save_dir + filename
|
2006-01-09 22:40:54 +01:00
|
|
|
|
2005-12-24 15:58:55 +01:00
|
|
|
def podcastFilenameExists( self, channel, url):
|
|
|
|
return exists( self.getPodcastFilename( channel, url))
|
2005-11-21 19:21:25 +01:00
|
|
|
|
|
|
|
class gPodderChannelWriter( object):
|
|
|
|
def write( self, channels):
|
|
|
|
filename = gPodderLib().getChannelsFilename()
|
|
|
|
fd = open( filename, "w")
|
2006-03-03 21:04:25 +01:00
|
|
|
print >> fd, '<!-- automatically generated, will be overwritten on next gpodder shutdown.-->'
|
|
|
|
print >> fd, '<channels>'
|
2005-11-21 19:21:25 +01:00
|
|
|
for chan in channels:
|
2006-03-03 21:04:25 +01:00
|
|
|
print >> fd, ' <channel name="%s">' %chan.filename
|
|
|
|
print >> fd, ' <url>%s</url>' %chan.url
|
|
|
|
print >> fd, ' <download_dir>%s</download_dir>' %chan.save_dir
|
|
|
|
print >> fd, ' </channel>'
|
|
|
|
print >> fd, '</channels>'
|
2005-11-21 19:21:25 +01:00
|
|
|
fd.close()
|
|
|
|
|
|
|
|
class gPodderChannelReader( DefaultHandler):
|
|
|
|
channels = []
|
|
|
|
current_item = None
|
|
|
|
current_element_data = ""
|
|
|
|
|
|
|
|
def __init__( self):
|
|
|
|
None
|
|
|
|
|
|
|
|
def read( self, force_update = False):
|
|
|
|
self.channels = []
|
|
|
|
parser = make_parser()
|
|
|
|
parser.setContentHandler( self)
|
|
|
|
if exists( gPodderLib().getChannelsFilename()):
|
|
|
|
parser.parse( gPodderLib().getChannelsFilename())
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
reader = rssReader()
|
|
|
|
input_channels = []
|
|
|
|
|
|
|
|
for channel in self.channels:
|
2006-03-03 21:04:25 +01:00
|
|
|
cachefile = channel.downloadRss(force_update)
|
|
|
|
reader.parseXML(channel.url, cachefile)
|
2005-11-21 19:21:25 +01:00
|
|
|
|
|
|
|
if channel.filename != "" and channel.filename != "__unknown__":
|
|
|
|
reader.channel.shortname = channel.filename
|
|
|
|
|
|
|
|
input_channels.append( reader.channel)
|
|
|
|
|
|
|
|
return input_channels
|
|
|
|
|
|
|
|
def startElement( self, name, attrs):
|
|
|
|
self.current_element_data = ""
|
|
|
|
|
|
|
|
if name == "channel":
|
2006-03-03 21:04:25 +01:00
|
|
|
self.current_item = podcastChannel()
|
2005-11-21 19:21:25 +01:00
|
|
|
self.current_item.filename = attrs.get( "name", "")
|
|
|
|
|
|
|
|
def endElement( self, name):
|
|
|
|
if self.current_item != None:
|
|
|
|
if name == "url":
|
|
|
|
self.current_item.url = self.current_element_data
|
2006-03-03 21:04:25 +01:00
|
|
|
if name == "download_dir":
|
|
|
|
self.current_item.download_dir = self.current_element_data
|
2005-11-21 19:21:25 +01:00
|
|
|
if name == "channel":
|
|
|
|
self.channels.append( self.current_item)
|
|
|
|
self.current_item = None
|
|
|
|
|
|
|
|
def characters( self, ch):
|
|
|
|
self.current_element_data = self.current_element_data + ch
|
|
|
|
|
|
|
|
|