From acfd3e1fbc54d2a0370adc4483d8d3efd42ae3a6 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 25 Sep 2012 13:54:13 +0200 Subject: [PATCH] add patch dict to overwrite imdb data --- oxdata/movie/management/__init__.py | 0 oxdata/movie/management/commands/__init__.py | 0 .../management/commands/load_imdb_patches.py | 27 +++++++++++++++++++ oxdata/movie/models.py | 16 +++++++++++ 4 files changed, 43 insertions(+) create mode 100644 oxdata/movie/management/__init__.py create mode 100644 oxdata/movie/management/commands/__init__.py create mode 100644 oxdata/movie/management/commands/load_imdb_patches.py diff --git a/oxdata/movie/management/__init__.py b/oxdata/movie/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/oxdata/movie/management/commands/__init__.py b/oxdata/movie/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/oxdata/movie/management/commands/load_imdb_patches.py b/oxdata/movie/management/commands/load_imdb_patches.py new file mode 100644 index 0000000..970f0f4 --- /dev/null +++ b/oxdata/movie/management/commands/load_imdb_patches.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# vi:si:et:sw=4:sts=4:ts=4 +import json +from optparse import make_option + +from django.core.management.base import BaseCommand, CommandError +from django.conf import settings + +from ...models import Imdb + +class Command(BaseCommand): + """ + load new ids into cache + """ + help = 'load ids from sites that dont support search.' + args = '' + + def handle(self, *args, **options): + for arg in args: + with open(arg) as f: + data = json.load(f) + for id in data: + i, created = Imdb.objects.get_or_create(imdb=id) + i.patch = data[id] + i.update() + i.save() + print i diff --git a/oxdata/movie/models.py b/oxdata/movie/models.py index 175331d..714956b 100644 --- a/oxdata/movie/models.py +++ b/oxdata/movie/models.py @@ -9,6 +9,7 @@ from urllib import quote from django.db import models from django.conf import settings import ox +from ox.django.fields import DictField from lookup.models import get_movie_id from poster.models import getPosters @@ -72,6 +73,7 @@ class Imdb(models.Model): seriesTitle = models.CharField(max_length=1000, blank=True, default='') invalid = models.BooleanField(default=False) + patch = DictField(default=None, blank=True) def __unicode__(self): return u"[%s] %s%s" % (self.imdb, self.title, self.year and ' (%s)' % self.year or '') @@ -79,8 +81,21 @@ class Imdb(models.Model): keys = ('title', 'director', 'year', 'season', 'episode', 'seriesTitle', 'episodeTitle', 'episodeYear', 'episodeDirector') + def apply_patch(self, data): + if self.patch: + data.update(self.patch) + if 'seriesTitle' in data and 'episodeTitle' in data: + if 'season' in data and 'episode' in data: + data['title'] = "%s (S%02dE%02d) %s" % ( + data['seriesTitle'], data['season'], data['episode'], data['episodeTitle']) + else: + data['title'] = "%s (S01) %s" % (data['seriesTitle'], data['episodeTitle']) + data['title'] = data['title'].strip() + return data + def update(self): info = ox.web.imdb.ImdbCombined(self.imdb) + info = self.apply_patch(info) if info: for key in self.keys: if key in info: @@ -101,6 +116,7 @@ class Imdb(models.Model): def data(self, request=None, timeout=ox.cache.cache_timeout): data = ox.web.imdb.Imdb(self.imdb, timeout=timeout) + data = self.apply_patch(data) def fix_links(t): def fix_names(m): return '%s' % (