-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathiconDownloader.py
35 lines (32 loc) · 1.2 KB
/
iconDownloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import workerpool, urllib2, gobject, os
class IconDownloader(workerpool.Job):
def __init__(self, basehost, channel, imageurl, callback):
self.basehost = basehost
self.channel = channel
self.imageurl = imageurl
self.callback = callback
if not os.path.exists(os.environ['HOME'] + '/.tvmaxe'):
os.mkdir(os.environ['HOME'] + '/.tvmaxe')
if not os.path.exists(os.environ['HOME'] + '/.tvmaxe/cache'):
os.mkdir(os.environ['HOME'] + '/.tvmaxe/cache')
def run(self):
imageurl = self.imageurl
if imageurl == '':
return
basehost = self.basehost
if not os.path.exists(os.environ['HOME'] + '/.tvmaxe/cache/' + os.path.basename(imageurl)):
if imageurl.startswith('http://'):
req = urllib2.Request(imageurl)
response = urllib2.urlopen(req)
else:
req = urllib2.Request(basehost + imageurl) # deprecated...
response = urllib2.urlopen(req)
imgdata = response.read()
fh = open(os.environ['HOME'] + '/.tvmaxe/cache/' + os.path.basename(imageurl), 'wb')
fh.write(imgdata)
fh.close()
else:
fh = open(os.environ['HOME'] + '/.tvmaxe/cache/' + os.path.basename(imageurl), 'rb')
imgdata = fh.read()
fh.close()
gobject.idle_add(self.callback, self.channel, imgdata)