2018-10-20 22:20:31 +00:00
|
|
|
import errno
|
|
|
|
import mimetypes
|
2018-10-27 00:33:45 +00:00
|
|
|
from threading import Lock
|
|
|
|
|
|
|
|
from apscheduler.triggers.cron import CronTrigger
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2019-01-02 18:10:37 +00:00
|
|
|
from YtManagerApp.management.notification_manager import OPERATION_ID_SYNCHRONIZE
|
2018-12-30 12:45:42 +00:00
|
|
|
from YtManagerApp.scheduler import scheduler
|
2018-12-29 15:11:20 +00:00
|
|
|
from YtManagerApp.management.appconfig import appconfig
|
2018-10-20 22:20:31 +00:00
|
|
|
from YtManagerApp.management.downloader import fetch_thumbnail, downloader_process_all, downloader_process_subscription
|
2018-10-13 20:01:45 +00:00
|
|
|
from YtManagerApp.models import *
|
2018-10-29 16:52:09 +00:00
|
|
|
from YtManagerApp.utils import youtube
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-11-04 21:32:18 +00:00
|
|
|
from YtManagerApp.management import notification_manager
|
|
|
|
|
2018-10-13 20:01:45 +00:00
|
|
|
log = logging.getLogger('sync')
|
2018-10-20 22:20:31 +00:00
|
|
|
__lock = Lock()
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-29 20:45:12 +00:00
|
|
|
_ENABLE_UPDATE_STATS = True
|
2018-10-27 00:33:45 +00:00
|
|
|
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2019-01-02 18:10:37 +00:00
|
|
|
def __check_new_videos_sub(subscription: Subscription, yt_api: youtube.YoutubeAPI, progress_callback):
|
2018-10-13 20:01:45 +00:00
|
|
|
# Get list of videos
|
2018-10-29 16:52:09 +00:00
|
|
|
for item in yt_api.playlist_items(subscription.playlist_id):
|
|
|
|
results = Video.objects.filter(video_id=item.resource_video_id, subscription=subscription)
|
2018-10-13 20:01:45 +00:00
|
|
|
if len(results) == 0:
|
2018-10-29 16:52:09 +00:00
|
|
|
log.info('New video for subscription %s: %s %s"', subscription, item.resource_video_id, item.title)
|
|
|
|
Video.create(item, subscription)
|
|
|
|
|
|
|
|
if _ENABLE_UPDATE_STATS:
|
|
|
|
all_vids = Video.objects.filter(subscription=subscription)
|
|
|
|
all_vids_ids = [video.video_id for video in all_vids]
|
|
|
|
all_vids_dict = {v.video_id: v for v in all_vids}
|
|
|
|
|
|
|
|
for yt_video in yt_api.videos(all_vids_ids, part='id,statistics'):
|
|
|
|
video = all_vids_dict.get(yt_video.id)
|
2018-10-29 21:04:10 +00:00
|
|
|
|
|
|
|
if yt_video.n_likes is not None \
|
|
|
|
and yt_video.n_dislikes is not None \
|
|
|
|
and yt_video.n_likes + yt_video.n_dislikes > 0:
|
2018-10-29 16:52:09 +00:00
|
|
|
video.rating = yt_video.n_likes / (yt_video.n_likes + yt_video.n_dislikes)
|
2018-10-29 21:04:10 +00:00
|
|
|
|
2018-10-29 16:52:09 +00:00
|
|
|
video.views = yt_video.n_views
|
|
|
|
video.save()
|
2018-10-20 22:20:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def __detect_deleted(subscription: Subscription):
|
|
|
|
|
2018-12-09 23:15:10 +00:00
|
|
|
user = subscription.user
|
|
|
|
|
2018-10-20 22:20:31 +00:00
|
|
|
for video in Video.objects.filter(subscription=subscription, downloaded_path__isnull=False):
|
|
|
|
found_video = False
|
|
|
|
files = []
|
|
|
|
try:
|
|
|
|
files = list(video.get_files())
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
log.error("Could not access path %s. Error: %s", video.downloaded_path, e)
|
|
|
|
return
|
|
|
|
|
|
|
|
# Try to find a valid video file
|
|
|
|
for file in files:
|
|
|
|
mime, _ = mimetypes.guess_type(file)
|
|
|
|
if mime is not None and mime.startswith("video"):
|
|
|
|
found_video = True
|
|
|
|
|
|
|
|
# Video not found, we can safely assume that the video was deleted.
|
|
|
|
if not found_video:
|
|
|
|
log.info("Video %d was deleted! [%s %s]", video.id, video.video_id, video.name)
|
|
|
|
# Clean up
|
|
|
|
for file in files:
|
|
|
|
try:
|
|
|
|
os.unlink(file)
|
|
|
|
except OSError as e:
|
|
|
|
log.error("Could not delete redundant file %s. Error: %s", file, e)
|
|
|
|
video.downloaded_path = None
|
|
|
|
|
|
|
|
# Mark watched?
|
2018-12-29 18:43:39 +00:00
|
|
|
if user.preferences['mark_deleted_as_watched']:
|
2018-10-20 22:20:31 +00:00
|
|
|
video.watched = True
|
|
|
|
|
|
|
|
video.save()
|
2018-10-13 20:01:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def __fetch_thumbnails_obj(iterable, obj_type, id_attr):
|
|
|
|
for obj in iterable:
|
|
|
|
if obj.icon_default.startswith("http"):
|
|
|
|
obj.icon_default = fetch_thumbnail(obj.icon_default, obj_type, getattr(obj, id_attr), 'default')
|
|
|
|
if obj.icon_best.startswith("http"):
|
|
|
|
obj.icon_best = fetch_thumbnail(obj.icon_best, obj_type, getattr(obj, id_attr), 'best')
|
|
|
|
obj.save()
|
|
|
|
|
|
|
|
|
|
|
|
def __fetch_thumbnails():
|
|
|
|
log.info("Fetching subscription thumbnails... ")
|
|
|
|
__fetch_thumbnails_obj(Subscription.objects.filter(icon_default__istartswith='http'), 'sub', 'playlist_id')
|
|
|
|
__fetch_thumbnails_obj(Subscription.objects.filter(icon_best__istartswith='http'), 'sub', 'playlist_id')
|
|
|
|
|
|
|
|
log.info("Fetching video thumbnails... ")
|
|
|
|
__fetch_thumbnails_obj(Video.objects.filter(icon_default__istartswith='http'), 'video', 'video_id')
|
|
|
|
__fetch_thumbnails_obj(Video.objects.filter(icon_best__istartswith='http'), 'video', 'video_id')
|
|
|
|
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2019-01-02 18:10:37 +00:00
|
|
|
def __compute_progress(stage, stage_count, items, total_items):
|
|
|
|
stage_percent = float(stage) / stage_count
|
|
|
|
|
|
|
|
|
2018-10-13 20:01:45 +00:00
|
|
|
def synchronize():
|
2018-10-20 22:20:31 +00:00
|
|
|
if not __lock.acquire(blocking=False):
|
|
|
|
# Synchronize already running in another thread
|
|
|
|
log.info("Synchronize already running in another thread")
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
log.info("Running scheduled synchronization... ")
|
2019-01-02 18:10:37 +00:00
|
|
|
notification_manager.notify_status_operation_progress(
|
|
|
|
OPERATION_ID_SYNCHRONIZE,
|
|
|
|
'Running scheduled synchronization: checking for new videos...',
|
|
|
|
0.1,
|
|
|
|
None
|
|
|
|
)
|
2018-10-20 22:20:31 +00:00
|
|
|
|
|
|
|
# Sync subscribed playlists/channels
|
|
|
|
log.info("Sync - checking videos")
|
2018-10-29 16:52:09 +00:00
|
|
|
yt_api = youtube.YoutubeAPI.build_public()
|
2018-10-20 22:20:31 +00:00
|
|
|
for subscription in Subscription.objects.all():
|
|
|
|
__check_new_videos_sub(subscription, yt_api)
|
|
|
|
__detect_deleted(subscription)
|
|
|
|
|
2019-01-02 18:10:37 +00:00
|
|
|
notification_manager.notify_status_operation_progress(
|
|
|
|
OPERATION_ID_SYNCHRONIZE,
|
|
|
|
'Running scheduled synchronization: enqueueing videos to download...',
|
|
|
|
0.5,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
|
2018-10-20 22:20:31 +00:00
|
|
|
log.info("Sync - checking for videos to download")
|
|
|
|
downloader_process_all()
|
|
|
|
|
2019-01-02 18:10:37 +00:00
|
|
|
notification_manager.notify_status_operation_progress(
|
|
|
|
OPERATION_ID_SYNCHRONIZE,
|
|
|
|
'Running scheduled synchronization: fetching thumbnails...',
|
|
|
|
0.7,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
|
2018-10-20 22:20:31 +00:00
|
|
|
log.info("Sync - fetching missing thumbnails")
|
|
|
|
__fetch_thumbnails()
|
|
|
|
|
|
|
|
log.info("Synchronization finished.")
|
2019-01-02 18:10:37 +00:00
|
|
|
notification_manager.notify_status_operation_ended(
|
|
|
|
OPERATION_ID_SYNCHRONIZE,
|
|
|
|
'Synchronization finished.',
|
|
|
|
None
|
|
|
|
)
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-20 22:20:31 +00:00
|
|
|
finally:
|
|
|
|
__lock.release()
|
2018-10-10 22:43:50 +00:00
|
|
|
|
|
|
|
|
2018-10-20 22:20:31 +00:00
|
|
|
def synchronize_subscription(subscription: Subscription):
|
|
|
|
__lock.acquire()
|
|
|
|
try:
|
|
|
|
log.info("Running synchronization for single subscription %d [%s]", subscription.id, subscription.name)
|
2018-11-04 21:32:18 +00:00
|
|
|
notification_manager.notify_status_update(f'Synchronization started for subscription <strong>{subscription.name}</strong>.')
|
|
|
|
|
2018-10-29 16:52:09 +00:00
|
|
|
yt_api = youtube.YoutubeAPI.build_public()
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-20 22:20:31 +00:00
|
|
|
log.info("Sync - checking videos")
|
|
|
|
__check_new_videos_sub(subscription, yt_api)
|
|
|
|
__detect_deleted(subscription)
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-20 22:20:31 +00:00
|
|
|
log.info("Sync - checking for videos to download")
|
|
|
|
downloader_process_subscription(subscription)
|
2018-10-10 22:43:50 +00:00
|
|
|
|
2018-10-20 22:20:31 +00:00
|
|
|
log.info("Sync - fetching missing thumbnails")
|
|
|
|
__fetch_thumbnails()
|
|
|
|
|
|
|
|
log.info("Synchronization finished for subscription %d [%s].", subscription.id, subscription.name)
|
2018-11-04 21:32:18 +00:00
|
|
|
notification_manager.notify_status_update(f'Synchronization finished for subscription <strong>{subscription.name}</strong>.')
|
2018-10-20 22:20:31 +00:00
|
|
|
|
|
|
|
finally:
|
|
|
|
__lock.release()
|
|
|
|
|
|
|
|
|
2018-12-31 11:53:25 +00:00
|
|
|
__global_sync_job = None
|
|
|
|
|
|
|
|
|
2018-10-20 22:20:31 +00:00
|
|
|
def schedule_synchronize_global():
|
2018-12-31 11:53:25 +00:00
|
|
|
global __global_sync_job
|
|
|
|
|
2018-12-29 15:11:20 +00:00
|
|
|
trigger = CronTrigger.from_crontab(appconfig.sync_schedule)
|
2018-12-31 11:53:25 +00:00
|
|
|
|
|
|
|
if __global_sync_job is None:
|
|
|
|
trigger = CronTrigger.from_crontab(appconfig.sync_schedule)
|
|
|
|
__global_sync_job = scheduler.add_job(synchronize, trigger, max_instances=1, coalesce=True)
|
|
|
|
|
|
|
|
else:
|
|
|
|
__global_sync_job.reschedule(trigger, max_instances=1, coalesce=True)
|
|
|
|
|
|
|
|
log.info('Scheduled synchronize job job=%s', __global_sync_job.id)
|
2018-10-20 22:20:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def schedule_synchronize_now():
|
2018-12-30 12:45:42 +00:00
|
|
|
job = scheduler.add_job(synchronize, max_instances=1, coalesce=True)
|
2018-10-29 16:52:09 +00:00
|
|
|
log.info('Scheduled synchronize now job job=%s', job.id)
|
2018-10-20 22:20:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def schedule_synchronize_now_subscription(subscription: Subscription):
|
2018-12-30 12:45:42 +00:00
|
|
|
job = scheduler.add_job(synchronize_subscription, args=[subscription])
|
2018-10-29 16:52:09 +00:00
|
|
|
log.info('Scheduled synchronize subscription job subscription=(%s), job=%s', subscription, job.id)
|