pandora/pandora/item/tasks.py

168 lines
6.4 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2012-01-11 07:42:32 +00:00
import os
from datetime import timedelta, datetime
import gzip
2012-02-15 18:02:06 +00:00
import random
random
2012-01-11 07:42:32 +00:00
from django.conf import settings
from django.db import connection, transaction
2012-01-11 07:42:32 +00:00
from ox.utils import ET
2011-12-27 13:21:22 +00:00
from celery.task import task, periodic_task
import models
2012-06-07 08:59:13 +00:00
@periodic_task(run_every=timedelta(days=1), queue='encoding')
def cronjob(**kwargs):
2012-02-15 18:02:06 +00:00
update_random_sort()
update_random_clip_sort()
2012-02-15 18:02:06 +00:00
def update_random_sort():
if filter(lambda f: f['id'] == 'random', settings.CONFIG['itemKeys']):
random.seed()
ids = [f['item'] for f in models.ItemSort.objects.values('item')]
random.shuffle(ids)
2012-02-22 12:28:42 +00:00
n = 1
2012-02-15 18:02:06 +00:00
for i in ids:
models.ItemSort.objects.filter(pk=i).update(random=n)
n += 1
2011-01-01 11:44:42 +00:00
def update_random_clip_sort():
if filter(lambda f: f['id'] == 'random', settings.CONFIG['itemKeys']):
with transaction.commit_on_success():
cursor = connection.cursor()
cursor.execute('DROP TABLE clip_random;')
cursor.execute('CREATE TABLE "clip_random" AS SELECT id AS clip_id, row_number() OVER (ORDER BY random()) AS random FROM "clip_clip"')
cursor.execute('ALTER TABLE "clip_random" ADD UNIQUE ("clip_id")')
cursor.execute('CREATE INDEX "clip_random_clip_id_idx" ON "clip_random" ("clip_id")')
cursor.execute('CREATE INDEX "clip_random_random_idx" ON "clip_random" ("random")')
2012-03-22 21:33:42 +00:00
@task(ignore_results=True, queue='default')
def update_clips(itemId):
item = models.Item.objects.get(itemId=itemId)
item.clips.all().update(user=item.user.id)
2012-03-21 08:13:38 +00:00
@task(ignore_results=True, queue='default')
def update_poster(itemId):
2010-09-23 16:01:48 +00:00
item = models.Item.objects.get(itemId=itemId)
2010-12-01 00:00:33 +00:00
item.make_poster(True)
2011-01-21 10:49:24 +00:00
item.make_icon()
models.Item.objects.filter(pk=item.id).update(
poster=item.poster.name,
2012-07-01 12:17:04 +00:00
poster_height=item.poster.height,
poster_width=item.poster.width,
icon=item.icon.name
)
2011-01-01 11:44:42 +00:00
@task(ignore_results=True, queue='default')
def update_file_paths(itemId):
item = models.Item.objects.get(itemId=itemId)
for f in item.files.all():
if f.normalize_path() != f.path:
f.save()
2012-03-21 08:13:38 +00:00
@task(ignore_results=True, queue='default')
2011-01-16 13:28:57 +00:00
def update_external(itemId):
item = models.Item.objects.get(itemId=itemId)
2011-01-16 13:28:57 +00:00
item.update_external()
@task(queue="encoding")
2011-08-19 16:54:42 +00:00
def update_timeline(itemId):
item = models.Item.objects.get(itemId=itemId)
item.update_timeline(async=False)
2012-06-06 14:38:18 +00:00
@task(queue="encoding")
def rebuild_timeline(itemId):
i = models.Item.objects.get(itemId=itemId)
for s in i.streams():
s.make_timeline()
i.update_timeline()
2012-03-23 16:37:32 +00:00
@task(queue="encoding")
def load_subtitles(itemId):
item = models.Item.objects.get(itemId=itemId)
if item.load_subtitles():
item.update_find()
item.update_sort()
item.update_facets()
2011-06-02 08:19:15 +00:00
2012-03-21 08:13:38 +00:00
@task(ignore_results=True, queue='default')
2012-01-11 07:42:32 +00:00
def update_sitemap(base_url):
sitemap = os.path.abspath(os.path.join(settings.MEDIA_ROOT, 'sitemap.xml.gz'))
def absolute_url(url):
return base_url + url
urlset = ET.Element('urlset')
urlset.attrib['xmlns'] = "http://www.sitemaps.org/schemas/sitemap/0.9"
urlset.attrib['xmlns:xsi'] = "http://www.w3.org/2001/XMLSchema-instance"
urlset.attrib['xsi:schemaLocation'] = "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
urlset.attrib['xmlns:video']= "http://www.google.com/schemas/sitemap-video/1.0"
url = ET.SubElement(urlset, "url")
loc = ET.SubElement(url, "loc")
loc.text = absolute_url('')
# always, hourly, daily, weekly, monthly, yearly, never
changefreq = ET.SubElement(url, "changefreq")
changefreq.text = 'daily'
# This date should be in W3C Datetime format, can be %Y-%m-%d
lastmod = ET.SubElement(url, "lastmod")
lastmod.text = datetime.now().strftime("%Y-%m-%d")
# priority of page on site values 0.1 - 1.0
priority = ET.SubElement(url, "priority")
priority.text = '1.0'
for page in [s['id'] for s in settings.CONFIG['sitePages']]:
url = ET.SubElement(urlset, "url")
loc = ET.SubElement(url, "loc")
loc.text = absolute_url(page)
# always, hourly, daily, weekly, monthly, yearly, never
changefreq = ET.SubElement(url, "changefreq")
changefreq.text = 'monthly'
# priority of page on site values 0.1 - 1.0
priority = ET.SubElement(url, "priority")
priority.text = '1.0'
allowed_level = settings.CONFIG['capabilities']['canSeeItem']['guest']
for i in models.Item.objects.filter(level__lte=allowed_level):
url = ET.SubElement(urlset, "url")
# URL of the page. This URL must begin with the protocol (such as http)
loc = ET.SubElement(url, "loc")
2013-03-09 09:35:04 +00:00
loc.text = absolute_url("%s/info" % i.itemId)
2012-01-11 07:42:32 +00:00
# This date should be in W3C Datetime format, can be %Y-%m-%d
lastmod = ET.SubElement(url, "lastmod")
lastmod.text = i.modified.strftime("%Y-%m-%d")
# always, hourly, daily, weekly, monthly, yearly, never
changefreq = ET.SubElement(url, "changefreq")
changefreq.text = 'monthly'
# priority of page on site values 0.1 - 1.0
priority = ET.SubElement(url, "priority")
priority.text = '1.0'
video = ET.SubElement(url, "video:video")
#el = ET.SubElement(video, "video:content_loc")
#el.text = absolute_url("%s/video" % i.itemId)
el = ET.SubElement(video, "video:player_loc")
el.attrib['allow_embed'] = 'no'
el.text = absolute_url("%s/player" % i.itemId)
2012-01-11 07:42:32 +00:00
el = ET.SubElement(video, "video:title")
el.text = i.get('title')
el = ET.SubElement(video, "video:thumbnail_loc")
2013-03-08 05:36:26 +00:00
el.text = absolute_url("%s/96p.jpg" % i.itemId)
2013-03-09 09:35:04 +00:00
description = i.get_item_description()
2012-01-11 07:42:32 +00:00
if description:
el = ET.SubElement(video, "video:description")
el.text = i.get('description', i.get('summary', ''))
el = ET.SubElement(video, "video:family_friendly")
el.text = 'Yes'
2012-01-11 12:22:59 +00:00
duration = i.sort.duration
if duration > 0:
2012-01-11 07:42:32 +00:00
el = ET.SubElement(video, "video:duration")
2012-01-11 12:22:59 +00:00
el.text = "%s" % int(duration)
2012-01-11 07:42:32 +00:00
with open(sitemap[:-3], 'wb') as f:
f.write('<?xml version="1.0" encoding="UTF-8"?>\n' + ET.tostring(urlset))
2012-01-11 07:42:32 +00:00
with gzip.open(sitemap, 'wb') as f:
f.write('<?xml version="1.0" encoding="UTF-8"?>\n' + ET.tostring(urlset))