Files
HDB/gallery-dl/gallery-dl.py
2024-07-21 16:18:54 +02:00

136 lines
4.0 KiB
Python

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 artist;
DROP TABLE IF EXISTS website;
DROP TABLE IF EXISTS links;
""")
conn.executescript("""
CREATE TABLE IF NOT EXISTS artist (
id integer NOT NULL CONSTRAINT artist_pk PRIMARY KEY AUTOINCREMENT,
name text NOT NULL
);
CREATE TABLE IF NOT EXISTS website (
id integer NOT NULL CONSTRAINT website_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,
website_id integer NOT NULL,
artist_id integer NOT NULL,
CONSTRAINT links_artist FOREIGN KEY (artist_id)
REFERENCES artist (id),
CONSTRAINT links_website FOREIGN KEY (website_id)
REFERENCES website (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 website(name) VALUES('{website_name}')")
conn.execute(f"INSERT INTO artist(name) VALUES('{artist}')")
sql = f"SELECT id FROM artist WHERE name like '{artist}'"
artist_id = conn.execute(sql).fetchone()[0]
sql = f"SELECT id FROM website 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, website_id, artist_id) VALUES({values})"
conn.execute(sql)
def import_website(website_name):
file_name = f"downloaded_{website_name.lower()}.txt"
file_contents = open(file_name).readlines()
for line in file_contents:
if line.startswith("#"):
continue
if "#" not in line:
continue
url = line.split("# ")[0].rstrip()
artist = line.split("# ")[1].rstrip()
name_needed(url, artist)
insert_into_db(website_name, url, artist)
break # do only on for now
def read_db():
result = conn.execute("""
SELECT
l.url,
w.name,
a.name
FROM links l
INNER JOIN website w ON l.website_id = w.id
INNER JOIN artist a ON l.artist_id = a.id
""")
return result.fetchall()
def galleryDl(url, website_name, artist_name):
directory = f"--directory Artists/{artist_name}/{website_name}"
command = f"gallery-dl --config gallery-dl/config.json {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()
conn.close()
for row in db_contents:
print(row)
# galleryDl(row[0], row[1], row[2])