def link(section, file_regex, curversion): if str(curversion) == '0': utils.debug(2, 'Initial search for %s' % section, utils.whoami()) url = 'https://distrowatch.com/index.php?distribution=' + section + '&release=all&month=all&year=all' else: utils.debug(2, 'Searching for %s' % section, utils.whoami()) url = 'https://distrowatch.com/' match = regex(url, file_regex) if match: isolink = cleanuplink(match) try: meta, url = utils.linkinfo(isolink) except Exception as e: utils.debug(5, "Error %s" % e, utils.whoami()) return filename = utils.file_name(url) if meta.get('Content-Type').split(';')[0] == 'text/html': print('Download link for %s is an intermediary website.' % filename) print('Will retry getting a download link.') match = regex(url, file_regex) if len(match) == 0: print( 'Failed to get a download link from the intermediary website. Quitting.' ) else: isolink = cleanuplink(match) return isolink else: return isolink
def mainloop(): utils.debug(2, '-----mainloop(start)-----', utils.whoami()) for i in dllist: curversion = str(utils.config.get(i, 'current_version')) downloadcfg = str(utils.config.get(i, 'download')) file_regex = str(utils.config.get(i, 'file_regex')) linkprovider = str(utils.config.get(i, 'link_provider')) chkresult = '' if str(downloadcfg) == '1': try: create = importlib.import_module(linkprovider) dllink = create.link(i, file_regex, curversion) except ModuleNotFoundError: print('No link provider %s found.' % linkprovider) print('Please check your config again.') sys.exit(2) except Exception as e: print('There was an exception in the %s module.' % linkprovider) print(e) sys.exit(2) if dllink: utils.debug(2, 'Download link is %s' % dllink, utils.whoami()) file_name = utils.file_name(dllink) utils.debug(2, 'Filename is %s' % file_name, utils.whoami()) if file_name == curversion: utils.debug( 1, 'We already have the current version for %s.' % i, utils.whoami()) else: dlinfo = download.download(dllink, dlpath) if hasattr(create, 'chkurl'): utils.debug( 3, 'Starting checksum check for %s' % linkprovider, utils.whoami()) chkresult = create.chksum(file_name, dlpath) # utils.linkinfo may return an Exception with HTTP Error code. Dont update current_version then. if not utils.is_number(dlinfo) and chkresult is not 'Fail': utils.config.set(i, 'current_version', file_name) utils.debug( 2, 'Current_version is: ' + utils.config.get(i, 'current_version'), utils.whoami()) with open(utils.configfile, 'w') as configfile: utils.config.write(configfile) elif utils.is_number(dlinfo): print('Download failed with HTTP error code: %s' % dlinfo) else: print('Download failed due to checksum mismatch.') else: utils.debug(4, 'No download link found for %s' % i, utils.whoami()) else: utils.debug(2, 'Download for %s is set to off.' % i, utils.whoami()) utils.debug(2, '-----mainloop(stop)-----', utils.whoami())
def main(dlist, path, dbglevel): print("Starting up. Reading Config.") utils.debug(1, 'Config is: %s' % utils.configfile, utils.whoami()) if len(path) == 0: raise ValueError('You must specify a path in the %s configfile.' % utils.configfile) utils.debug(1, 'Download folder is: ' + path, utils.whoami()) utils.debug(1, 'Downloads in config are:', utils.whoami()) utils.debug(1, '\n'.join(dlist) + '\n', utils.whoami()) intervalsec = timecalc(intdenom, interval) utils.debug(3, 'Interval is %s seconds' % intervalsec, utils.whoami()) mainloop() if not dbglevel >= 5: startscheduler(intervalsec, dbglevel) else: print('') print('----- We are running in one-off mode. (debuglevel >= 5) -----')
def link(section, file_regex, curversion): url = 'https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/' utils.debug(2, 'Searching for %s' % section, utils.whoami()) match = regex(url, file_regex) if match: isolink = combinelink(match, url) meta, isolink = utils.linkinfo(isolink) return isolink
def download(url, path): meta, url = utils.linkinfo(url) filename = utils.file_name(url) try: file_size = int(meta.get('Content-Length')) file_size = ((file_size / 1024) / 1024) / 1024 print("Downloading %s with size %s GB" % (filename, file_size)) except Exception as e: print("It was exception type: %s" % e) print("Downloading %s" % filename) urllib.request.urlretrieve(url, path + filename) utils.debug(1, "Download for %s finished" % filename, utils.whoami())