From 1ef08bea088596880d003fe58c0c62917dd08ccc Mon Sep 17 00:00:00 2001 From: Sergio Date: Mon, 14 Aug 2023 13:43:19 +0200 Subject: [PATCH] zxdb.py actualizado --- .gitignore | 1 + zxdb.py | 120 ++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 88 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index e43b0f9..6f5ace0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .DS_Store +settings.json \ No newline at end of file diff --git a/zxdb.py b/zxdb.py index d90ecba..cf2ebdf 100644 --- a/zxdb.py +++ b/zxdb.py @@ -1,39 +1,93 @@ +## 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 - -config = { - 'user': 'root', - 'password': 'unJEPimbJddHP8', - 'host': '127.0.0.1', - 'database': 'zxdb', - 'raise_on_warnings': True -} - -try: - cnx = mysql.connector.connect(**config) -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) +from urllib.parse import urlparse +from urllib.request import urlretrieve -cursor = cnx.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)) - -cursor.close() -cnx.close() +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()