2010-06-26 14:32:08 +00:00
|
|
|
# -*- 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
|
2012-03-22 20:19:19 +00:00
|
|
|
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
|
2010-06-26 14:32:08 +00:00
|
|
|
|
|
|
|
import models
|
|
|
|
|
|
|
|
|
2012-06-07 08:59:13 +00:00
|
|
|
@periodic_task(run_every=timedelta(days=1), queue='encoding')
|
2010-06-26 14:32:08 +00:00
|
|
|
def cronjob(**kwargs):
|
2012-02-15 18:02:06 +00:00
|
|
|
update_random_sort()
|
2012-03-22 20:19:19 +00:00
|
|
|
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
|
|
|
|
2012-03-08 18:56:31 +00:00
|
|
|
def update_random_clip_sort():
|
|
|
|
if filter(lambda f: f['id'] == 'random', settings.CONFIG['itemKeys']):
|
2012-03-22 20:19:19 +00:00
|
|
|
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-08 18:56:31 +00:00
|
|
|
|
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')
|
2010-11-27 12:12:53 +00:00
|
|
|
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()
|
2012-03-10 01:46:26 +00:00
|
|
|
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,
|
2012-03-10 01:46:26 +00:00
|
|
|
icon=item.icon.name
|
|
|
|
)
|
2011-01-01 11:44:42 +00:00
|
|
|
|
2012-09-11 13:17:35 +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):
|
2010-12-25 13:45:19 +00:00
|
|
|
item = models.Item.objects.get(itemId=itemId)
|
2011-01-16 13:28:57 +00:00
|
|
|
item.update_external()
|
2010-06-26 14:32:08 +00:00
|
|
|
|
2013-03-12 09:36:00 +00:00
|
|
|
@task(queue="encoding")
|
2011-08-19 16:54:42 +00:00
|
|
|
def update_timeline(itemId):
|
2010-10-16 11:49:45 +00:00
|
|
|
item = models.Item.objects.get(itemId=itemId)
|
2013-03-12 09:36:00 +00:00
|
|
|
item.update_timeline(async=False)
|
2011-06-01 16:40:39 +00:00
|
|
|
|
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")
|
2011-06-01 16:40:39 +00:00
|
|
|
def load_subtitles(itemId):
|
|
|
|
item = models.Item.objects.get(itemId=itemId)
|
2012-09-08 12:41:19 +00:00
|
|
|
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'
|
2013-03-07 10:24:25 +00:00
|
|
|
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
|
|
|
|
2012-01-11 07:51:26 +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))
|