def gtfs_download(self, url, dt, zone): """Do downloading of one file.""" print("Downloading", self.slug, url, zone, dt) # Use only standard library functions to avoid dependencies. #furl = urllib.urlopen(url) opener = FancyURLopener() # We have to set up an authentication method on the opener if # we will need to authenticate. This does HTTP BASIC only so # far. if 'authentication' in self.data: auth_name = self.data['authentication'] auth = auth_data['sites'][auth_name] # A callback method which performs the authentication. # Return (user, pass) tuple. opener.prompt_user_passwd = \ lambda host, realm: (auth['username'], auth['password']) # URL parameters auth method if 'url_suffix' in auth: url = url + auth['url_suffix'] if "{API_KEY}" in url: try: auth_name = self.data['authentication'] except KeyError: auth_name = self.name auth = auth_data['sites'][auth_name] url = url.format(API_KEY=auth['API_KEY']) # Make GTFS path. gtfs_path = self.path_gtfszip(dt, zone) util.makedirs(os.path.dirname(gtfs_path)) # Open the URL. print("**** Connecting to %s" % url) # Open GTFS and relay data from web to file. with util.create_file(gtfs_path) as tmp_gtfs_path: opener.retrieve(url, tmp_gtfs_path) self.test_corrupted_zip(gtfs_path)