-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b91d9c1
commit 0564066
Showing
7 changed files
with
177 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from imagehash import whash, ImageHash | ||
from videohash import VideoHash | ||
from videohash.utils import ( | ||
create_and_return_temporary_directory as mk_temp_dir, | ||
does_path_exists | ||
) | ||
from PIL import Image | ||
from os.path import join, sep | ||
from pathlib import Path | ||
from asyncio import get_event_loop | ||
from motor.motor_asyncio import AsyncIOMotorGridOut | ||
|
||
Image.ANTIALIAS = Image.Resampling.LANCZOS | ||
|
||
|
||
class VHashPatch(VideoHash): | ||
hash: ImageHash | ||
_file: AsyncIOMotorGridOut | ||
|
||
def __init__(self, *args, **kwargs): | ||
file, *_ = args | ||
self._file = file | ||
super().__init__(*args, **kwargs) | ||
|
||
def _calc_hash(self): self.hash = whash(self.image) | ||
|
||
def _create_required_dirs_and_check_for_errors(self): | ||
if not self.storage_path: self.storage_path = mk_temp_dir() | ||
|
||
assert does_path_exists(self.storage_path), f"Storage path '{self.storage_path}' does not exist." | ||
|
||
self.storage_path = join(self.storage_path, (f"{self.task_uid}{sep}")) | ||
|
||
self.video_dir = join(self.storage_path, (f"video{sep}")) | ||
Path(self.video_dir).mkdir(parents=True, exist_ok=True) | ||
|
||
self.video_download_dir = join(self.storage_path, (f"downloadedvideo{sep}")) | ||
Path(self.video_download_dir).mkdir(parents=True, exist_ok=True) | ||
|
||
self.frames_dir = join(self.storage_path, (f"frames{sep}")) | ||
Path(self.frames_dir).mkdir(parents=True, exist_ok=True) | ||
|
||
self.tiles_dir = join(self.storage_path, (f"tiles{sep}")) | ||
Path(self.tiles_dir).mkdir(parents=True, exist_ok=True) | ||
|
||
self.collage_dir = join(self.storage_path, (f"collage{sep}")) | ||
Path(self.collage_dir).mkdir(parents=True, exist_ok=True) | ||
|
||
self.horizontally_concatenated_image_dir = join( | ||
self.storage_path, | ||
f"horizontally_concatenated_image{sep}" | ||
) | ||
Path(self.horizontally_concatenated_image_dir).mkdir( | ||
parents=True, | ||
exist_ok=True | ||
) | ||
|
||
def _copy_video_to_video_dir(self): | ||
assert self._file.metadata, "No file meta to read" | ||
|
||
extension = self._file.metadata.get("file_extension") | ||
self.video_path = join(self.video_dir, f"video.{extension}") | ||
|
||
get_event_loop().run_until_complete(self._write_file()) | ||
|
||
async def _write_file(self): | ||
with open(self.video_path, "wb") as file: | ||
self._file.seek(0) | ||
file.write(await self._file.read()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,37 @@ | ||
from celery import Celery | ||
from shared.settings import BROKER_URL, RESULT_URL, CELERY_CONFIG | ||
from shared.worker_services import Zipper | ||
from shared.worker_services import Zipper, Hasher | ||
from asyncio import get_event_loop | ||
|
||
if not BROKER_URL or not RESULT_URL: raise ValueError("no broker environment") | ||
|
||
WORKER: Celery = Celery() | ||
worker: Celery = Celery(**CELERY_CONFIG) | ||
|
||
WORKER.conf.broker_url = BROKER_URL | ||
WORKER.conf.result_backend = RESULT_URL | ||
|
||
|
||
@WORKER.task(name="produce_download_task") | ||
@worker.task(name="produce_download_task") | ||
def produce_download_task(bucket_name: str, file_ids: list[str]) -> str | None: | ||
zipper: Zipper = Zipper(bucket_name, file_ids) | ||
task: Zipper = Zipper(bucket_name, file_ids) | ||
loop = get_event_loop() | ||
|
||
async def _inner(): | ||
await task.archive_objects() | ||
await task.write_archive() | ||
task.delete_temp_zip() | ||
|
||
loop.run_until_complete(_inner()) | ||
return task.archive_id | ||
|
||
|
||
worker.task(name="produce_handle_media_task") | ||
def produce_handle_media_task(bucket_name: str, uid: str) -> None: | ||
loop = get_event_loop() | ||
task: Hasher = Hasher(bucket_name, uid) | ||
|
||
async def _inner(): | ||
await zipper.archive_objects() | ||
await zipper.write_archive() | ||
zipper.delete_temp_zip() | ||
await task.get_file() | ||
await task.hash() | ||
|
||
loop.run_until_complete(_inner()) | ||
return zipper.archive_id | ||
|
||
|
||
if __name__ == "__main__": WORKER.worker_main(argv=CELERY_CONFIG) | ||
if __name__ == "__main__": worker.start() |