2005-11-21 19:21:25 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
|
|
|
from libgpodder import gPodderChannelReader
|
|
|
|
from libgpodder import gPodderChannelWriter
|
|
|
|
from libgpodder import gPodderLib
|
|
|
|
from libpodcasts import podcastChannel
|
|
|
|
from librssreader import rssReader
|
|
|
|
from libwget import downloadThread
|
|
|
|
|
2005-12-08 20:47:35 +01:00
|
|
|
from os import kill
|
|
|
|
from string import strip
|
|
|
|
import popen2
|
|
|
|
import signal
|
|
|
|
|
2005-11-21 19:21:25 +01:00
|
|
|
|
|
|
|
#TODO: move to libwget??
|
|
|
|
class DownloadPool:
|
|
|
|
def __init__(self, max_downloads = 3):
|
|
|
|
assert(max_downloads > 0)
|
|
|
|
|
|
|
|
self.max_downloads = max_downloads
|
|
|
|
self.cur_downloads = 0
|
|
|
|
|
|
|
|
def addone(self):
|
|
|
|
self.cur_downloads += 1
|
|
|
|
|
|
|
|
def set(self):
|
|
|
|
'''Ping function for downloadThread'''
|
|
|
|
if self.cur_downloads >0:
|
|
|
|
self.cur_downloads -= 1
|
|
|
|
else:
|
|
|
|
self.cur_downloads = 0
|
|
|
|
|
|
|
|
def may_download(self):
|
|
|
|
return self.cur_downloads < self.max_downloads
|
|
|
|
|
|
|
|
|
|
|
|
def list_channels():
|
|
|
|
reader = gPodderChannelReader()
|
|
|
|
reader.read()
|
|
|
|
for id, channel in enumerate(reader.channels):
|
|
|
|
print "%s: %s" %(id, channel.url)
|
|
|
|
|
|
|
|
def add_channel(url):
|
|
|
|
reader = gPodderChannelReader()
|
|
|
|
channels = reader.read()
|
|
|
|
|
|
|
|
cachefile = gPodderLib().downloadRss(url)
|
|
|
|
rssreader = rssReader()
|
|
|
|
rssreader.parseXML(url, cachefile)
|
|
|
|
|
|
|
|
channels.append(rssreader.channel)
|
|
|
|
gPodderChannelWriter().write(channels)
|
|
|
|
|
|
|
|
def del_channel(chid):
|
|
|
|
#TODO maybe add id to channels.xml
|
|
|
|
reader = gPodderChannelReader()
|
|
|
|
channels = reader.read()
|
|
|
|
if chid >=0 and chid < len(channels):
|
|
|
|
ch = channels.pop(chid)
|
2006-03-31 15:00:54 +02:00
|
|
|
print _('delete channel: %s') %ch.url
|
2005-11-21 19:21:25 +01:00
|
|
|
gPodderChannelWriter().write(channels)
|
|
|
|
else:
|
2006-03-31 15:00:54 +02:00
|
|
|
print _('%s is not a valid id') %str(chid)
|
2005-11-21 19:21:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
def update():
|
|
|
|
reader = gPodderChannelReader()
|
|
|
|
reader.read(True)
|
|
|
|
|
|
|
|
|
|
|
|
def run():
|
|
|
|
'''Update channels und download all items which are not already downloaded'''
|
|
|
|
reader = gPodderChannelReader()
|
|
|
|
updated_channels = reader.read(True)
|
|
|
|
|
|
|
|
pool = DownloadPool()
|
|
|
|
|
|
|
|
for channel in updated_channels:
|
|
|
|
for item in channel.items:
|
2006-03-29 14:41:34 +02:00
|
|
|
filename = channel.getPodcastFilename( item.url)
|
2006-01-09 22:40:54 +01:00
|
|
|
if not channel.isDownloaded( item):
|
2005-11-21 19:21:25 +01:00
|
|
|
while not pool.may_download():
|
|
|
|
time.sleep(3)
|
|
|
|
|
|
|
|
pool.addone()
|
|
|
|
#thread will call pool.set() when finished
|
|
|
|
thread = downloadThread(item.url, filename, pool)
|
|
|
|
thread.download()
|
|
|
|
|
|
|
|
|
2005-12-08 20:47:35 +01:00
|
|
|
def testForWget():
|
|
|
|
command = "wget --version | head -n1"
|
|
|
|
process = popen2.Popen3( command, True)
|
|
|
|
stdout = process.fromchild
|
|
|
|
data = stdout.readline( 80)
|
|
|
|
kill( process.pid, signal.SIGKILL)
|
|
|
|
return strip( data)
|
|
|
|
# end testForWget()
|
|
|
|
|