|
| 1 | +# fetchcode is a free software tool from nexB Inc. and others. |
| 2 | +# Visit https://github.com/nexB/fetchcode for support and download. |
| 3 | +# |
| 4 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 5 | +# http://nexb.com and http://aboutcode.org |
| 6 | +# |
| 7 | +# This software is licensed under the Apache License version 2.0. |
| 8 | +# |
| 9 | +# You may not use this software except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at: |
| 11 | +# http://apache.org/licenses/LICENSE-2.0 |
| 12 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 13 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 14 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations under the License. |
| 16 | + |
| 17 | +from ftplib import FTP |
| 18 | +from mimetypes import MimeTypes |
| 19 | +import os |
| 20 | +import tempfile |
| 21 | +from urllib.parse import urlparse |
| 22 | + |
| 23 | +import requests |
| 24 | + |
| 25 | + |
| 26 | +class Response: |
| 27 | + def __init__(self, location, content_type, size, url): |
| 28 | + """ |
| 29 | + Represent the response from fetching a URL with: |
| 30 | + - `location`: the absolute location of the files that was fetched |
| 31 | + - `content_type`: content type of the file |
| 32 | + - `size`: size of the retrieved content in bytes |
| 33 | + - `url`: fetched URL |
| 34 | + """ |
| 35 | + self.url = url |
| 36 | + self.size = size |
| 37 | + self.content_type = content_type |
| 38 | + self.location = location |
| 39 | + |
| 40 | + |
| 41 | +def fetch_http(url, location): |
| 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` |
| 45 | + """ |
| 46 | + r = requests.get(url) |
| 47 | + with open(location, 'wb') as f: |
| 48 | + f.write(r.content) |
| 49 | + |
| 50 | + content_type = r.headers.get('content-type') |
| 51 | + size = r.headers.get('content-length') |
| 52 | + size = int(size) if size else None |
| 53 | + |
| 54 | + resp = Response(location=location, content_type=content_type, size=size, url=url) |
| 55 | + |
| 56 | + return resp |
| 57 | + |
| 58 | + |
| 59 | +def fetch_ftp(url, location): |
| 60 | + """ |
| 61 | + Return a `Response` object built from fetching the content at a FTP based `url` URL string |
| 62 | + saving the content in a file at `location` |
| 63 | + """ |
| 64 | + url_parts = urlparse(url) |
| 65 | + |
| 66 | + netloc = url_parts.netloc |
| 67 | + path = url_parts.path |
| 68 | + dir, file = os.path.split(path) |
| 69 | + |
| 70 | + ftp = FTP(netloc) |
| 71 | + ftp.login() |
| 72 | + |
| 73 | + size = ftp.size(path) |
| 74 | + mime = MimeTypes() |
| 75 | + mime_type = mime.guess_type(file) |
| 76 | + if mime_type: |
| 77 | + content_type = mime_type[0] |
| 78 | + else: |
| 79 | + content_type = None |
| 80 | + |
| 81 | + ftp.cwd(dir) |
| 82 | + file = 'RETR {}'.format(file) |
| 83 | + with open(location, 'wb') as f: |
| 84 | + ftp.retrbinary(file, f.write) |
| 85 | + ftp.close() |
| 86 | + |
| 87 | + resp = Response(location=location, content_type=content_type, size=size, url=url) |
| 88 | + return resp |
| 89 | + |
| 90 | + |
| 91 | +def fetch(url): |
| 92 | + """ |
| 93 | + Return a `Response` object built from fetching the content at the `url` URL string and store content at a temporary file. |
| 94 | + """ |
| 95 | + |
| 96 | + temp = tempfile.NamedTemporaryFile(delete=False) |
| 97 | + location = temp.name |
| 98 | + |
| 99 | + url_parts = urlparse(url) |
| 100 | + scheme = url_parts.scheme |
| 101 | + |
| 102 | + fetchers = {'ftp': fetch_ftp, 'http': fetch_http, 'https': fetch_http} |
| 103 | + |
| 104 | + if scheme in fetchers: |
| 105 | + return fetchers.get(scheme)(url, location) |
| 106 | + |
| 107 | + raise Exception('Not a supported/known scheme.') |
0 commit comments