-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackpopulate_video_downloads.py
74 lines (62 loc) · 2.63 KB
/
backpopulate_video_downloads.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
"""
Move 16:9 transcoded videos into the correct S3 paths for syncing, and update the resource
metadata to point to that path for downloads.
""" # noqa: E501
import os
from django.conf import settings
from django.core.management import CommandParser
from mitol.common.utils import now_in_utc
from content_sync.tasks import sync_unsynced_websites
from main.management.commands.filter import WebsiteFilterCommand
from videos.api import prepare_video_download_file
from videos.models import Video
script_path = os.path.dirname(os.path.realpath(__file__)) # noqa: PTH120
class Command(WebsiteFilterCommand):
"""
Move 16:9 transcoded videos into the correct S3 paths for syncing, and update the resource
metadata to point to that path for downloads.
""" # noqa: E501
help = __doc__
def add_arguments(self, parser: CommandParser) -> None:
super().add_arguments(parser)
parser.add_argument(
"-ss",
"--skip-sync",
dest="skip_sync",
action="store_true",
default=False,
help="Whether to skip running the sync_unsynced_websites task",
)
def handle(self, *args, **options):
"""
Run the command
"""
super().handle(*args, **options)
is_verbose = options["verbosity"] > 1
videos = Video.objects.all()
self.filter_videos(videos=videos)
self.stdout.write(
f"Updating downloadable video files for {videos.count()} videos."
)
for video in videos:
if is_verbose:
self.stdout.write(
f"Updating video {video.source_key} for site {video.website.short_id}" # noqa: E501
)
try:
prepare_video_download_file(video)
except Exception as exc: # pylint:disable=broad-except # noqa: BLE001
self.stderr.write(
f"Error Updating video {video.source_key} for site {video.website.short_id}: {exc}" # noqa: E501
)
self.stdout.write(
f"Completed Updating downloadable video files for {videos.count()} videos."
)
if settings.CONTENT_SYNC_BACKEND and not options["skip_sync"]:
self.stdout.write("Syncing all unsynced websites to the designated backend")
start = now_in_utc()
task = sync_unsynced_websites.delay(create_backends=True)
self.stdout.write(f"Starting task {task}...")
task.get()
total_seconds = (now_in_utc() - start).total_seconds()
self.stdout.write(f"Backend sync finished, took {total_seconds} seconds")