96 lines
2.9 KiB
Python
96 lines
2.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
from __future__ import division
|
|
|
|
import re
|
|
from urllib import quote
|
|
|
|
from django.conf import settings
|
|
from ox.django.shortcuts import render_to_json_response, json_response
|
|
import ox.web.imdb
|
|
from ox.utils import json
|
|
|
|
from api.actions import actions
|
|
from poster.models import getPosters
|
|
from lookup.models import getMovieIdByImdbId
|
|
import models
|
|
|
|
|
|
def posters(request, imdbId):
|
|
movie_id = getMovieIdByImdbId(imdb_id=imdbId)
|
|
return getPosters(movie_id, request.build_absolute_uri('/'))
|
|
|
|
def links(request, imdbId):
|
|
movie_id = getMovieIdByImdbId(imdb_id=imdbId)
|
|
links = []
|
|
if movie_id:
|
|
links = movie_id.links()
|
|
return links
|
|
|
|
def getId(request):
|
|
data = json.loads(request.POST['data'])
|
|
response = json_response()
|
|
movie = models.find(data)
|
|
if movie:
|
|
response['data'] = movie.json()
|
|
else:
|
|
response['status'] = {'text':'not found', 'code': 404}
|
|
return render_to_json_response(response)
|
|
actions.register(getId)
|
|
|
|
def getData(request):
|
|
response = json_response()
|
|
data = json.loads(request.POST['data'])
|
|
id = data['id']
|
|
if len(id) == 7:
|
|
data = ox.web.imdb.Imdb(id)
|
|
i, created = models.Imdb.objects.get_or_create(imdb=id)
|
|
if created:
|
|
i.update()
|
|
|
|
def fix_links(t):
|
|
def fix_names(m):
|
|
return '<a href="/name=%s">%s</a>' % (
|
|
quote(m.group(2).encode('utf-8')), m.group(2)
|
|
)
|
|
t = re.sub('<a href="(/name/.*?/)">(.*?)</a>', fix_names, t)
|
|
def fix_titles(m):
|
|
return '<a href="/title=%s">%s</a>' % (
|
|
quote(m.group(2).encode('utf-8')), m.group(2)
|
|
)
|
|
t = re.sub('<a href="(/title/.*?/)">(.*?)</a>', fix_titles, t)
|
|
return t
|
|
if 'trivia' in data:
|
|
data['trivia'] = [fix_links(t) for t in data['trivia']]
|
|
|
|
if 'summary' in data:
|
|
data['summary'] = fix_links(data['summary'])
|
|
|
|
if 'reviews' in data:
|
|
reviews = []
|
|
for r in data['reviews']:
|
|
for url in settings.REVIEW_WHITELIST:
|
|
if url in r[0]:
|
|
reviews.append({
|
|
'source': settings.REVIEW_WHITELIST[url],
|
|
'url': r[0]
|
|
})
|
|
data['reviews'] = reviews
|
|
if not data['reviews']:
|
|
del data['reviews']
|
|
|
|
data['posters'] = posters(request, id)
|
|
data['links'] = links(request, id)
|
|
|
|
response['data'] = data
|
|
else:
|
|
response['status'] = {'text':'not found', 'code': 404}
|
|
|
|
return render_to_json_response(response)
|
|
actions.register(getData)
|
|
|
|
def parsePath(request):
|
|
path = json.loads(request.POST['data'])['path']
|
|
response = json_response(ox.parse_movie_path(path))
|
|
return render_to_json_response(response)
|
|
actions.register(parsePath)
|