coding utf-8 import tempfile import urllib2 urllib from time import sl

  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
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
import tempfile
import urllib2, urllib
from time import sleep
from threading import Thread
from urlgrabber import urlgrab
from Track import Track
import os
import sqlobject
import shutil
from PyQt4 import Qt
class DownloadError:
pass
class DownloadProcessor:
def __init__(self, parent):
self.parent = parent
def start(self, filename, url, basename, length, text):
"For internal use only"
self.parent.started = True
self.parent.length = length
def update(self, read):
"For internal use only"
#print read
self.parent.pos = read
def end(self, size):
"For internal use only"
self.parent.finished = True
if not self.parent.track:
return
home = os.path.expanduser('~')
coldir = self.parent.config['caching']['collection_dir']
if not os.path.exists(coldir):
os.mkdir(coldir)
if not os.path.isdir(coldir):
return
artdir = os.path.join(coldir, self.parent.track.artist)
if not os.path.exists(artdir):
os.mkdir(artdir)
while not os.path.isdir(artdir):
artdir += "_"
filep = os.path.join(artdir, self.parent.track.title)+'.mp3'
if os.path.exists(filep):
return
shutil.copyfile(self.parent.file.name, filep)
track = self.parent.files(artist = self.parent.track.artist,
title = self.parent.track.title, length = int(self.parent.track.length),
file = filep)
track.sync()
class Buffer(Qt.QThread):
"Download a track to temporary file"
length = None
pos = None
started = False
finished = False
file = None
track = None
class files(sqlobject.SQLObject):
artist = sqlobject.StringCol()
title = sqlobject.StringCol()
length = sqlobject.IntCol()
file = sqlobject.StringCol()
def __init__(self, track, begin = -1, end = -1, config = None):
self.config = config
home = os.path.expanduser('~')
dbfile = os.path.join(home,'.vplayer/cached_tracks.db')
sqlobject.sqlhub.processConnection = sqlobject.connectionForURI('sqlite://'+dbfile)
if isinstance(track, Track):
self.url = track.url
self.track = track
elif isinstance(track, str):
self.url = track
self.file = tempfile.NamedTemporaryFile()
Qt.QThread.__init__(self)
#super(Buffer, self).start()
def run(self):
if self.track:
results = self.files.selectBy(artist = self.track.artist,
title = self.track.title,
length = int(self.track.length))
if results.count()>0:
self.file = open(results[0].file)
self.length = os.path.getsize(results[0].file)
self.pos = self.length
self.started = True
self.finished = True
return
proc = DownloadProcessor(self)
res = urlgrab(self.url, self.file.name, progress_obj=proc)
if not res:
raise DownloadError()
self.filename = res