14
14
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
15
15
# specific language governing permissions and limitations under the License.
16
16
17
- from ftplib import FTP
18
- from mimetypes import MimeTypes
19
17
import os
20
18
import tempfile
19
+ from ftplib import FTP
20
+ from mimetypes import MimeTypes
21
21
from urllib .parse import urlparse
22
22
23
23
import requests
@@ -40,19 +40,18 @@ def __init__(self, location, content_type, size, url):
40
40
41
41
def fetch_http (url , location ):
42
42
"""
43
- Return a `Response` object built from fetching the content at a HTTP/HTTPS based `url` URL string
44
- saving the content in a file at `location`
43
+ Return a `Response` object built from fetching the content at a HTTP/HTTPS based
44
+ `url` URL string saving the content in a file at `location`
45
45
"""
46
46
r = requests .get (url )
47
- with open (location , 'wb' ) as f :
47
+ with open (location , "wb" ) as f :
48
48
f .write (r .content )
49
49
50
- content_type = r .headers .get (' content-type' )
51
- size = r .headers .get (' content-length' )
50
+ content_type = r .headers .get (" content-type" )
51
+ size = r .headers .get (" content-length" )
52
52
size = int (size ) if size else None
53
53
54
- resp = Response (location = location ,
55
- content_type = content_type , size = size , url = url )
54
+ resp = Response (location = location , content_type = content_type , size = size , url = url )
56
55
57
56
return resp
58
57
@@ -80,19 +79,19 @@ def fetch_ftp(url, location):
80
79
content_type = None
81
80
82
81
ftp .cwd (dir )
83
- file = ' RETR {}' .format (file )
84
- with open (location , 'wb' ) as f :
82
+ file = " RETR {}" .format (file )
83
+ with open (location , "wb" ) as f :
85
84
ftp .retrbinary (file , f .write )
86
85
ftp .close ()
87
86
88
- resp = Response (location = location ,
89
- content_type = content_type , size = size , url = url )
87
+ resp = Response (location = location , content_type = content_type , size = size , url = url )
90
88
return resp
91
89
92
90
93
91
def fetch (url ):
94
92
"""
95
- Return a `Response` object built from fetching the content at the `url` URL string and store content at a temporary file.
93
+ Return a `Response` object built from fetching the content at the `url` URL string and
94
+ store content at a temporary file.
96
95
"""
97
96
98
97
temp = tempfile .NamedTemporaryFile (delete = False )
@@ -101,9 +100,9 @@ def fetch(url):
101
100
url_parts = urlparse (url )
102
101
scheme = url_parts .scheme
103
102
104
- fetchers = {' ftp' : fetch_ftp , ' http' : fetch_http , ' https' : fetch_http }
103
+ fetchers = {" ftp" : fetch_ftp , " http" : fetch_http , " https" : fetch_http }
105
104
106
105
if scheme in fetchers :
107
106
return fetchers .get (scheme )(url , location )
108
107
109
- raise Exception (' Not a supported/known scheme.' )
108
+ raise Exception (" Not a supported/known scheme." )
0 commit comments