import subprocess import sqlite3 import os import urllib.parse import shutil # conn = sqlite3.connect("Hentai.sqlite3") conn = sqlite3.connect(":memory:") conn.executescript(""" DROP TABLE IF EXISTS artists; DROP TABLE IF EXISTS websites; DROP TABLE IF EXISTS links; """) conn.executescript(""" CREATE TABLE IF NOT EXISTS artists ( id integer NOT NULL CONSTRAINT artists_pk PRIMARY KEY AUTOINCREMENT, name text NOT NULL ); CREATE TABLE IF NOT EXISTS websites ( id integer NOT NULL CONSTRAINT websites_pk PRIMARY KEY AUTOINCREMENT, name text NOT NULL ); CREATE TABLE IF NOT EXISTS links ( id integer NOT NULL CONSTRAINT links_pk PRIMARY KEY AUTOINCREMENT, url text NOT NULL, websites_id integer NOT NULL, artists_id integer NOT NULL, CONSTRAINT links_artists FOREIGN KEY (artists_id) REFERENCES artists (id), CONSTRAINT links_websites FOREIGN KEY (websites_id) REFERENCES websites (id) ); """) def get_artist_from_url(url): if "rule34.xxx" in url: return urllib.parse.unquote(url)[51:] if "kemono.su" in url or "coomer.su" in url: return url.split("/")[5] return "" def mv(source, destination): if not os.path.isdir(source): return if os.path.isfile(source): shutil.copy(source, destination) shutil.rmtree(source) return for item in os.listdir(source): if not os.path.isdir(destination): os.rename(source, destination) return if os.path.isdir(f"{source}/{item}"): mv(f"{source}/{item}", f"{destination}/{item}") elif os.path.isfile(f"{destination}/{item}"): shutil.copy(f"{source}/{item}", f"{destination}/{item}") else: shutil.copy(f"{source}/{item}", f"{destination}") shutil.rmtree(source) # existing folder names may be not what is wanted, so this will take care of it def name_needed(url, artist): artist_from_url = get_artist_from_url(url) if artist_from_url == "": exit(1) mv(f"Artists/{artist_from_url}", f"Artists/{artist}") def insert_into_db(website_name, url, artist): conn.execute(f"INSERT INTO websites(name) VALUES('{website_name}')") conn.execute(f"INSERT INTO artists(name) VALUES('{artist}')") sql = f"SELECT id FROM artists WHERE name like '{artist}'" artist_id = conn.execute(sql).fetchone()[0] sql = f"SELECT id FROM websites WHERE name like '{website_name}'" website_id = conn.execute(sql).fetchone()[0] values = f"'{url}', '{website_id}', '{artist_id}'" sql = f"INSERT INTO links(url, websites_id, artists_id) VALUES({values})" conn.execute(sql) def import_website(website_name): file_contents = open(f"{website_name}.links").readlines() for line in file_contents: if line.startswith("#"): continue if "#" not in line: continue url = line.split("#")[0].strip() artist = line.split("#")[1].strip() name_needed(url, artist) insert_into_db(website_name, url, artist) def read_db(): result = conn.execute(""" SELECT l.url, w.name, a.name FROM links l INNER JOIN websites w ON l.websites_id = w.id INNER JOIN artists a ON l.artists_id = a.id """) return result.fetchall() def galleryDl(url, website_name, artist_name): base_command = "gallery-dl --config gallery-dl/config.json " directory = f"--directory Artists/{artist_name}/{website_name} " command = base_command + directory + url print(f"command: {command}") return process = subprocess.run(command, shell=True) print(f'Command \'{process.args}\' exited with {process.returncode} code') # This will not work with Rule34 as is # Rule34 will need it's artists to be added like the others import_website("Rule34") # conn.commit() db_contents = read_db() artist_count = conn.execute("select count(id) from artists") print(artist_count.fetchall()) conn.close() # exit(0) for row in db_contents: print(row) galleryDl(row[0], row[1], row[2]) break