ComicOPDS/opds/catalog.py

260 lines
10 KiB
Python
Raw Normal View History

2022-06-07 09:16:43 +02:00
import os
from uuid import uuid4
from urllib.parse import quote
from jinja2 import Environment, FileSystemLoader, select_autoescape
from .entry import Entry
from .link import Link
2022-06-07 16:16:18 +02:00
import sqlite3,json
2023-01-19 15:41:27 +01:00
import config
2024-01-29 19:59:33 +01:00
import extras
2022-06-07 09:16:43 +02:00
class Catalog(object):
def __init__(
self,
title,
id=None,
author_name=None,
author_uri=None,
root_url=None,
url=None,
):
self.title = title
self.id = id or uuid4()
self.author_name = author_name
self.author_uri = author_uri
self.root_url = root_url
self.url = url
self.entries = []
def add_entry(self, entry):
self.entries.append(entry)
def render(self):
env = Environment(
loader=FileSystemLoader(
searchpath=os.path.join(os.path.dirname(__file__), "templates")
),
autoescape=select_autoescape(["html", "xml"]),
)
template = env.get_template("catalog.opds.jinja2")
return template.render(catalog=self)
def fromsearch(root_url, url, content_base_path, content_relative_path):
c = Catalog(
title="test"
)
return c
def fromdir(root_url, url, content_base_path, content_relative_path):
2024-01-29 19:59:33 +01:00
2022-06-07 09:16:43 +02:00
path = os.path.join(content_base_path, content_relative_path)
2022-06-07 16:16:18 +02:00
if os.path.basename(content_relative_path) == "":
c = Catalog(
title="Comics",
root_url=root_url,
url=url
)
else:
c = Catalog(
2024-01-29 19:59:33 +01:00
title=extras.xmlesc(os.path.basename(content_relative_path)),
root_url=root_url,
url=url
)
#title=os.path.basename(os.path.dirname(path)), root_url=root_url, url=url
2022-06-07 16:16:18 +02:00
##########WORKING AREA###########
searchArr=[]
if c.url.endswith("/catalog"):
with open('test.json') as fi:
data=json.load(fi)
print("--> LOADED FILE") # try and get this as low as possible.
#searchArr=["Girl","Bat","Part One"]
for e in data:
for key, value in e.items():
searchArr.append(key)
print(searchArr)
######################
2023-01-19 15:41:27 +01:00
2022-06-07 16:16:18 +02:00
2022-06-07 09:16:43 +02:00
if not "search" in c.url:
onlydirs = [
f for f in os.listdir(path) if not os.path.isfile(os.path.join(path, f))
]
2024-01-29 20:16:28 +01:00
onlydirs.sort()
print(onlydirs)
2022-06-07 09:16:43 +02:00
for dirname in onlydirs:
2024-01-29 20:16:28 +01:00
print(dirname)
2022-06-07 09:16:43 +02:00
link = Link(
2023-01-19 15:41:27 +01:00
href=quote(f"/catalog/{content_relative_path}/{dirname}").replace('//','/'), #windows fix
2022-06-07 09:16:43 +02:00
rel="subsection",
rpath=path,
type="application/atom+xml;profile=opds-catalog;kind=acquisition",
)
2024-01-29 19:59:33 +01:00
c.add_entry(Entry(title=extras.xmlesc(dirname), id=uuid4(), links=[link]))
2022-06-07 09:16:43 +02:00
if c.url.endswith("/catalog"):
2022-06-07 09:41:45 +02:00
for i in searchArr:
link2 = Link(
href=quote(f"/catalog/search["+i+"]"),
rel="subsection",
rpath=path,
type="application/atom+xml;profile=opds-catalog;kind=acquisition",
)
2023-01-19 15:41:27 +01:00
c.add_entry(Entry(title="["+i+"]",id=uuid4(),links=[link2]))
2022-06-07 09:16:43 +02:00
if not "search" in c.url:
onlyfiles = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
onlyfiles.sort()
2022-06-07 09:16:43 +02:00
for filename in onlyfiles:
2024-01-29 20:16:28 +01:00
if not filename.endswith('cbz'):
continue
2022-06-07 09:16:43 +02:00
link = Link(
href=quote(f"/content/{content_relative_path}/{filename}"),
rel="http://opds-spec.org/acquisition",
rpath=path,
type=mimetype(filename),
)
2024-01-29 19:59:33 +01:00
#c.add_entry(Entry(title=filename.rsplit(".",1)[0], id=uuid4(), links=[link]))
c.add_entry(Entry(title=extras.xmlesc(filename).rsplit(".",1)[0], id=uuid4(), links=[link]))
2022-06-07 09:16:43 +02:00
#fixed issue with multiple . in filename
#print(c.render())
else:
2022-06-07 16:16:18 +02:00
with open('test.json') as fi:
data=json.load(fi)
config._print("--> LOADED 2 FILE") # try and get this as low as possible.
2022-06-07 16:16:18 +02:00
for e in data:
for key, value in e.items():
config._print(key)
2022-06-07 16:16:18 +02:00
searchArr.append(key)
2022-06-07 09:41:45 +02:00
for i in searchArr:
config._print("i (in searchArr): " + i)
config._print("quote i: " + quote(f""+i))
2022-06-07 09:41:45 +02:00
if quote(f""+i) in c.url:
conn = sqlite3.connect('app.db')
2022-06-07 16:16:18 +02:00
for e in data:
config._print("e (in data): " + str(e))
2022-06-07 16:16:18 +02:00
for key, value in e.items():
config._print("key: " + key)
2022-06-07 16:28:58 +02:00
if key == i:
config._print("key <" + str(key) + "> matches <" + str(i) + ">")
2022-06-07 16:28:58 +02:00
query="SELECT * FROM COMICS where "
for h in value:
2022-06-07 16:28:58 +02:00
first=True
for j,k in h.items():
2022-06-07 16:28:58 +02:00
if j == 'SQL':
query = query + k
if k != '' and j != "SQL":
config._print(j)
config._print(k)
config._print(query)
if not first and j != 'limit':
2022-06-07 16:28:58 +02:00
query = query + "and "
config._print(query)
2022-06-07 16:28:58 +02:00
if type(k) == list:
config._print(k)
2022-06-07 16:28:58 +02:00
if j == "series" or j == "title":
firstS = True
query = query + "("
config._print(query)
2022-06-07 16:28:58 +02:00
for l in k:
if not firstS:
query = query + "or "
config._print(query)
2022-06-07 16:28:58 +02:00
query = query + j + " like '%" + l + "%' "
config._print(query)
2022-06-07 16:28:58 +02:00
if firstS:
firstS = False
query = query + ") "
config._print(query)
2022-06-07 16:28:58 +02:00
else:
query = query + j + " in ("
config._print(query)
2022-06-07 16:28:58 +02:00
firstL = True
for l in k:
if not firstL:
query = query + ","
config._print(query)
query = query + "'" + str(l) + "'"
config._print(query)
2022-06-07 16:28:58 +02:00
if firstL:
firstL = False
query = query + ") "
config._print(query)
2022-06-07 16:28:58 +02:00
elif j != 'limit':
query = query + j + " like '%" + str(k) + "%' "
config._print(query)
elif j == 'limit':
config.DEFAULT_SEARCH_NUMBER = k
2022-06-07 16:28:58 +02:00
else:
print(">>>>>>>>>>>ERROR THIS SHOULD NOT HAPPEN<<<<<<<<<<<")
2022-06-07 16:28:58 +02:00
if first:
first = False
query = query + " order by series asc, cast(issue as unsigned) asc "
if config.DEFAULT_SEARCH_NUMBER != 0:
query = query + "LIMIT " + str(config.DEFAULT_SEARCH_NUMBER) + ";"
else:
query = query + ";"
break
else:
config._print("key <" + str(key) + "> DOES NOT match <" + str(i) + ">")
config._print("----> " + query)
2022-06-07 16:28:58 +02:00
sql = query
#sql="SELECT * from COMICS where SERIES like '%" + i+ "%' or Title like '%" + i+ "%';"
#config._print(sql)
2022-06-07 09:41:45 +02:00
s = conn.execute(sql)
2022-06-07 16:28:58 +02:00
#list=[]
2022-06-07 09:41:45 +02:00
for r in s:
#config._print(r)
2023-01-19 15:41:27 +01:00
tUrl=f""+r[7].replace('\\','/').replace(config.WIN_DRIVE_LETTER + ':','').replace(config.CONTENT_BASE_DIR,"/content")
#config._print(tUrl)
2022-06-07 09:41:45 +02:00
tTitle=r[6]
link3 = Link(
#href=quote(f"/content/DC Comics/Earth Cities/Gotham City/Batgirl/Annual/(2012) Batgirl Annual/Batgirl Annual #001 - The Blood That Moves Us [December, 2012].cbz"),
href=quote(tUrl),
rel="http://opds-spec.org/acquisition",
rpath=path,
type="application/x-cbz",
2022-06-07 09:16:43 +02:00
)
#config._print(link3.href)
2022-06-07 09:41:45 +02:00
c.add_entry(
Entry(
title=tTitle,
id=uuid4(),
links=[link3]
)
)
2023-01-17 15:00:37 +01:00
#print(c.title)
2022-06-07 09:16:43 +02:00
return c
2023-01-17 15:00:37 +01:00
2022-06-07 09:16:43 +02:00
def mimetype(path):
extension = path.split(".")[-1].lower()
if extension == "pdf":
return "application/pdf"
elif extension == "epub":
return "application/epub"
elif extension == "mobi":
return "application/mobi"
elif extension == "cbz":
return "application/x-cbz"
else:
return "application/unknown"