]>
git.ipfire.org Git - location/location-database.git/blob - tools/downloader.py
2 ###############################################################################
4 # location-database - A database to determine someone's #
5 # location on the Internet #
6 # Copyright (C) 2018 Michael Tremer #
8 # This program is free software: you can redistribute it and/or modify #
9 # it under the terms of the GNU General Public License as published by #
10 # the Free Software Foundation, either version 3 of the License, or #
11 # (at your option) any later version. #
13 # This program is distributed in the hope that it will be useful, #
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16 # GNU General Public License for more details. #
18 # You should have received a copy of the GNU General Public License #
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
21 ###############################################################################
28 log
= logging
.getLogger("downloader")
31 class Downloader(object):
32 USER_AGENT
= "location-database/1.0"
37 def set_proxy(self
, url
):
39 Sets a HTTP proxy that is used to perform all requests
41 log
.info("Using proxy %s" % url
)
44 def request(self
, url
, data
=None):
45 req
= urllib
.request
.Request(url
, data
=data
)
49 req
.set_proxy(self
.proxy
, "http")
53 req
.add_header("User-Agent", self
.USER_AGENT
)
55 return DownloaderContext(self
, req
)
58 class DownloaderContext(object):
59 def __init__(self
, downloader
, request
):
60 self
.downloader
= downloader
61 self
.request
= request
63 # Save the response object
67 log
.info("Retrieving %s..." % self
.request
.full_url
)
70 self
.response
= urllib
.request
.urlopen(self
.request
)
72 # Log the response headers
73 log
.debug("Response Headers:")
74 for header
in self
.headers
:
75 log
.debug(" %s: %s" % (header
, self
.get_header(header
)))
79 def __exit__(self
, type, value
, traceback
):
84 Makes the object iterable by going through each block
88 for line
in self
.body
:
90 for charset
in ("utf-8", "latin1"):
92 line
= line
.decode(charset
)
93 except UnicodeDecodeError:
101 # Skip commented lines
102 if line
.startswith("#"):
109 # End the block on an empty line
119 return self
.response
.headers
121 def get_header(self
, name
):
123 return self
.headers
.get(name
)
128 Returns a file-like object with the decoded content
131 content_type
= self
.get_header("Content-Type")
133 # Decompress any gzipped response on the fly
134 if content_type
in ("application/x-gzip", "application/gzip"):
135 return gzip
.GzipFile(fileobj
=self
.response
, mode
="rb")
137 # Return the response by default
141 if __name__
== "__main__":
144 # Enable debug logging
145 logging
.basicConfig(level
=logging
.DEBUG
)
149 for url
in sys
.argv
[1:]:
150 print("Downloading %s..." % url
)
152 with d
.request(url
) as r
: