pandora_indiancinema/tasks.py
2016-04-01 19:36:00 +02:00

69 lines
2.3 KiB
Python

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from datetime import datetime
from collections import Counter
import ox
from celery.task import periodic_task
from celery.schedules import crontab
from django.db import transaction
import itemlist.models
import item.models
import annotation.models
import text.models
@periodic_task(run_every=crontab(hour=6, minute=0), queue='encoding')
def cron(**kwargs):
update_double_imdb()
outofcopyright()
update_queued()
update_keywords()
def outofcopyright():
for i in item.models.Item.objects.filter(sort__year__lt=datetime.now().year - 60, level=2):
i.level = 1
i.save()
def update_double_imdb():
l = itemlist.models.List.get('j:Double IMDb')
for i in item.models.Item.objects.filter(data__contains='imdbId').exclude(id__in=l.items.all()):
if item.models.Item.objects.exclude(id=i.id).filter(sort__imdbId=i.sort.imdbId).exists():
l.add(i)
for i in l.items.all():
if not i.get('imdbId') or \
item.models.Item.objects.filter(sort__imdbId=i.sort.imdbId).count() == 1:
l.remove(i)
transaction.commit_unless_managed()
def update_keywords():
t = text.models.Text.get('j:Keywords')
keywords = Counter([a['value']
for a in annotation.models.Annotation.objects.filter(layer='keywords').all().values('value')
])
data = ['<ul>']
def s(k):
return ox.sort_string('%s %s' % (10000-keywords[k], k)).lower()
for k in sorted(keywords.keys(), key=s):
data.append('<li>%d <a href="/clip/keywords==%s">%s</a>' %((keywords[k], k.replace(' ', '_'), k)))
data.append('</ul>')
t.text = '\n'.join(data)
t.save()
def update_queued():
import archive.models
l = itemlist.models.List.get('j:Queued')
for f in archive.models.File.objects.filter(queued=True).exclude(id__in=l.items.all()):
l.add(f.item)
for i in l.items.all():
if not i.files.filter(queued=True).exists():
l.remove(i)
l = itemlist.models.List.get('j:Encoding')
for f in archive.models.File.objects.filter(encoding=True).exclude(id__in=l.items.all()):
l.add(f.item)
for i in l.items.all():
if not i.files.filter(encoding=True).exists():
l.remove(i)
transaction.commit_unless_managed()