I want to download a file from the web and upload it to dropbox.
import multiprocessing as m import requests as rr import dropbox url='http://www.onirikal.com/videos/mp4/battle_games.mp4' db=dropbox.Dropbox(Accesstoken) def d(url): r=rr.get(url,stream=True) with open('file.mp4','wb')as f: for a in r.iter_content(chunk_size=1000000): if a: f.truncate(0) f.write(a)
def u(): try: with open('file.mp4','rb')as ff: db.files_upload(ff.read(),'/file.mp4') except FileNotFoundError: pass if __name__=='__main__': p=m.Pool() re= p.apply_async(d,[url]) ree=p.apply_async(u) re.get(timeout=10) ree.get(timeout=10)
The 'files_upload' method will take and upload whatever data you give it. If you're unexpectedly getting a 0 byte file, the first step in debugging this would be to check on 'ff.read()'. Is that working properly and giving you the data you expect?
I notice that you're running both your 'd' and 'u' methods asychronously though, via 'apply_async'. It looks like 'u' is therefore running before 'd' completes, meaning the file isn't written to disk yet. Accordingly, there's nothing to upload from 'f.read()'. You should make sure you don't run 'u' until 'd' is complete. That's more of a general Python threading question though, and not about Dropbox itself, so I can't offer much help with that.