通过Django上传Dailymotion中的电影文件

时间:2016-04-30 12:46:52

标签: python django file-upload dailymotion-api

我正在开发一个由协会组织的电影挑战的网络应用程序。我的协会和我想直接将提交的电影上传到协会的Dailymotion帐户。

我已经完成了localy的测试,以了解如何在Dailymotion中上传电影,并且它可以使用简单的Python脚本,Dailymotion提供的Python SDK以及使用存储在我的硬盘中的电影的直接路径。它就像一个魅力,所以我试图在我正在开发的Django应用程序中实现它。

我不明白会发生什么,但如果我尝试通过表单,它会返回给我这个错误:

TypeError at /a/bioinfuse/submit_movie/15

expected string or Unicode object, NoneType found

Request Method:     POST
Request URL:    http://127.0.0.1:8000/a/bioinfuse/submit_movie/15
Django Version:     1.9.5
Exception Type:     TypeError
Exception Value:    

expected string or Unicode object, NoneType found

Exception Location:     /home/nolwenn/.virtualenvs/jebif-django/local/lib/python2.7/site-packages/pp.py in submit, line 461
Python Executable:  /home/nolwenn/.virtualenvs/jebif-django/bin/python
Python Version:     2.7.6
Python Path:    

['/home/nolwenn/programmation/jebif',
 '/home/nolwenn/.virtualenvs/jebif-django/lib/python2.7',
 '/home/nolwenn/.virtualenvs/jebif-django/lib/python2.7/plat-x86_64-linux-gnu',
 '/home/nolwenn/.virtualenvs/jebif-django/lib/python2.7/lib-tk',
 '/home/nolwenn/.virtualenvs/jebif-django/lib/python2.7/lib-old',
 '/home/nolwenn/.virtualenvs/jebif-django/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/nolwenn/.virtualenvs/jebif-django/local/lib/python2.7/site-packages',
 '/home/nolwenn/.virtualenvs/jebif-django/lib/python2.7/site-packages']

这是submit_video视图:

def submit_movie(request, member):
    def submit_movie(d, file, data, m_id):
        q_movie = Movie.objects.get(id=m_id)
        url = d.upload(file)
        movie = d.post('/me/videos',
                       {'url': url, 'title': data['title'],
                        'published': 'true', 'channel': 'tech',
                        'private': 'true',
                        'description': data['description']})
        url = d.get('/video/'+movie,
                    {'fields': 'embed_url', 'id': id_movie})['embed_url']
        q_movie.url = url
        q_movie.save()

    context = base(request)
    role = Member.objects.get(user=member).role
    member = Member.objects.get(user=member)
    challenge = Challenge.objects.filter(is_open=True).order_by('stop_date')[0]
    if request.method == 'GET':
        submit_movie_form = SubmitMovieForm({'submit_date': now()})
    else:
        submit_movie_form = SubmitMovieForm(request.POST, request.FILES)

        if submit_movie_form.is_valid():
            title = submit_movie_form.cleaned_data['title']
            description = submit_movie_form.cleaned_data['description']
            submit_date = submit_movie_form.cleaned_data['submit_date']
            file_movie = request.FILES['file_movie']
            associated_key = AssociatedKey.objects.get(associated_key=member.associated_key)
            register_movie = Movie(challenge=challenge,
                                   associated_key=associated_key,
                                   title=title,
                                   description=description,
                                   submit_date=submit_date)
            register_movie.save()
            m_id = register_movie.id

            d = dailymotion.Dailymotion()
            d.set_grant_type('password', api_key=API_KEY,
                             api_secret=API_SECRET, scope=['manage_videos'],
                             info={'username': USERNAME, 'password': PASSWORD})
            data = {'title': title, 'description': description}
            job = pp.Server()
            job.submit(submit_movie, (d, file_movie, data, m_id))
            return HttpResponseRedirect(reverse('bioinfuse:index'))

    context['submit_movie_form'] = submit_movie_form
    context['role'] = role
    return render(request, "submit_movie.html", context)

SubmitMovieForm表单:

class SubmitMovieForm(forms.ModelForm):
    file_movie = forms.FileField(label="Votre vidéo")
    class Meta:
        model = Movie
        exclude = ('challenge', 'associated_key', 'movie_url', 'published')

使用的模板形式:

<form action="{% url 'bioinfuse:submit_movie'  user.id %}" method="post" enctype="multipart/form-data">{% csrf_token %}
    <fieldset>
        {{ submit_movie_form.as_p }}
        <input type="submit" value="Mettre à jour">
    </fieldset>
</form>

我想知道Django是否希望我首先在服务器中上传电影,但我希望直接在Dailymotion上传电影文件,而不必将文件存储在服务器中。

你知道如何在不在我们的服务器中创建临时文件的情况下直接上传Dailymotion中的电影吗?如果有可能,当然。

想要查看更多代码?继续GitHub repository,应用程序名称为bioinfuse。

TL; DR:如何通过Django上传Dailymotion中的电影,而不将其存储在Web应用程序的服务器上?

提前致谢!

1 个答案:

答案 0 :(得分:1)

不,不可能。该文件需要暂时在您的服务器内,因为请求通过您的服务器。