## Script para descargar pantallas de carga de spectrum a partir de zxdb import os import mysql.connector import requests import time import random 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/" 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): 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_all(): for file in files: download_file(file) def main(): connect() download_all() if __name__ == "__main__": main()