summaryrefslogtreecommitdiffstats
path: root/customgrabber.py
blob: 04647bf40ec7026f0bbd6cc21642e4e55b823462 (plain)
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
# file: /usr/lib/python2.5/site-packages/urlgrabber/customgrabber.py

import grabber, sys, os
import subprocess
import urllib2

def get_filesize(url):
    usock = urllib2.urlopen(url)
    size =  usock.info().get('Content-Length')
    if size is None:
        size = 0
    return float(size) 

class AxelGrabber(grabber.URLGrabber):
   def urlgrab(self, url, filename=None, **kwargs):
        """grab the file at  and make a local copy at 
        If filename is none, the basename of the url is used.
        urlgrab returns the filename of the local file, which may be 
        different from the passed-in filename if copy_local == 0.
        """

        opts = self.opts.derive(**kwargs)
        (url,parts) = opts.urlparser.parse(url, opts)
        (scheme, host, path, parm, query, frag) = parts


        def retryfunc(opts, url, filename):
            fsize = get_filesize(url)
            if (fsize/1024) < 100:
               parts = 1
            elif (fsize/1024) < 500:
               parts = 2
            elif (fsize/1024/1024) < 1:
               parts = 3
            elif (fsize/1024/1024) < 5:
               parts = 4
            elif (fsize/1024/1024) < 10:
               parts = 6
            elif (fsize/1024/1024) < 15:
               parts = 8
            else:
               parts = 10
            if os.path.exists(filename):
               if not os.path.exists("%s.st" % filename):
                  os.unlink(filename)
            p = subprocess.Popen(['/usr/bin/axel','-n','%s' % parts,'-a','-o',filename,url],stdout=sys.stdout,stderr=sys.stderr)
            o = p.wait()
            if o:
               raise grabber.URLGrabError(-1)
            return filename
 
        return self._retry(opts, retryfunc, url, filename)