add post api, return posters too

This commit is contained in:
j 2012-07-08 10:36:13 +02:00
parent 5e253ba9ac
commit 9221b3ea92
3 changed files with 52 additions and 4 deletions

View file

@ -142,7 +142,8 @@ class MovieId(models.Model):
'url': 'http://google.com/search?q=%s' % quote(qs.encode('utf-8'))}) 'url': 'http://google.com/search?q=%s' % quote(qs.encode('utf-8'))})
return links return links
def json(self): def json(self, prefix):
from poster.models import getPosters
json = {} json = {}
json['imdb.com'] = {} json['imdb.com'] = {}
for key in [ for key in [
@ -197,5 +198,10 @@ class MovieId(models.Model):
json['0xdb.org']['suggested_name'] = self.suggested_name() json['0xdb.org']['suggested_name'] = self.suggested_name()
json['0xdb.org']['suggested_path'] = self.suggested_path() json['0xdb.org']['suggested_path'] = self.suggested_path()
posters = getPosters(self, prefix)
for key in posters:
if not key in json:
json[key] = {}
json[key]['posters'] = posters[key]
return json return json

View file

@ -5,7 +5,9 @@ from django.db import models
from django.db.models import Q from django.db.models import Q
from django.contrib.auth.models import User from django.contrib.auth.models import User
from ox.django.shortcuts import render_to_json_response from ox.django.shortcuts import render_to_json_response, json_response
from ox.utils import json
from api.actions import actions
import models import models
@ -41,6 +43,7 @@ def get_movie_id(request):
return movie_id return movie_id
def ids(request): def ids(request):
prefix = request.build_absolute_uri('/')
json = {} json = {}
if 'title' in request.GET: if 'title' in request.GET:
title = request.GET['title'] title = request.GET['title']
@ -52,13 +55,51 @@ def ids(request):
if year: if year:
movies = movies.filter(year=year) movies = movies.filter(year=year)
if movies.count() == 1: if movies.count() == 1:
json = movies[0].json() json = movies[0].json(prefix)
else: else:
movie = get_movie_id(request) movie = get_movie_id(request)
if movie: if movie:
json = movie.json() json = movie.json(prefix)
return render_to_json_response(json) return render_to_json_response(json)
def get(request):
prefix = request.build_absolute_uri('/')
response = json_response()
data = json.loads(request.POST['data'])
movie_id = None
movieId = None
if 'id' in data:
movieId = data['id']
elif 'itemId' in data:
movieId = data['itemId']
if movieId:
if len(movieId) == 7:
movie_id = models.getMovieIdByImdbId(imdb_id=movieId)
else:
try:
movie_id = models.MovieId.objects.get(oxdb_id=movieId)
except models.MovieId.DoesNotExist:
movie_id = None
if 'imdb' in data:
movieId = data['imdb']
movie_id = models.getMovieIdByImdbId(imdb_id=movieId)
elif 'oxdb' in data:
oxdbId = data['oxdb']
movie_id = models.MovieId.objects.get(oxdb_id=oxdbId)
elif 'criterion' in data:
criterion_id = data['criterion']
movie_id = models.MovieId.objects.get(criterion_id=criterion_id)
elif 'wikipedia' in data:
wikipedia_id = data['wikipedia']
movie_id = models.MovieId.objects.get(wikipedia_id=wikipedia_id)
if movie_id:
movie_id.updateFromImdb()
response['data'] = movie_id.json(prefix)
else:
response['status'] = {'text':'not found', 'code': 404}
return render_to_json_response(response)
actions.register(get)
def urls(request): def urls(request):
json = {} json = {}
return render_to_json_response(json) return render_to_json_response(json)

View file

@ -38,6 +38,7 @@ def getId(request):
return render_to_json_response(response) return render_to_json_response(response)
actions.register(getId) actions.register(getId)
def getData(request): def getData(request):
response = json_response() response = json_response()
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])