refactor thumbnail check task

This commit is contained in:
simon 2023-03-16 17:12:50 +07:00
parent 0ab41f74dc
commit 461c2c4600
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
2 changed files with 55 additions and 45 deletions

View File

@ -11,7 +11,7 @@ from time import sleep
import requests import requests
from home.src.download import queue # partial import from home.src.download import queue # partial import
from home.src.es.connect import IndexPaginate from home.src.es.connect import ElasticWrap, IndexPaginate
from home.src.ta.config import AppConfig from home.src.ta.config import AppConfig
from mutagen.mp4 import MP4, MP4Cover from mutagen.mp4 import MP4, MP4Cover
from PIL import Image, ImageFile, ImageFilter, UnidentifiedImageError from PIL import Image, ImageFile, ImageFilter, UnidentifiedImageError
@ -272,49 +272,59 @@ class ValidatorCallback:
class ThumbValidator: class ThumbValidator:
"""validate thumbnails""" """validate thumbnails"""
def download_missing(self): INDEX = [
"""download all missing artwork""" {
self.download_missing_videos() "data": {
self.download_missing_channels()
self.download_missing_playlists()
def download_missing_videos(self):
"""get all missing video thumbnails"""
data = {
"query": {"term": {"active": {"value": True}}}, "query": {"term": {"active": {"value": True}}},
"sort": [{"youtube_id": {"order": "asc"}}],
"_source": ["vid_thumb_url", "youtube_id"], "_source": ["vid_thumb_url", "youtube_id"],
} },
paginate = IndexPaginate( "name": "ta_video",
"ta_video", data, size=5000, callback=ValidatorCallback },
) {
_ = paginate.get_results() "data": {
def download_missing_channels(self):
"""get all missing channel thumbnails"""
data = {
"query": {"term": {"channel_active": {"value": True}}}, "query": {"term": {"channel_active": {"value": True}}},
"sort": [{"channel_id": {"order": "asc"}}],
"_source": { "_source": {
"excludes": ["channel_description", "channel_overwrites"] "excludes": ["channel_description", "channel_overwrites"]
}, },
} },
"name": "ta_channel",
},
{
"data": {
"query": {"term": {"playlist_active": {"value": True}}},
"_source": ["playlist_id", "playlist_thumbnail"],
},
"name": "ta_playlist",
},
]
def __init__(self, task):
self.task = task
def validate(self):
"""validate all indexes"""
for index in self.INDEX:
total = self._get_total(index["name"])
if not total:
continue
paginate = IndexPaginate( paginate = IndexPaginate(
"ta_channel", data, callback=ValidatorCallback index_name=index["name"],
data=index["data"],
size=1000,
callback=ValidatorCallback,
task=self.task,
total=total,
) )
_ = paginate.get_results() _ = paginate.get_results()
def download_missing_playlists(self): @staticmethod
"""get all missing playlist artwork""" def _get_total(index_name):
data = { """get total documents in index"""
"query": {"term": {"playlist_active": {"value": True}}}, path = f"{index_name}/_count"
"sort": [{"playlist_id": {"order": "asc"}}], response, _ = ElasticWrap(path).get()
"_source": ["playlist_id", "playlist_thumbnail"],
} return response.get("count")
paginate = IndexPaginate(
"ta_playlist", data, callback=ValidatorCallback
)
_ = paginate.get_results()
class ThumbFilesystem: class ThumbFilesystem:

View File

@ -282,10 +282,10 @@ def rescan_filesystem(self):
manager.init(self) manager.init(self)
Filesystem(task=self).process() Filesystem(task=self).process()
ThumbValidator().download_missing() ThumbValidator(task=self).validate()
@shared_task(bind=True, name="thumbnail_check") @shared_task(bind=True, name="thumbnail_check", base=BaseTask)
def thumbnail_check(self): def thumbnail_check(self):
"""validate thumbnails""" """validate thumbnails"""
manager = TaskManager() manager = TaskManager()
@ -294,7 +294,7 @@ def thumbnail_check(self):
return return
manager.init(self) manager.init(self)
ThumbValidator().download_missing() ThumbValidator(task=self).validate()
@shared_task(bind=True, name="resync_thumbs") @shared_task(bind=True, name="resync_thumbs")