1
- from pycurl import Curl
1
+ from urllib2 import urlopen , URLError
2
2
from bs4 import BeautifulSoup
3
3
from os .path import join , dirname , basename
4
4
from os import makedirs
@@ -49,19 +49,6 @@ def run(self) :
49
49
self .func (url )
50
50
self .queue .task_done ()
51
51
52
- class Cacher (Thread ) :
53
- def __init__ (self , queue , func ) :
54
- Thread .__init__ (self )
55
- self .queue = queue
56
- self .curl = Curl ()
57
- self .curl .setopt (self .curl .FOLLOWLOCATION , True )
58
- self .func = func
59
- def run (self ) :
60
- while True :
61
- url = self .queue .get ()
62
- self .func (self .curl , url )
63
- self .queue .task_done ()
64
-
65
52
66
53
class Cache () :
67
54
""" The Cache object is the only relevant API object at the moment
@@ -88,7 +75,7 @@ def display_counter (self, message) :
88
75
stdout .write ("{} {}/{}\r " .format (message , self .counter , self .total ))
89
76
stdout .flush ()
90
77
91
- def cache_file (self , curl , url ) :
78
+ def cache_file (self , url ) :
92
79
"""Low level interface to caching a single file.
93
80
94
81
:param curl: The user is responsible for providing a curl.Curl object as the curl parameter.
@@ -104,18 +91,11 @@ def cache_file (self, curl, url) :
104
91
except OSError as exc :
105
92
if exc .errno == EEXIST : pass
106
93
else : raise
107
- with open (dest , "wb+" ) as fd :
108
- curl .setopt (curl .URL , url )
109
- curl .setopt (curl .FOLLOWLOCATION , True )
110
- curl .setopt (curl .WRITEDATA , fd )
111
- if not self .no_timeouts :
112
- curl .setopt (curl .CONNECTTIMEOUT , 2 )
113
- curl .setopt (curl .LOW_SPEED_LIMIT , 50 * 1024 )
114
- curl .setopt (curl .LOW_SPEED_TIME , 2 )
115
- try :
116
- curl .perform ()
117
- except Exception as e :
118
- stderr .write ("[ ERROR ] file {} did not download {}\n " .format (url , str (e )))
94
+ try :
95
+ with open (dest , "wb+" ) as fd :
96
+ fd .write (urlopen (url ).read ())
97
+ except URLError as e :
98
+ stderr .write (e .reason )
119
99
self .counter += 1
120
100
self .display_counter ("Caching Files" )
121
101
@@ -401,7 +381,7 @@ def cache_descriptor_list(self, list) :
401
381
"""
402
382
self .total = len (list )
403
383
self .display_counter ("Caching Files" )
404
- do_queue (Cacher , self .cache_file , list )
384
+ do_queue (Reader , self .cache_file , list )
405
385
stdout .write ("\n " )
406
386
407
387
def cache_pack_list (self , list ) :
@@ -412,7 +392,7 @@ def cache_pack_list(self, list) :
412
392
"""
413
393
self .total = len (list ) * 2
414
394
self .display_counter ("Caching Files" )
415
- do_queue (Cacher , self .cache_pdsc_and_pack , list )
395
+ do_queue (Reader , self .cache_pdsc_and_pack , list )
416
396
stdout .write ("\n " )
417
397
418
398
def pdsc_from_cache (self , url ) :
0 commit comments