From 6ce1ac37d871b972e765d433490e12b242c5068c Mon Sep 17 00:00:00 2001 From: Sergio Date: Thu, 17 Aug 2023 09:27:46 +0200 Subject: [PATCH] zxdb.py: ahora usa cache para obtener datos --- zxdb.py | 54 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/zxdb.py b/zxdb.py index cf2ebdf..15dec71 100644 --- a/zxdb.py +++ b/zxdb.py @@ -5,13 +5,15 @@ import mysql.connector import requests import time import random +import shutil from mysql.connector import errorcode from urllib.parse import urlparse from urllib.request import urlretrieve -destination_path = r"/home/sergio/zx-loading/" -url_prefix = r"https://spectrumcomputing.co.uk/" +destination_path = r'/home/sergio/zx/loading_screens/' +url_prefix = r'https://spectrumcomputing.co.uk/' +cache_path = r'/home/sergio/zx/cache/' files = [] @@ -66,27 +68,49 @@ def url_filename(url): return filename -def download_file(file): - downloaded_file = url_filename(file) - destination_filename = os.path.join(destination_path, downloaded_file) - if not os.path.isfile(destination_filename): - r = requests.get(file) - with open(destination_filename, "wb") as f: - f.write(r.content) - print("downloaded : {:{width}}".format(downloaded_file, width=50)) - time.sleep(random.randint(4, 8)) - else: - print("skipping : {:{width}}".format(downloaded_file, width=50)) +def download_file(file, dest): + r = requests.get(file) + with open(dest, "wb") as f: + f.write(r.content) def download_all(): + cont = 0 + total = len(files) for file in files: - download_file(file) + cont = cont + 1 + downloaded_file = url_filename(file) + destination_filename = os.path.join(destination_path, downloaded_file) + if not os.path.isfile(destination_filename): + download_file(file, destination_filename) + print("downloaded : {:{width}} ({} / {})".format(downloaded_file, cont, total, width=50)) + time.sleep(random.randint(4, 8)) + else: + print("skipping : {:{width}} ({} / {})".format(downloaded_file, cont, total, width=50)) + +def get_files(): + cont = 0 + total = len(files) + for file in files: + cont = cont + 1 + downloaded_file = url_filename(file) + destination_filename = os.path.join(destination_path, downloaded_file) + cache_filename = os.path.join(cache_path, downloaded_file) + if not os.path.isfile(destination_filename): + if os.path.isfile(cache_filename): + shutil.copyfile(cache_filename, destination_filename) + print("cached : {:{width}} ({} / {})".format(downloaded_file, cont, total, width=50)) + else: + download_file(file, destination_filename) + print("downloaded : {:{width}} ({} / {})".format(downloaded_file, cont, total, width=50)) + time.sleep(random.randint(4, 8)) + else: + print("skipping : {:{width}} ({} / {})".format(downloaded_file, cont, total, width=50)) def main(): connect() - download_all() + get_files() if __name__ == "__main__":