forked from CERNDocumentServer/cds-videos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
559 lines (469 loc) · 18.2 KB
/
Copy pathutils.py
File metadata and controls
559 lines (469 loc) · 18.2 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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2017, 2018, 2019 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.
"""Helper methods for CDS records."""
import json
import re
from datetime import timedelta
from html import unescape
from urllib import parse
import six
from flask import current_app, g, request
from flask_security import current_user
from invenio_db import db
from invenio_files_rest.models import as_bucket
from invenio_files_rest.tasks import remove_file_data
from invenio_indexer.utils import schema_to_index
from invenio_jsonschemas import current_jsonschemas
from invenio_pidstore.errors import PersistentIdentifierError
from invenio_pidstore.models import PersistentIdentifier
from invenio_pidstore.providers.datacite import DataCiteProvider
from invenio_pidstore.resolver import Resolver
from invenio_records.api import Record
from invenio_records.models import RecordMetadata
from invenio_records_files.models import RecordsBuckets
from invenio_search import current_search
from invenio_search.engine import search
from six.moves.html_parser import HTMLParser
from sqlalchemy_continuum import version_class
from ..deposit.fetcher import deposit_fetcher
from .fetchers import recid_fetcher
def schema_prefix(schema):
"""Get index prefix for a given schema."""
if not schema:
return None
index = schema_to_index(schema, index_names=current_search.mappings.keys())
return index.split("-")[0]
def is_record(record):
"""Determine if a record is a bibliographic record."""
return schema_prefix(record.get("$schema")) == "records"
def is_deposit(record):
"""Determine if a record is a deposit record."""
return schema_prefix(record.get("$schema")) == "deposits"
def is_project_record(record):
project_schema = current_jsonschemas.url_to_path(record["$schema"])
return "records/videos/project/project-v" in project_schema
def lowercase_value(value):
"""Lowercase value if not an integer.
This function is used when we compare user's identity groups and record's
stored `_access` values. If the value is not a string, it is considered the
user ID (integer) and thus we return it unchanged.
"""
lowercase_value = ""
try:
lowercase_value = value.lower()
except AttributeError:
# Add the user ID (integer) to the list
lowercase_value = value
return lowercase_value
def get_user_provides():
"""Extract the user's provides from g."""
return [lowercase_value(need.value) for need in g.identity.provides]
def remove_html_tags(html_tag_remover, value):
"""Remove any HTML tags."""
html_tag_remover.reset()
html_tag_remover.feed(value)
return html_tag_remover.get_data()
def format_pid_link(url_template, pid_value):
"""Format a pid url."""
if request:
return url_template.format(
host=request.host, scheme=request.scheme, pid_value=pid_value
)
else:
r = parse(current_app.config["THEME_SITEURL"])
return url_template.format(host=r.netloc, scheme=r.scheme, pid_value=pid_value)
class HTMLTagRemover(HTMLParser):
"""Remove all HTML tags by keeping only the value within the tag."""
values = []
def reset(self):
"""Reset the list of values."""
HTMLParser.reset(self)
self.values = []
def handle_data(self, data):
"""Append only the value within the tags."""
self.values.append(data)
def get_data(self):
"""Return only values."""
return "".join(self.values)
def unescape(self, value):
"""HTMLParser removes `unescape` method. Use directly `html.unescape()`."""
return unescape(value)
def _get_record_and_deposit(record_uuid):
"""Find a record and it's deposit from the record UUID."""
from ..deposit.api import Project, Video
from ..deposit.api import is_project_record as is_project_deposit
record = Record.get_record(record_uuid)
deposit = None
if is_record(record):
deposit_cls = Project if is_project_record(record) else Video
try:
depid = deposit_fetcher(None, record)
_, deposit = Resolver(
pid_type=depid.pid_type,
object_type="rec",
getter=deposit_cls.get_record,
).resolve(depid.pid_value)
except (PersistentIdentifierError, AttributeError):
# there is no deposit associated with the record
pass
else:
deposit_cls = Project if is_project_deposit(record) else Video
deposit = deposit_cls(record, model=record.model)
try:
record = deposit.fetch_published()
except (PersistentIdentifierError, KeyError):
record = None
return record, deposit
def delete_project_record(record_uuid, reason=None, hard=False):
"""Delete project."""
from ..deposit.api import Video
report = []
_, deposit = _get_record_and_deposit(record_uuid)
if deposit:
videos = deposit.videos
# Delete each video first
for video in videos:
report.append(("INFO", "Removing Video {}".format(video.id)))
if "pid" in video["_deposit"]:
# Find published video
record_pid = recid_fetcher(None, video)
video_pid, deposit = Resolver(
pid_type=record_pid.pid_type,
object_type="rec",
getter=Record.get_record,
).resolve(record_pid.pid_value)
else:
# Find deposit
depid = deposit_fetcher(None, video)
video_pid, deposit = Resolver(
pid_type=depid.pid_type,
object_type="rec",
getter=Video.get_record,
).resolve(depid.pid_value)
report.extend(delete_video_record(video_pid.object_uuid, reason, hard))
# Save all changes made so far
db.session.commit()
if hard:
report.extend(wipe_record(record_uuid))
else:
report.extend(delete_record(record_uuid, reason=reason))
# Save all changes made so far
db.session.commit()
return report
def delete_video_record(record_uuid, reason=None, hard=False):
"""Delete video."""
from invenio_indexer.api import RecordIndexer
report = []
_, deposit = _get_record_and_deposit(record_uuid)
if deposit:
# Start deleting the deposit
if hard:
deposit._delete_flows()
report.append(
(
"INFO",
"Deleted all flows for deposit {}.".format(deposit.id),
)
)
project = deposit.project
project_uuid = project.id
if project.is_published():
try:
project = project.edit()
project._delete_videos([deposit.ref])
project.publish().commit()
report.append(
(
"INFO",
"Removed Video {0} from project {1}.".format(
deposit.id, project.id
),
)
)
except Exception as e:
report.append(
(
"WARN",
"Couldn't remove Video from project. {}".format(e),
)
)
else:
try:
project._delete_videos([deposit.ref])
project.commit()
report.append(
(
"INFO",
"Removed Video {0} from project {1}.".format(
deposit.id, project.id
),
)
)
except Exception as e:
report.append(
(
"WARN",
"Couldn't remove Video from project. {}".format(e),
)
)
# Save all changes made so far
db.session.commit()
# Reindex the project to delete the video reference
report.append(("INFO", "Reindexing project."))
RecordIndexer().index_by_id(project_uuid)
if hard:
report.extend(wipe_record(record_uuid))
else:
report.extend(delete_record(record_uuid, reason))
return report
def _delete_doi(record):
"""Mark DOI as deleted."""
if not "doi" in record:
return
try:
doi = PersistentIdentifier.get("doi", record["doi"])
dcp = DataCiteProvider.get(doi.pid_value)
dcp.delete()
return ("INFO", "DOI deleted for record {}".format(doi))
except Exception as e:
return (
"WARN",
"Couldn't delete DOI from record {0} - {1}".format(record.id, e),
)
def wipe_record(record_uuid):
"""Delete completely a record from the system."""
from invenio_indexer.api import RecordIndexer
report = [("INFO", "Wiping record {}".format(record_uuid))]
file_ids = []
for record in _get_record_and_deposit(record_uuid):
if record is None:
continue
uuid = record.id
# Remove the record from index
try:
RecordIndexer().delete(record)
report.append(("INFO", "Deleted record from index."))
except search.NotFoundError:
report.append(("WARN", "Couldn't delete record from index."))
# Delete PIDs
try:
PersistentIdentifier.query.filter(
PersistentIdentifier.object_uuid == uuid,
PersistentIdentifier.pid_type != "doi",
).delete()
report.append(("INFO", "Deleted PIDs from record."))
except Exception as e:
report.append(("ERROR", "Couldn't delete PIDs from record - {}.".format(e)))
report.append(_delete_doi(record))
# Delete record bucket reference
record_bucket = RecordsBuckets.query.filter(
RecordsBuckets.record_id == uuid
).one_or_none()
try:
RecordsBuckets.query.filter(RecordsBuckets.record_id == uuid).delete()
report.append(("INFO", "Deleted RecordBucket from record."))
except Exception as e:
report.append(
(
"ERROR",
"Couldn't delete RecordBucket from record - {}.".format(e),
)
)
# Delete metadata and versions
try:
RecordMetadataVersion = version_class(RecordMetadata)
db.session.query(RecordMetadataVersion).filter(
RecordMetadataVersion.id == uuid
).delete()
RecordMetadata.query.filter(RecordMetadata.id == uuid).delete()
report.append(("INFO", "Deleted record metadata and versions."))
except Exception as e:
report.append(("ERROR", "Couldn't delete record metadata - {}.".format(e)))
# Delete Files
file_ids = []
if record_bucket:
bucket = as_bucket(record_bucket.bucket_id)
record_bucket.bucket.locked = False
# Make files writable
for obj in bucket.objects:
# skip if file is None (due to a previous soft deletion)
if not obj.file:
continue
file_ids.append(str(obj.file.id))
obj.file.writable = True
db.session.add(obj.file)
try:
bucket.remove()
report.append(("INFO", "Deleted bucket."))
except Exception as e:
report.append(("ERROR", "Couldn't delete bucket- {}.".format(e)))
db.session.commit()
# Completely delete files
for file_id in file_ids:
try:
task = remove_file_data.delay(file_id)
report.append(
(
"INFO",
"File {0} deleted from disk by task {1}.".format(uuid, task.id),
)
)
except Exception as e:
report.append(("ERROR", "Couldn't delete file from disk - {}.".format(e)))
return report
def delete_record(record_uuid, reason):
"""Delete record and store the deleting reason within the JSON."""
from invenio_indexer.api import RecordIndexer
report = [("INFO", "Deleting record {0} - {1}".format(record_uuid, reason))]
for record in _get_record_and_deposit(record_uuid):
if record is None:
continue
uuid = record.id
# Mark all pid as deleted
for pid in PersistentIdentifier.query.filter(
PersistentIdentifier.object_uuid == uuid,
PersistentIdentifier.pid_type != "doi",
):
try:
pid.delete()
except Exception as e:
report.append(("ERROR", "Couldn't delete PID {}".format(e)))
report.append(_delete_doi(record))
# Remove the record from index
try:
RecordIndexer().delete(record)
report.append(("INFO", "Deleted record from index."))
except search.NotFoundError:
report.append(("WARN", "Couldn't delete record from index."))
record_bucket = RecordsBuckets.query.filter(
RecordsBuckets.record_id == uuid
).one_or_none()
try:
RecordsBuckets.query.filter(RecordsBuckets.record_id == uuid).delete()
report.append(("INFO", "Deleted RecordBucket from record."))
except Exception as e:
report.append(
(
"ERROR",
"Couldn't delete RecordBucket from record - {}.".format(e),
)
)
if record_bucket:
bucket = as_bucket(record_bucket.bucket_id)
bucket.locked = False
try:
bucket.remove()
report.append(("INFO", "Deleted bucket."))
except Exception as e:
report.append(("ERROR", "Couldn't delete bucket- {}.".format(e)))
if is_record(record):
# Clear the record and put the deletion information
removal_reasons = dict(current_app.config["CDS_REMOVAL_REASONS"])
if reason in removal_reasons:
reason = removal_reasons[reason]
try:
record.clear()
record.update(
{
"removal_reason": reason,
"removed_by": int(current_user.get_id()),
}
)
record.commit()
report.append(("INFO", "Update record content with {}".format(reason)))
except Exception as e:
report.append(("ERROR", "Couldn't update record content {}".format(e)))
else:
# Completely delete the deposit
try:
record.model.json = None
db.session.merge(record.model)
report.append(("INFO", "Delete deposit content."))
except Exception as e:
report.append(("ERROR", "Couldn't delete deposit content {}".format(e)))
db.session.commit()
return report
def to_string(value):
"""Ensure that the input value is returned as a string."""
if isinstance(value, six.string_types):
return value
else:
return json.dumps(value)
def parse_video_chapters(description):
"""Parse YouTube-style chapter timestamps from video description.
Looks for patterns like:
00:00 Introduction
0:30 Getting Started
1:23:45 Advanced Topics
Args:
description (str): Video description text
Returns:
list: List of chapter dicts with 'timestamp', 'seconds', and 'title' keys
"""
html_tag_remover = HTMLTagRemover()
if not description:
return []
# Regex pattern to match timestamp formats:
# - 0:00, 00:00, 0:0, 00:0, 0:00:00, 00:00:00, etc.
# - Followed by optional space/tab and chapter title
pattern = r'(?:^|\n)\s*(\d{1,2}:(?:\d{1,2}:)?\d{1,2})\s*[-\s]*(.+?)(?=\n|$)'
chapters = []
matches = re.findall(pattern, description, re.MULTILINE)
for timestamp_str, title in matches:
# Parse timestamp to seconds
time_parts = timestamp_str.split(':')
if len(time_parts) == 2: # MM:SS format
minutes, seconds = map(int, time_parts)
total_seconds = minutes * 60 + seconds
elif len(time_parts) == 3: # HH:MM:SS format
hours, minutes, seconds = map(int, time_parts)
total_seconds = hours * 3600 + minutes * 60 + seconds
else:
continue
# Clean up title
title = remove_html_tags(html_tag_remover, title).strip()
if title:
chapters.append({
'timestamp': timestamp_str,
'seconds': total_seconds,
'title': title
})
# Sort chapters by timestamp
chapters.sort(key=lambda x: x['seconds'])
return chapters
def seconds_to_timestamp(seconds):
"""Convert seconds to timestamp string (MM:SS or HH:MM:SS).
Args:
seconds (int): Number of seconds
Returns:
str: Formatted timestamp string
"""
td = timedelta(seconds=seconds)
hours = td.seconds // 3600
minutes = (td.seconds % 3600) // 60
secs = td.seconds % 60
if hours > 0:
return f"{hours}:{minutes:02d}:{secs:02d}"
else:
return f"{minutes}:{secs:02d}"