From 6abbaa57cc58fbf6cf645d931b05e40b02d4ca0c Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 26 Jul 2011 19:22:23 +0200 Subject: [PATCH] local poster --- pandora/item/models.py | 117 +++++++++++++++++------------------------ pandora/item/urls.py | 2 + pandora/item/views.py | 21 ++++++++ 3 files changed, 72 insertions(+), 68 deletions(-) diff --git a/pandora/item/models.py b/pandora/item/models.py index 4b1c572ce..72b4ab849 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -308,20 +308,17 @@ class Item(models.Model): return poster def get_posters(self): - posters = {} + posters = { + 'local': [{ + 'url': '/%s/poster.pandora.jpg' % self.itemId, + 'width': 640, + 'height': 1024, + }] + } for p in self.poster_urls.all(): if p.service not in posters: posters[p.service] = [] posters[p.service].append({'url': p.url, 'width': p.width, 'height': p.height}) - local_posters = self.local_posters().keys() - if local_posters: - posters['local'] = [] - for p in local_posters: - #FIXME: media_url is no longer public - url = p.replace(settings.MEDIA_ROOT, settings.MEDIA_URL) - width = 640 - height = 1024 - posters['local'].append({'url': url, 'width': width, 'height': height}) return posters def get_stream(self): @@ -374,6 +371,8 @@ class Item(models.Model): i['poster'] = self.get_poster() i['posters'] = self.get_posters() + i['posterFrames'] = ['/%s/frame/poster/%d.jpg' %(self.itemId, p) + for p in range(0, len(self.poster_frames()))] return i @@ -730,7 +729,7 @@ class Item(models.Model): #extract.timeline_strip(self, self.data['cuts'], stream.info, self.timeline_prefix[:-8]) stream.extract_derivatives() - self.make_local_posters() + self.make_local_poster() self.make_poster() self.make_icon() self.rendered = files != {} @@ -790,74 +789,56 @@ class Item(models.Model): else: if force: self.delete_poster() - local_posters = self.make_local_posters() - if local_posters: - with open(local_posters[0]) as f: - self.poster.save('poster.jpg', ContentFile(f.read())) + poster = self.make_local_poster() + with open(poster) as f: + self.poster.save('poster.jpg', ContentFile(f.read())) - def local_posters(self): - part = 1 - posters = {} + def make_local_poster(self): + poster = self.path('poster.local.jpg') + poster = os.path.abspath(os.path.join(settings.MEDIA_ROOT, poster)) + + frame = self.get_poster_frame_path() timeline = self.path('timeline.64.png') timeline = os.path.abspath(os.path.join(settings.MEDIA_ROOT, timeline)) - if not os.path.exists(timeline): - path = self.path('poster.pandora.jpg') - path = os.path.abspath(os.path.join(settings.MEDIA_ROOT, path)) - posters[path] = False - return posters - if self.poster_frame >= 0: - frame = self.get_poster_frame_path() - path = self.path('poster.pandora.%s.%s.jpg'%(part, self.poster_frame)) - path = os.path.abspath(os.path.join(settings.MEDIA_ROOT, path)) - posters[path] = frame - else: - for f in self.main_videos(): - for frame in f.frames.all(): - path = self.path('poster.pandora.%s.%s.jpg'%(part, frame.position)) - path = os.path.abspath(os.path.join(settings.MEDIA_ROOT, path)) - posters[path] = frame.frame.path - part += 1 - return posters - def make_local_posters(self): - posters = self.local_posters() - timeline = self.path('timeline.64.png') - timeline = os.path.abspath(os.path.join(settings.MEDIA_ROOT, timeline)) - ox.makedirs(os.path.join(settings.MEDIA_ROOT,self.path())) - for poster in posters: - frame = posters[poster] - cmd = [settings.ITEM_POSTER, - '-t', self.get('title').encode('utf-8'), - '-d', u', '.join(self.get('director', ['Unknown Director'])).encode('utf-8'), - '-y', str(self.get('year', '')), - '-p', poster - ] - if frame: - cmd += [ - '-f', frame, - '-l', timeline, - ] - if settings.USE_IMDB: - if len(self.itemId) == 7: - cmd += ['-i', self.itemId] - cmd += ['-o', self.oxdbId] - else: + director = u', '.join(self.get('director', ['Unknown Director'])) + cmd = [settings.ITEM_POSTER, + '-t', self.get('title').encode('utf-8'), + '-d', director.encode('utf-8'), + '-y', str(self.get('year', '')), + '-p', poster + ] + if frame: + cmd += [ + '-f', frame, + '-l', timeline, + ] + if settings.USE_IMDB: + if len(self.itemId) == 7: cmd += ['-i', self.itemId] - p = subprocess.Popen(cmd) - p.wait() - return posters.keys() + cmd += ['-o', self.oxdbId] + else: + cmd += ['-i', self.itemId] + ox.makedirs(os.path.join(settings.MEDIA_ROOT,self.path())) + p = subprocess.Popen(cmd) + p.wait() + return poster + + def poster_frames(self): + frames = [] + for f in self.main_videos(): + for ff in f.frames.all(): + frames.append(ff.frame.path) + return frames def get_poster_frame_path(self): if self.poster_frame >= 0: size = int(settings.VIDEO_PROFILE.split('.')[0][:-1]) return self.frame(self.poster_frame, size) - frames = [] - for f in self.main_videos(): - for ff in f.frames.all(): - frames.append(ff.frame.path) - if frames: - return frames[int(len(frames)/2)] + frames = self.poster_frames() + if frames: + return frames[int(len(frames)/2)] def make_icon(self): frame = self.get_poster_frame_path() diff --git a/pandora/item/urls.py b/pandora/item/urls.py index d2c90f85a..7f25e5a3b 100644 --- a/pandora/item/urls.py +++ b/pandora/item/urls.py @@ -6,11 +6,13 @@ from django.conf.urls.defaults import * urlpatterns = patterns("item.views", (r'^(?P[A-Z0-9].*)/frame/(?P\d+)/(?P[0-9\.,]+).jpg$', 'frame'), + (r'^(?P[A-Z0-9].*)/frame/poster/(?P\d+).jpg$', 'poster_frame'), (r'^(?P[A-Z0-9].*)/(?P[a-f0-9]+)/(?P.*\.(?Pwebm|ogv|mp4))$', 'video'), (r'^(?P[A-Z0-9][A-Za-z0-9]+)/torrent/(?P.*?)$', 'torrent'), (r'^(?P[A-Z0-9].*)/(?P.*\.(?Pwebm|ogv|mp4))$', 'video'), (r'^(?P[A-Z0-9].*)/poster\.(?P\d+)\.jpg$', 'poster'), (r'^(?P[A-Z0-9].*)/poster\.(?Plarge)\.jpg$', 'poster'), + (r'^(?P[A-Z0-9].*)/poster\.pandora\.jpg$', 'poster_local'), (r'^(?P[A-Z0-9].*)/poster\.jpg$', 'poster'), (r'^(?P[A-Z0-9].*)/icon\.(?P\d+)\.jpg$', 'icon'), (r'^(?P[A-Z0-9].*)/icon\.jpg$', 'icon'), diff --git a/pandora/item/views.py b/pandora/item/views.py index 34cadbb8d..441dc4ecb 100644 --- a/pandora/item/views.py +++ b/pandora/item/views.py @@ -448,6 +448,7 @@ def setPoster(request): #parse path and return info ''' data = json.loads(request.POST['data']) item = get_object_or_404_json(models.Item, itemId=data['id']) + response = json_response() if item.editable(request.user): valid_urls = [p.url for p in item.poster_urls.all()] if data['url'] in valid_urls: @@ -458,6 +459,11 @@ def setPoster(request): #parse path and return info tasks.update_poster.delay(item.itemId) response = json_response() response['data']['poster'] = item.get_poster() + elif data['url'].endswith('/poster.pandora.jpg'): + item.poster_url = '' + item.save() + tasks.update_poster.delay(item.itemId) + response['data']['poster'] = item.get_poster() else: response = json_response(status=403, text='invalid poster url') else: @@ -529,6 +535,16 @@ def frame(request, id, position, size): raise Http404 return HttpFileResponse(frame, content_type='image/jpeg') +def poster_frame(request, id, position): + item = get_object_or_404(models.Item, itemId=id) + position = int(position) + frames = item.poster_frames() + print frames, position + if frames and len(frames) > position: + frame = frames[position] + return HttpFileResponse(frame, content_type='image/jpeg') + raise Http404 + def image_to_response(item, image, size=None): if size: @@ -543,6 +559,11 @@ def image_to_response(item, image, size=None): path = image.path return HttpFileResponse(path, content_type='image/jpeg') +def poster_local(request, id): + item = get_object_or_404(models.Item, itemId=id) + poster = item.path('poster.local.jpg') + poster = os.path.abspath(os.path.join(settings.MEDIA_ROOT, poster)) + return HttpFileResponse(poster, content_type='image/jpeg') def poster(request, id, size=None): item = get_object_or_404(models.Item, itemId=id)