zxdb.py actualizado

This commit is contained in:
2023-08-14 13:43:19 +02:00
parent 874ca12fcf
commit 1ef08bea08
2 changed files with 88 additions and 33 deletions
+1
View File
@@ -1 +1,2 @@
.DS_Store
settings.json
+77 -23
View File
@@ -1,16 +1,50 @@
import mysql.connector
from mysql.connector import errorcode
## 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
"user": "root",
"password": "unJEPimbJddHP8",
"host": "127.0.0.1",
"database": "zxdb",
"raise_on_warnings": True,
}
try:
cnx = mysql.connector.connect(**config)
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")
@@ -19,21 +53,41 @@ except mysql.connector.Error as err:
else:
print(err)
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))
finally:
if connection.is_connected():
connection.close()
cursor.close()
cnx.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()