def import_video(request): if request.method == 'POST': form = VideoImportForm(request.POST) if form.is_valid(): rules = list(ProviderRule.objects.all()) patterns = [re.compile(r.regex) for r in rules] for i, part in re_parts(patterns, form.cleaned_data['url']): rule = rules[i] FORMAT = getattr(settings, "OEMBED_FORMAT", "json") url = u"%s?url=%s&maxwidth=%s&maxheight=%s&format=%s" % ( rule.endpoint, part, 600, 400, FORMAT ) resp = simplejson.loads(fetch(url)) replacement = render_to_string('oembed/%s.html' % resp['type'], {'response': resp}) # TODO: this should be in oembed, bit dont want to modify it now if(resp): if(resp.has_key('thumbnail_url')): thumbnail_url = resp['thumbnail_url'] else: p = re.compile('v=([^&]+)') m = p.search(form.cleaned_data['url']) id = m.group() id = id.replace("v=","") thumbnail_url = "http://img.youtube.com/vi/"+id+"/1.jpg" #import pdb; pdb.set_trace() video = Video(creator= request.user, import_url=form.cleaned_data['url'], was_uploaded=False, thumbnail_url=thumbnail_url, title=form.cleaned_data['title'], ) video.save() request.user.message_set.create(message=_("Video '%s' was \ successfully imported!") % video.title) return HttpResponseRedirect(reverse('show_video', args=(video.id,))) else: return render_to_response('video/unsuccessful_import.html', { 'url': form.cleaned_data['url'] }, context_instance=RequestContext(request)); else: form = VideoImportForm(request.POST) return render_to_response("", {'form': form}, context_instance=RequestContext(request))
def save(self, user): video = None rules = list(ProviderRule.objects.all()) patterns = [re.compile(r.regex) for r in rules] self.full_clean() for i, part in re_parts(patterns, self.cleaned_data['url']): rule = rules[i] FORMAT = getattr(settings, "OEMBED_FORMAT", "json") url = u"%s?url=%s&maxwidth=%s&maxheight=%s&format=%s" % ( rule.endpoint, part, 600, 400, FORMAT ) resp = simplejson.loads(fetch(url)) replacement = render_to_string('oembed/%s.html' % resp['type'], {'response': resp}) # TODO: this should be in oembed, bit dont want to modify it now if(resp): if(resp.has_key('thumbnail_url')): thumbnail_url = resp['thumbnail_url'] else: p = re.compile('v=([^&]+)') m = p.search(self.cleaned_data['url']) id = m.group() id = id.replace("v=","") thumbnail_url = "http://img.youtube.com/vi/"+id+"/1.jpg" #import pdb; pdb.set_trace() video = Video(creator= user, import_url=self.cleaned_data['url'], was_uploaded=False, thumbnail_url=thumbnail_url, title=self.cleaned_data['title'], ) video.save() return video