forked from MrBunsy/random_eprints
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_eprints.py
More file actions
381 lines (318 loc) · 13.4 KB
/
random_eprints.py
File metadata and controls
381 lines (318 loc) · 13.4 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import argparse
import io
import random
from operator import indexOf
# from .random_text import RandomText
from fpdf import FPDF
# pdf = FPDF()
# pdf.add_page()
# pdf.set_font('helvetica', size=12)
# pdf.cell(text="hello world")
# pdf.output("hello_world.pdf")
import hashlib
import random
import os
from PIL import Image
from datetime import datetime, timedelta
import base64
dirname = os.path.dirname(__file__)
try:
#when imported as a python package
from .random_text import RandomText
except:
#when run locally
from random_text import RandomText
class Subjects:
def __init__(self, subjects_file_path="/opt/eprints3/archives/clocks/cfg/subjects"):
self.subjects_loc = []
self.subjects_loc_lookup = {}
self.subjects_divisions = []
self.divisions_ids = []
self.divisions_lookup = {}
# self.subjects = []
self.subject_ids = []
with open(subjects_file_path) as subjects_file:
lines = [line for line in subjects_file.readlines()]
for line in lines:
line = line.strip()
if line.startswith("#"):
continue
if len(line) == 0:
continue
if line[0].islower():
(id, text, p, d) = line.split(":")
self.subjects_divisions.append(line)
self.divisions_ids.append(id)
self.divisions_lookup[id] = text
if line[0].isupper():
self.subjects_loc.append(line)
(id, text, p, d) = line.split(":")
subject = text.replace(f"{id} ", "")
self.subjects_loc_lookup[id] = subject
# self.subjects.append(subject)
self.subject_ids.append(id)
def get_random_subject(self):
id = random.choice(self.subject_ids)
division_id = random.choice(self.divisions_ids)
return {
"id": id,
"subject": self.subjects_loc_lookup[id],
"division_id": division_id,
"division": self.divisions_lookup[division_id]
}
return random.choice(self.subjects)
# def get_division(self, subject):
# return random.choice(self.subjects_divisions)
class RandomName:
def __init__(self, preallocate=5):
self.all_names = []
firstnames = ["given_names_male", "given_names_female"]
for filename in firstnames:
with open(os.path.join(dirname,filename)) as namesfile:
names = [name.strip() for name in namesfile.readlines()]
self.all_names += names
with open(os.path.join(dirname,"surnames")) as namesfile:
self.surnames = [name.strip() for name in namesfile.readlines()]
self.preallocated_names = []
for i in range(preallocate):
self.preallocated_names.append(self._get_name())
def _get_name(self):
name = random.choice(self.all_names) + " " + random.choice(self.surnames)
#make lowercase than capitalise each word
name = name.lower().title()
return name
def get_name(self):
return random.choice(self.preallocated_names)
class RandomImage:
def __init__(self, folder_path):
self.folder_path = folder_path
#https://stackoverflow.com/a/3207973
self.images = [os.path.join(self.folder_path, image) for image in os.listdir(self.folder_path) if os.path.isfile(os.path.join(self.folder_path, image))]
def get_random_image_path(self):
return random.choice(self.images)
class RandomEPrint:
def __init__(self, textgenerator, subjects, namegen, imagegen, pdfs=1, image_count=1, contributions=True,
wordcount= 100, author_count=3, eprint_id=1, include_docs=True):
self.textgenerator =textgenerator
self.subjects = subjects
self.pdfs = pdfs
self.imagegen = imagegen
self.image_count = image_count
#3.5 style contributions if true, else 3.4 style creators
self.contributions = contributions
self.wordcount = wordcount
self.words = ""
self.author_count = author_count
self.namegen = namegen
self.eprint_id = eprint_id
self.include_docs = include_docs
self.generate()
def get_pdf(self, title, text, authors, image_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font('helvetica', size=12)
full_text=f"""{title}
by {', '.join(authors)}
{text}
"""
# pdf.multi_cell(w=pdf.epw/2, text=title)
# pdf.multi_cell(w=pdf.epw/2,text=f"by {', '.join(authors)}")
# pdf.multi_cell(w=pdf.epw/2,text=text)
image = Image.open(image_path)
image.thumbnail((300,300), Image.Resampling.LANCZOS)
pdf.image(image, x=pdf.epw * 0.6, y=10, keep_aspect_ratio=True, w=pdf.epw / 3)
pdf.multi_cell(w=pdf.epw/2, text=full_text)
# pdf.image(image_path, x=pdf.epw*0.75,y=10, keep_aspect_ratio=True,w=pdf.epw/4)
# pdf.output("test.pdf")
return pdf
def generate(self):
self.words = " ".join(self.textgenerator.get_words(self.wordcount))
self.subject_info = self.subjects.get_random_subject()
self.subject = self.subject_info["id"]
self.division = self.subject_info["division_id"]
self.publication = f"Journal of {self.subject_info['division']}"
self.volume = random.randint(1,10)
self.number = random.randint(1,4)
min_date = datetime(1950, month=1, day=1)
now = datetime.now()
self.nowstring = now.strftime("%Y-%m-%d %H:%M:%S")
self.date = min_date + (now - min_date) * random.random()
self.authors = []
while len(self.authors) < self.author_count and len(self.authors) < len(self.namegen.preallocated_names):
# for i in range(self.author_count):
new_name = self.namegen.get_name()
if new_name not in self.authors:
self.authors.append(new_name)
if not "." in self.words:
#make sure there's at least one sentance
self.words += "."
# up to the first full stop
self.title = self.words[:self.words.index(".")]
self.abstract = self.title
self.image_path = self.imagegen.get_random_image_path()
self.pdf = self.get_pdf(self.title, self.words, self.authors, self.image_path)
pdf_bytes = self.pdf.output()
# hash = hashlib.md5(pdf_bytes)
md5_hasher = hashlib.md5()
md5_hasher.update(pdf_bytes)
self.pdf_hash = md5_hasher.hexdigest()
self.pdf_size = len(pdf_bytes)
self.pdf_base64 = base64.b64encode(pdf_bytes).decode()
self.doc_id=1
def get_xml(self):
xml = f"""<eprint id='http://example.eprints-hosting.org/id/eprint/1'>
<eprintid>{self.eprint_id}</eprintid>
<rev_number>1</rev_number>
<documents>
"""
if self.include_docs:
xml +=f"""<document id='http://example.eprints-hosting.org/id/document/1'>
<docid>{self.eprint_id}</docid>
<rev_number>1</rev_number>
<files>
<file id='http://example.eprints-hosting.org/id/file/$n'>
<fileid>{self.eprint_id}</fileid>
<datasetid>document</datasetid>
<objectid>{self.eprint_id}</objectid>
<filename>document{self.eprint_id}.pdf</filename>
<mime_type>application/pdf</mime_type>
<hash>$md5</hash>
<hash_type>MD5</hash_type>
<filesize>{self.pdf_size}</filesize>
<mtime>{self.nowstring}</mtime>
<url>http://example.eprints-hosting.org/id/eprint/$n/1/$name.pdf</url>
<data encoding='base64'>{self.pdf_base64}</data>
</file>
</files>
<eprintid>{self.eprint_id}</eprintid>
<pos>{1}</pos>
<placement>1</placement>
<mime_type>application/pdf</mime_type>
<format>text</format>
<language>en</language>
<security>public</security>
<main>document{self.eprint_id}.pdf</main>
</document>
"""
xml += f"""</documents>
<eprint_status>archive</eprint_status>
<userid>1</userid>
<dir>documents/disk0/00/00/00/01</dir>
<datestamp>{self.nowstring}</datestamp>
<date>{self.date.year}</date>
<lastmod>{self.nowstring}</lastmod>
<status_changed>{self.nowstring}</status_changed>
<type>article</type>
<metadata_visibility>show</metadata_visibility>
"""
if self.contributions:
xml += " <contributions>"
for author in self.authors:
given, family = author.split(" ")
xml += f"""<item>
<type>http://www.loc.gov/loc.terms/relators/AUT</type>
<contributor>
<datasetid>person</datasetid>
<name>{family}, {given}</name>
</contributor>
</item>"""
xml += " </contributions>"
else:
xml+=" <creators>"
for author in self.authors:
given, family = author.split(" ")
email = f"{given}.{family}@example.ac.uk"
xml+=f"""<item>
<name>
<family>{family}</family>
<given>${given}</given>
</name>
<id>{email}</id>
</item>"""
xml += " </creators>"
xml +=f"""<title>{self.title}</title>
<ispublished>pub</ispublished>
<subjects>
<item>{self.subject}</item>
</subjects>
<divisions>
<item>{self.division}</item>
</divisions>
<abstract>{self.abstract}</abstract>
<publication>{self.publication}</publication>
<volume>{self.volume}</volume>
<number>{self.number}</number>
<refereed>TRUE</refereed>
</eprint>"""
return xml
def parse_args_random_eprints():
parser = argparse.ArgumentParser(description="Generate random EPrints with images and/or PDFs")
# type=str
parser.add_argument('-n', '--records', type=int, help="Number of records", default=10)
parser.add_argument('-c', '--creatorcount', type=int, help="Size of creator name pool", default=5)
parser.add_argument('-i', '--imagecount', type=int, help="Max images per record", default=1)
parser.add_argument('-p', '--pdfcount', type=int, help="Max PDFS per record", default=1)
parser.add_argument('-d', '--docs', help="Include documents", action='store_true')
parser.add_argument('-s', '--subjects', type=str, help="subjects file path", default='subjects')#/opt/eprints3/flavours/pub_lib/defaultcfg/subjects
parser.add_argument('-t', '--textfiles', type=str, help="Relative path to text file folder",
default=os.path.join(dirname,'texts'))
parser.add_argument('-f', '--tofile', type=str, help="produce file rather than stdout",)
parser.add_argument('--debug', action='store_true')
return parser.parse_args()
def get_random_eprint(seed, text_file_folder=None, subjects_file_path=None,author_count=3, **kwargs):
'''
TODO consider adding more arguments. for now, use default settings to generate a random eprint
:return:
'''
random.seed(seed)
# path relative to this file
dir_path = os.path.dirname(os.path.realpath(__file__))
if text_file_folder is None:
text_file_folder = os.path.join(dir_path, "texts")
if subjects_file_path is None:
subjects_file_path = os.path.join(dir_path, "subjects")
textfiles = [os.path.join(text_file_folder, textfile) for textfile in os.listdir(text_file_folder) if
os.path.isfile(os.path.join(text_file_folder, textfile))]
textgens = [RandomText(textfile) for textfile in textfiles]
namegen = RandomName(preallocate=author_count*2)
imagegen = RandomImage(os.path.join(dirname, "images"))
subjects = Subjects(subjects_file_path)
eprint = RandomEPrint(random.choice(textgens), subjects, namegen, imagegen,author_count=author_count, **kwargs)
return eprint
def get_xml(eprints):
final_xml = f"""<?xml version='1.0' encoding='utf-8'?>
<eprints xmlns='http://eprints.org/ep2/data/2.0'>"""
for eprint in eprints:
final_xml += eprint.get_xml()
final_xml += "</eprints>"
return final_xml
if __name__ == "__main__":
'''
TODO unify a bit with get_random_eprint.
This was originally written to be called from the command line, but can now be used as a python package as well
'''
#random.seed(1)
args = parse_args_random_eprints()
subjects = Subjects(args.subjects)
#a few different text generators, from different source material.
text_file_folder = os.path.join(dirname, args.textfiles)
textfiles = [os.path.join(text_file_folder, textfile) for textfile in os.listdir(text_file_folder) if os.path.isfile(os.path.join(text_file_folder, textfile))]
textgens = [RandomText(textfile) for textfile in textfiles]
namegen = RandomName()
imagegen = RandomImage(os.path.join(dirname,"images"))
# final_xml = f"""<?xml version='1.0' encoding='utf-8'?>
# <eprints xmlns='http://eprints.org/ep2/data/2.0'>"""
eprints = []
for i in range(args.records):
eprint = RandomEPrint(random.choice(textgens), subjects, namegen, imagegen)
eprints.append(eprint)
# final_xml += eprint.get_xml()
if args.debug:
eprint.pdf.output(f"eprint_{i}.pdf")
final_xml = get_xml(eprints)
# final_xml += "</eprints>"
if args.tofile:
with open(args.tofile, "w") as file:
file.write(final_xml)
else:
print(final_xml)