-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
database.py
225 lines (182 loc) · 5.96 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from peewee import (
BooleanField,
CharField,
DoesNotExist,
ForeignKeyField,
IntegerField,
Model,
TextField,
)
from playhouse.apsw_ext import APSWDatabase
from gutenberg2zim.constants import logger
timeout = 10
db = APSWDatabase(
"gutenberg.db",
pragmas=(
("journal_mode", "WAL"),
("cache_size", 10000),
("mmap_size", 1024 * 1024 * 32),
),
timeout=timeout,
)
db.connect()
db.execute_sql("PRAGMA journal_mode=WAL;")
class BaseModel(Model):
@classmethod
def get_or_none(cls, *query, **kwargs):
try:
return cls.get(*query, **kwargs)
except DoesNotExist:
return None
class License(BaseModel):
class Meta:
database = db
fixtures = (
{"slug": "PD", "name": "Public domain in the USA."},
{"slug": "None", "name": "None"},
{
"slug": "Copyright",
"name": "Copyrighted. Read the copyright "
"notice inside this book "
"for details.",
},
)
slug = CharField(max_length=20, primary_key=True)
name = CharField()
def __unicode__(self):
return self.name
class Author(BaseModel):
class Meta:
database = db
fixtures = (
{
"gut_id": "116",
"last_name": "Various",
},
{
"gut_id": "216",
"last_name": "Anonymous",
},
)
gut_id = CharField(primary_key=True, max_length=100)
last_name = CharField(max_length=150)
first_names = CharField(max_length=300, null=True)
birth_year = CharField(max_length=10, null=True)
death_year = CharField(max_length=10, null=True)
def __unicode__(self):
return self.name()
def fname(self):
return f"{self.name()}.{self.gut_id}"
def name(self):
def sanitize(text):
return text.strip().replace("/", "-")[:230]
if not self.first_names and not self.last_name:
return sanitize("Anonymous")
if not self.first_names:
return sanitize(self.last_name)
if not self.last_name:
return sanitize(self.first_names)
return sanitize(f"{self.first_names} {self.last_name}")
def to_dict(self):
return {
"label": self.name(),
"id": self.gut_id,
"last_name": self.last_name,
"first_names": self.first_names,
"birth_year": self.birth_year,
"death_year": self.death_year,
}
def to_array(self):
return [
self.name(),
self.gut_id,
# self.last_name,
# self.first_names,
# self.birth_year,
# self.death_year,
]
class Book(BaseModel):
class Meta:
database = db
id = IntegerField(primary_key=True) # noqa: A003
title = CharField(max_length=500)
subtitle = CharField(max_length=500, null=True)
author = ForeignKeyField(Author, related_name="books")
license = ForeignKeyField(License, related_name="books") # noqa: A003
language = CharField(max_length=10)
downloads = IntegerField(default=0)
bookshelf = CharField(max_length=500, null=True)
cover_page = IntegerField(default=0)
popularity = 0
html_etag = CharField(max_length=500, null=True)
epub_etag = CharField(max_length=500, null=True)
cover_etag = CharField(max_length=500, null=True)
def __unicode__(self):
return f"{self.id}/{self.title}/{self.bookshelf}"
def to_dict(self):
return {
"title": self.title,
"subtitle": self.subtitle,
"author": self.author.name(),
"license": self.license,
"language": self.language,
"downloads": self.downloads,
"bookshelf": self.bookshelf,
"cover_page": self.cover_page,
}
def to_array(self, all_requested_formats):
fmts = self.requested_formats(all_requested_formats)
return [
self.title,
# self.subtitle,
self.author.name(),
# self.license,
# self.language,
# self.downloads
"{html}{epub}{pdf}".format(
html=int("html" in fmts),
epub=int("epub" in fmts),
pdf=int("pdf" in fmts),
),
self.id,
self.bookshelf,
]
def formats(self):
from gutenberg2zim.utils import main_formats_for
return main_formats_for(self)
def requested_formats(self, all_requested_formats):
fmts = self.formats()
requested_formats = [fmt for fmt in fmts if fmt in all_requested_formats]
return requested_formats
class BookFormat(BaseModel):
class Meta:
database = db
book = ForeignKeyField(Book, related_name="bookformats")
mime = CharField(max_length=100)
images = BooleanField(default=True)
pattern = CharField(max_length=100)
downloaded_from = CharField(max_length=300, null=True)
def __unicode__(self):
return f"[{self.mime}] {self.book.title}"
class Url(BaseModel):
class Meta:
database = db
url = TextField(index=True)
def __unicode__(self):
return self.url
def load_fixtures(model):
logger.info(f"Loading fixtures for {model._meta.name}")
for fixture in getattr(model._meta, "fixtures", []):
f = model.create(**fixture)
logger.debug(f"[fixtures] Created {f}")
def setup_database(*, wipe=False):
logger.info("Setting up the database")
for model in (License, Author, Book, BookFormat, Url):
if wipe:
model.drop_table(fail_silently=True)
if not model.table_exists():
model.create_table()
logger.debug(f"Created table for {model._meta.name}") # type: ignore
load_fixtures(model)
else:
logger.debug(f"{model._meta.name} table already exists.") # type: ignore