forked from wpoa/open-access-media-importer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oa-cache
executable file
·427 lines (387 loc) · 14.9 KB
/
oa-cache
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from os import listdir, path, remove, rename
from os.path import exists, join, isfile
from os import rename
from sys import argv, exit, stderr, stdout
import errno
# import gobject, pygst
# pygst.require("0.10")
# import gst
import logging
import progressbar
import mutagen
import mutagen.oggtheora
import datetime
import pprint
import subprocess
from helpers import autovividict # media
from helpers import make_datestring
from helpers import filename_from_url
from model import session
# from model import setup_all
# from model import create_all
from model import set_source
from model import Article, Category, Journal, SupplementaryMaterial
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
try:
action = argv[1]
target = argv[2]
except IndexError:
print(
"""
oa-cache – Open Access Media Importer local operations
usage: oa-cache browse-database [source] |
oa-cache clear-media [source] |
oa-cache clear-database [source] |
oa-cache convert-media [source] |
oa-cache find-media [source] |
oa-cache forget-converted [source] |
oa-cache forget-downloaded [source] |
oa-cache forget-uploaded [source] |
oa-cache print-database-path [source]
oa-cache stats [source]
"""
)
exit(1)
try:
assert action in [
"browse-database",
"clear-media",
"clear-database",
"convert-media",
"find-media",
"forget-converted",
"forget-downloaded",
"forget-uploaded",
"print-database-path",
"stats",
]
except AssertionError: # invalid action
print("Unknown action “%s”.\n" % action)
exit(2)
try:
exec("from sources import %s as source_module" % target)
except ImportError: # ungültige Quelle
print("Unbekannte Quelle “%s”.\n" % target)
exit(3)
set_source(target)
# setup_all(True)
from helpers import config
if action == "browse-database":
filename = config.database_path(target)
try:
subprocess.call(["sqlite:///", filename])
except OSError:
print(
"Unable to start sqlitebrowser <http://sqlitebrowser.sourceforge.net/>.\n"
)
exit(4)
if action == "clear-media":
media_raw_directory = config.get_media_refined_source_path(target)
listing = listdir(media_raw_directory)
metadata_refined_directory = config.get_metadata_refined_source_path(target)
download_cache_path = path.join(metadata_refined_directory, "download_cache")
remove(download_cache_path)
for filename in listing:
media_path = path.join(media_raw_directory, filename)
print("Removing “%s” … " % media_path)
remove(media_path)
print("done.\n")
if action == "clear-database":
filename = config.database_path(target)
print("Removing “%s” … " % filename)
try:
remove(filename)
print("done.\n")
except OSError as e:
print("\n%s\n" % str(e))
# Erstellen Sie eine SQLAlchemy Engine und Session
engine = create_engine("sqlite:///mydata.sqlite")
Session = sessionmaker(bind=engine)
session = Session()
if action == "convert-media":
materials = (
session.query(SupplementaryMaterial)
# .filter_by(downloaded=True, converted=False)
.all()
)
print(session)
print(len(materials))
for material in materials:
media_refined_directory = config.get_media_refined_source_path(target)
media_raw_directory = config.get_media_raw_source_path(target)
temporary_media_path = join(media_refined_directory, "current.ogg")
filename = filename_from_url(material.url)
media_raw_path = join(media_raw_directory, filename)
media_refined_path = os.path.splitext(media_raw_path)[0] + ".ogg"
if material.converting:
print(
"Skipping conversion of “%s”, earlier attempt failed.\n"
% media_raw_path.encode("utf-8")
)
continue
if isfile(media_refined_path):
print(
"Skipping conversion of “%s”, exists at “%s”.\n"
% (media_raw_path.encode("utf-8"), media_refined_path.encode("utf-8"))
)
material.converted = True
session.commit()
continue
material.converting = True
session.commit()
print(
"Converting “%s”, saving into “%s” … "
% (media_raw_path.encode("utf-8"), media_refined_path.encode("utf-8"))
)
# Konvertierung mit ffmpeg durchführen
command = [
"ffmpeg",
"-i",
media_raw_path,
"-c:v",
"libtheora",
"-q:v",
"5",
media_refined_path,
]
try:
subprocess.run(command, check=True)
print("Conversion successful")
material.converted = True
session.commit()
except subprocess.CalledProcessError as e:
print("Error occurred during conversion:", e)
material.converting = False
session.commit()
try:
m = media.Media(media_raw_path)
m.find_streams()
m.convert(temporary_media_path)
except RuntimeError as e:
logging.error(
"%s: Skipping conversion of “%s”.", e, media_raw_path.encode("utf-8")
)
continue
try:
f = mutagen.oggtheora.OggTheora(temporary_media_path)
for key, value in [
("TITLE", material.title),
("ALBUM", material.article.title),
("ARTIST", material.article.contrib_authors),
("COPYRIGHTS", material.article.copyright_holder),
("LICENSE", material.article.license_url),
("DESCRIPTION", material.caption),
(
"DATE",
datetime.date(
material.article.article_year,
material.article.article_month,
material.article.article_day,
).strftime("%Y-%m-%d"),
),
]:
if value is not None:
f[key] = value
else:
logging.warning("Missing metadata: %s.", key)
f.save()
except mutagen.oggtheora.OggTheoraHeaderError:
pass # Most probably an encoding failure.
rename(temporary_media_path, media_refined_path)
print("done.\n")
material.converting = False
material.converted = True
session.commit()
if action == "forget-converted":
materials = SupplementaryMaterial.query.filter_by(converted=True).all()
print("Forgetting conversion of %s materials … " % len(materials))
for material in materials:
material.converted = False
session.commit()
print("done.\n")
if action == "forget-downloaded":
materials = SupplementaryMaterial.query.filter_by(downloaded=True).all()
print("Forgetting download of %s materials … " % len(materials))
for material in materials:
material.downloaded = False
session.commit()
print("done.\n")
if action == "forget-uploaded":
materials = SupplementaryMaterial.query.filter_by(uploaded=True).all()
print("Forgetting upload of %s materials … " % len(materials))
for material in materials:
material.uploaded = False
session.commit()
print("done.\n")
if action == "find-media":
# skip = [article.name for article in session.query.Article.all()]
# if len(skip) > 0:
# print("Skipping %s records … \n" % len(skip))
source_path = config.get_metadata_raw_source_path(target)
print(source_module)
for result in source_module.list_articles(
source_path, supplementary_materials=True # ,# skip=skip
):
try:
journal = Journal.title == result["journal-title"]
if not journal:
journal = Journal(title=result["journal-title"])
article = Article.title = (result["article-title"],)
contrib_authors = (result["article-contrib-authors"],)
if not article:
# if there is a whitelist, skip non-whitelisted content
doi = result["doi"]
try:
doi_prefix = doi.split("/")[0]
except AttributeError:
print("Skipping Article “%s”, as it has no DOI.\n" % result["name"])
continue
if config.whitelist_doi and doi_prefix not in config.whitelist_doi:
print(
"Skipping DOI %s, prefix %s is not in whitelist.\n"
% (doi, doi_prefix)
)
continue
article = Article(
name=result["name"],
doi=doi,
title=result["article-title"],
contrib_authors=result["article-contrib-authors"],
abstract=result["article-abstract"],
year=result["article-year"],
month=result["article-month"],
day=result["article-day"],
url=result["article-url"],
license_url=result["article-license-url"],
license_text=result["article-license-text"],
copyright_statement=result["article-copyright-statement"],
copyright_holder=result["article-copyright-holder"],
journal=journal,
)
print(result)
# for category_name in result["article-categories"]:
# category = Category.name == category_name
# if not category:
# category = Category(name=category_name)
# category.articles.append(article)
materials = result["supplementary-materials"]
if materials:
print("“%s”:\n" % result["article-title"].encode("utf-8"))
mimetypes = {}
for material in materials:
mimetype = material["mimetype"] + "/" + material["mime-subtype"]
try:
mimetypes[mimetype] += 1
except KeyError:
mimetypes[mimetype] = 1
supplementary_material = SupplementaryMaterial.url = material["url"]
if not supplementary_material:
supplementary_material = SupplementaryMaterial(
label=material["label"],
title=material["title"],
caption=material["caption"],
mimetype=material["mimetype"],
mime_subtype=material["mime-subtype"],
url=material["url"],
article=article,
)
for mimetype in mimetypes.keys():
print("\t%s × %s\n" % (mimetypes[mimetype], mimetype))
print("\n")
session.commit()
except KeyboardInterrupt:
print("Saving database …\n")
session.commit()
exit(0)
if action == "print-database-path":
filename = config.database_path(target)
stdout.write(filename)
if action == "stats":
print("Counting supplementary materials … ")
materials = SupplementaryMaterial.query.all()
print(str(len(materials)) + " supplementary materials found.\n")
p = progressbar.ProgressBar(maxval=len(materials)).start()
completed = 0
licenses = {
"free": autovividict(),
"non-free": autovividict(),
}
licensing_publishers = {
"url": autovividict(),
"url-from-text": autovividict(),
"text": autovividict(),
"none": autovividict(),
}
mimetypes = {
"free": autovividict(),
"non-free": autovividict(),
"misreported": autovividict(),
}
mimetypes_publishers = {
"correct": autovividict(),
"incorrect": autovividict(),
"unknown": autovividict(),
}
mimetypes_prefix_publishers = {"free": autovividict(), "non-free": autovividict()}
for material in materials:
license_url = material.article.license_url
license_text = material.article.license_text
copyright_statement = material.article.copyright_statement
doi = material.article.doi
if doi is not None:
doi_prefix = doi.split("/")[0]
else:
doi_prefix = "None"
mimetype = material.mimetype
mimetype_composite = mimetype + "/" + material.mime_subtype
try:
mimetype_composite_reported = (
material.mimetype_reported + "/" + material.mime_subtype_reported
)
if mimetype_composite == "application/msword":
mimetypes_publishers["unknown"][doi_prefix] += 1
elif mimetype_composite != mimetype_composite_reported:
mimetypes["misreported"][mimetype_composite][
mimetype_composite_reported
] += 1
mimetypes_publishers["incorrect"][doi_prefix] += 1
else: # mimetype is correct
mimetypes_publishers["correct"][doi_prefix] += 1
except TypeError: # oa-get update-mimetypes was not run
mimetypes_publishers["unknown"][doi_prefix] += 1
if license_url in config.free_license_urls:
licenses["free"][license_url] += 1
mimetypes["free"][mimetype_composite] += 1
mimetypes_prefix_publishers["free"][mimetype][doi_prefix] += 1
else:
licenses["non-free"][license_url] += 1
mimetypes["non-free"][mimetype_composite] += 1
mimetypes_prefix_publishers["non-free"][mimetype][doi_prefix] += 1
if license_url is not None:
if license_text is not None or copyright_statement is not None:
licensing_publishers["url-from-text"][doi_prefix] += 1
else: # URL was given, no text lookup necessary
licensing_publishers["url"][doi_prefix] += 1
else:
if license_text is not None or copyright_statement is not None:
licensing_publishers["text"][doi_prefix] += 1
else: # no licensing information at all
licensing_publishers["none"][doi_prefix] += 1
completed += 1
p.update(completed)
print("\n")
pp = pprint.PrettyPrinter(indent=4)
output = pp.pformat(
{
"licenses": licenses,
"licensing_publishers": licensing_publishers,
"mimetypes": mimetypes,
"mimetypes_publishers": mimetypes_publishers,
"mimetypes_prefix_publishers": mimetypes_prefix_publishers,
}
)
stdout.write(output + "\n")