zxdb.py: ahora usa cache para obtener datos

This commit is contained in:
2023-08-17 09:27:46 +02:00
parent 1ef08bea08
commit 6ce1ac37d8
+37 -13
View File
@@ -5,13 +5,15 @@ import mysql.connector
import requests import requests
import time import time
import random import random
import shutil
from mysql.connector import errorcode from mysql.connector import errorcode
from urllib.parse import urlparse from urllib.parse import urlparse
from urllib.request import urlretrieve from urllib.request import urlretrieve
destination_path = r"/home/sergio/zx-loading/" destination_path = r'/home/sergio/zx/loading_screens/'
url_prefix = r"https://spectrumcomputing.co.uk/" url_prefix = r'https://spectrumcomputing.co.uk/'
cache_path = r'/home/sergio/zx/cache/'
files = [] files = []
@@ -66,27 +68,49 @@ def url_filename(url):
return filename return filename
def download_file(file): def download_file(file, dest):
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) r = requests.get(file)
with open(destination_filename, "wb") as f: with open(dest, "wb") as f:
f.write(r.content) 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_all(): def download_all():
cont = 0
total = len(files)
for file in 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(): def main():
connect() connect()
download_all() get_files()
if __name__ == "__main__": if __name__ == "__main__":