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
+39 -15
View File
@@ -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__":