findFiles

This commit is contained in:
j 2011-01-19 17:36:03 +05:30
parent 4c20349896
commit c493af50e7
2 changed files with 203 additions and 50 deletions

View file

@ -214,6 +214,26 @@ class File(models.Model):
return True return True
return False return False
def json(self, keys=None, user=None):
data = {
'available': self.available,
'duration': self.duration,
'framerate': self.framerate,
'height': self.height,
'width': self.width,
'oshash': self.oshash,
'samplerate': self.samplerate,
'video_codec': self.video_codec,
'audio_codec': self.audio_codec,
'name': self.name,
'size': self.size,
'info': self.info
}
if keys:
for k in data.keys():
if k not in keys:
del data[k]
return data
class Volume(models.Model): class Volume(models.Model):

View file

@ -7,6 +7,7 @@ from datetime import datetime
from django import forms from django import forms
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.conf import settings from django.conf import settings
from django.db.models import Count, Sum
from ox.utils import json from ox.utils import json
from ox.django.decorators import login_required_json from ox.django.decorators import login_required_json
@ -15,6 +16,7 @@ from ox.django.views import task_status
from item.utils import parse_path from item.utils import parse_path
from item.models import get_item from item.models import get_item
from item.views import _parse_query
import item.tasks import item.tasks
from api.actions import actions from api.actions import actions
@ -258,56 +260,187 @@ def lookup_file(request, oshash):
f = get_object_or_404(models.File, oshash=oshash) f = get_object_or_404(models.File, oshash=oshash)
return redirect(f.item.get_absolute_url()) return redirect(f.item.get_absolute_url())
"""
def fileInfo(request):
'''
param data
oshash string
return {'status': {'code': int, 'text': string},
'data': {imdbId:string }}
'''
if 'data' in request.POST:
oshash = json.loads(request.POST['data'])
elif 'oshash' in request.GET:
oshash = request.GET['oshash']
f = models.ItemFile.objects.get(oshash=oshash)
response = {'data': f.json()}
return render_to_json_response(response)
actions.register(fileInfo)
def subtitles(request): def _order_query(qs, sort, prefix=''):
order_by = []
if len(sort) == 1:
sort.append({'operator': '+', 'key': 'name'})
sort.append({'operator': '-', 'key': 'created'})
''' '''
param data if sort[0]['key'] == 'title':
oshash string sort.append({'operator': '-', 'key': 'year'})
language string sort.append({'operator': '+', 'key': 'director'})
subtitle string elif sort[0]['key'] == 'director':
return sort.append({'operator': '-', 'key': 'year'})
if no language is provided: sort.append({'operator': '+', 'key': 'title'})
{data: {languages: array}} elif sort[0]['key'] == 'year':
if language is set: sort.append({'operator': '+', 'key': 'director'})
{data: {subtitle: string}} sort.append({'operator': '+', 'key': 'title'})
if subtitle is set: elif not sort[0]['key'] in ('value', 'value_sort'):
saves subtitle for given language sort.append({'operator': '+', 'key': 'director'})
sort.append({'operator': '-', 'key': 'year'})
sort.append({'operator': '+', 'key': 'title'})
'''
for e in sort:
operator = e['operator']
if operator != '-':
operator = ''
key = {'id': 'item__itemId', 'name': 'sort_name'}.get(e['key'], e['key'])
#if operator=='-' and '%s_desc'%key in models.ItemSort.descending_fields:
# key = '%s_desc' % key
order = '%s%s%s' % (operator, prefix, key)
order_by.append(order)
if order_by:
qs = qs.order_by(*order_by)
return qs
def findFiles(request):
'''
param data {
'query': query,
'sort': array,
'range': array
}
query: query object, more on query syntax at
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
sort: array of key, operator dics
[
{
key: "year",
operator: "-"
},
{
key: "director",
operator: ""
}
]
range: result range, array [from, to]
keys: array of keys to return
group: group elements by, country, genre, director...
with keys, items is list of dicts with requested properties:
return {'status': {'code': int, 'text': string},
'data': {items: array}}
Groups
param data {
'query': query,
'key': string,
'group': string,
'range': array
}
query: query object, more on query syntax at
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
range: result range, array [from, to]
keys: array of keys to return
group: group elements by, country, genre, director...
possible values for keys: name, items
with keys
items contains list of {'name': string, 'items': int}:
return {'status': {'code': int, 'text': string},
'data': {items: array}}
without keys: return number of items in given query
return {'status': {'code': int, 'text': string},
'data': {items: int}}
Positions
param data {
'query': query,
'ids': []
}
query: query object, more on query syntax at
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
ids: ids of items for which positions are required
return {
status: {...},
data: {
positions: {
id: position
}
}
}
''' '''
if 'data' in request.POST:
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
oshash = data['oshash'] if settings.JSON_DEBUG:
language = data.get('language', None) print json.dumps(data, indent=2)
srt = data.get('subtitle', None) query = _parse_query(data, request.user)
if srt:
user = request.user
sub = models.Subtitles.objects.get_or_create(user, oshash, language)
sub.srt = srt
sub.save()
else:
response = json_response({}) response = json_response({})
if language: if 'group' in query:
q = models.Subtitles.objects.filter(item_file__oshash=oshash, language=language) if 'sort' in query:
if q.count() > 0: if len(query['sort']) == 1 and query['sort'][0]['key'] == 'items':
response['data']['subtitle'] = q[0].srt if query['group'] == "year":
order_by = query['sort'][0]['operator'] == '-' and 'items' or '-items'
else:
order_by = query['sort'][0]['operator'] == '-' and '-items' or 'items'
if query['group'] != "keyword":
order_by = (order_by, 'value_sort')
else:
order_by = (order_by,)
else:
order_by = query['sort'][0]['operator'] == '-' and '-value_sort' or 'value_sort'
order_by = (order_by, 'items')
else:
order_by = ('-value_sort', 'items')
response['data']['items'] = []
items = 'items'
item_qs = query['qs']
qs = models.Facet.objects.filter(key=query['group']).filter(item__id__in=item_qs)
qs = qs.values('value').annotate(items=Count('id')).order_by(*order_by)
if 'ids' in query:
#FIXME: this does not scale for larger results
response['data']['positions'] = {}
ids = [j['value'] for j in qs]
response['data']['positions'] = utils.get_positions(ids, query['ids'])
elif 'range' in data:
qs = qs[query['range'][0]:query['range'][1]]
response['data']['items'] = [{'name': i['value'], 'items': i[items]} for i in qs]
else:
response['data']['items'] = qs.count()
elif 'ids' in query:
#FIXME: this does not scale for larger results
qs = _order_query(query['qs'], query['sort'])
response['data']['positions'] = {}
ids = [j['itemId'] for j in qs.values('itemId')]
response['data']['positions'] = utils.get_positions(ids, query['ids'])
elif 'keys' in query:
response['data']['items'] = []
qs = models.File.objects.filter(item__in=query['qs'])
qs = _order_query(qs, query['sort'])
keys = query['keys']
qs = qs[query['range'][0]:query['range'][1]]
response['data']['items'] = [f.json(keys) for f in qs]
else: # otherwise stats
items = query['qs']
files = models.File.objects.filter(item__in=query['qs'])
#files = models.File.objects.all().filter(item__in=items).exclude(size__gt=0)
r = files.aggregate(
Sum('duration'),
Sum('pixels'),
Sum('size')
)
response['data']['duration'] = r['duration__sum']
response['data']['files'] = files.count()
response['data']['items'] = items.count()
response['data']['pixels'] = r['pixels__sum']
response['data']['runtime'] = items.filter(sort__runtime_desc__gt=0).aggregate(Sum('sort__runtime_desc'))['sort__runtime_desc__sum']
if response['data']['runtime'] == None:
response['data']['runtime'] = 0
response['data']['size'] = r['size__sum']
return render_to_json_response(response) return render_to_json_response(response)
l = models.Subtitles.objects.filter(item_file__oshash=oshash).values('language')
response['data']['languages'] = [f['language'] for f in l] actions.register(findFiles)
return render_to_json_response(response)
actions.register(subtitles)
"""