|
| 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 | +import tempfile |
| 18 | + |
| 19 | +import requests |
| 20 | + |
| 21 | + |
| 22 | +class Response: |
| 23 | + """ |
| 24 | + Represent the response from fetching a URL with: |
| 25 | +- `location`: the absolute location of the files that was fetched |
| 26 | +- `content_type`: content type of the file |
| 27 | +- `size`: size of the retrieved content in bytes |
| 28 | +- `url`: fetched URL |
| 29 | + """ |
| 30 | + def __init__(self, location, content_type, size, url): |
| 31 | + self.location = location |
| 32 | + self.content_type = content_type |
| 33 | + self.size = size |
| 34 | + self.url = url |
| 35 | + |
| 36 | + |
| 37 | +def fetch(url): |
| 38 | + """ |
| 39 | + Return a `Response` object built from fetching the content at the `url` URL string. |
| 40 | + """ |
| 41 | + r = requests.get(url) |
| 42 | + |
| 43 | + temp = tempfile.NamedTemporaryFile(delete=False) |
| 44 | + filename = temp.name |
| 45 | + |
| 46 | + with open(filename,'wb') as f: |
| 47 | + f.write(r.content) |
| 48 | + |
| 49 | + resp = Response(location=filename, |
| 50 | + content_type=r.headers['content-type'], |
| 51 | + size=int(r.headers['content-length']), |
| 52 | + url=url) |
| 53 | + |
| 54 | + return resp |
0 commit comments