## Script para descargar pantallas de carga de spectrum a partir de zxdb import os 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_screens/' url_prefix = r'https://spectrumcomputing.co.uk/' cache_path = r'/home/sergio/zx/cache/' files = [] def select1(cursor): query = "SELECT id, title FROM entries WHERE id BETWEEN %s AND %s" id_start = 1950 id_end = 1980 cursor.execute(query, (id_start, id_end)) for id, title in cursor: print("{} ({})".format(title, id)) def select2(cursor): query = "select file_link from downloads where filetype_id=1" cursor.execute(query) for file_link in cursor: files.append(url_prefix + str(file_link)[3:-3]) def connect(): config = { "user": "root", "password": "unJEPimbJddHP8", "host": "127.0.0.1", "database": "zxdb", "raise_on_warnings": True, } try: connection = mysql.connector.connect(**config) cursor = connection.cursor() select2(cursor) except mysql.connector.Error as err: if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: print("Something is wrong with your user name or password") elif err.errno == errorcode.ER_BAD_DB_ERROR: print("Database does not exist") else: print(err) finally: if connection.is_connected(): connection.close() cursor.close() def url_filename(url): parsed_url = urlparse(url) path = parsed_url.path filename = os.path.basename(path) return filename 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: 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() get_files() if __name__ == "__main__": main()