1
votes

I am new to python, I wrote simple script for uploading video from url to vk, I test this script with small files it's working, but for large files I get run out of memory, I read that using 'requests_toolbelt' it's possible to post large file, How can I add this to my script?

import vk
import requests
from homura import download
import glob
import os
import json


url=raw_input("Enter URL: ")
download(url)

file_name = glob.glob('*.mp4')[0]

session = vk.Session(access_token='TOKEN')
vkapi = vk.API(session,v='5.80' )
params={'name' : file_name,'privacy_view' : 'nobody', 'privacy_comment' : 'nobody'}
param = vkapi.video.save(**params)
upload_url = param['upload_url']

print ("Uploading ...")
request = requests.post(upload_url, files={'video_file': open(file_name, "rb")})

os.remove (file_name)

1
Please post the error and traceback.Itay4
when run script, after downloading it, for uploading it show 'killed' and close script, for small files I didn't get this error.Bardulf

1 Answers

1
votes

requests_toolbelt (https://github.com/requests/toolbelt) has just the example that might work for you:

import requests
from requests_toolbelt import MultipartEncoder
...
...
m=MultipartEncoder( fields={'video_file':(file_name, open(file_name, "rb"))})
response = requests.post(upload_url, data=m, headers={'Content-Type': m.content_type})

If you know your video file's MIME type, you can add it as a 3-rd item in the () tuple like this:

m=MultipartEncoder( fields={
  'video_file':(file_name, open(file_name,"rb"), "video/mp4")})