Possible Duplicate:
Problem accessing user uploaded video in temporary memory
So I've tried this question here and here but I might have been being too specific or misguided in my question.
Essentially I am trying to take a video submitted via <form> <input type='file'/> </form>
and submit it to youtube.
I've looked at youtube direct, but it is a nightmare to setup and doesn't really provide the functionality I'm looking for. I've looked at the youtube data api but as stated in the previous question the docs are somewhat lacking in specificity regarding implementation in python.
If anyone could walk me through this or point me in the direction of a great tutorial I would be forever grateful.
My previous questions pretty well outline what I am currently trying but here it is for quick reference:
html:
<form method='post' action='/new/' enctype="multi-part/form-data">{% csrf_token %}
<input type='file' name='file' id='file'/>
<input type='submit' />
</form>
django view (youtube-upload is a python module that uses the youtube data api to upload a file to youtube using the information specified):
def upload_video(request):
if request.method == 'POST':
video = request.FILE['file']
v = video.temporary_file_path
command = 'youtube-upload --email=email@gmail.com --password=password --title=title --description=description --category=Sports ' + v
r=subprocess.Popen(command, stdout=subprocess.PIPE)
vid = r.stdout.read()
else:
form = VideoForm()
request.upload_handlers.pop(0)
return render_to_response('create_check.html', RequestContext(request, locals() ) )
currently v=video.tempor开发者_开发问答ary_file_path draws
the error 'InMemoryUploadedFile' object has no attribute 'temporary_file_path'
.
Thanks for your help.
Your current problem is that your file hasn't been saved to disk, so how could it have a path?
There are two obvious solutions. First, you can change your FILE_UPLOAD_HANDLERS setting to have only one handler, like so:
("django.core.files.uploadhandler.TemporaryFileUploadHandler",)
so it will automatically save all files to disk.
Second, you could save the file manually by doing
with open('some/file/name', 'wb+') as destination:
for chunk in video.chunks():
destination.write(chunk)
and then just pass some/file/name
in the arguments to youtube_upload.main_upload
.
If you had read the Django Docs which answers to both of your first two questions pointed you to, you would know this.
The source of Youtube Upload makes it clear it can certainly be imported. Simply import youtube_upload
with it in your path and then call it with youtube_upload.main_upload(arguments, output)
where arguments
is
['--email=email@gmail.com', '--password=password', '--title=title', '--description=description', '--category=Sports', 'path/to/file']
The output
option defaults to sys.stdout
, you can redirect it if you need it.
精彩评论