upload_video_1.py 765 B

1234567891011121314151617181920
  1. def download_video(self, media_id, filename, media=False, folder='videos'):
  2. if not media:
  3. self.media_info(media_id)
  4. media = self.last_json['items'][0]
  5. filename = '{0}_{1}.mp4'.format(media['user']['username'], media_id) if not filename else '{0}.mp4'.format(filename)
  6. try:
  7. clips = media['video_versions']
  8. except Exception:
  9. return False
  10. fname = os.path.join(folder, filename)
  11. if os.path.exists(fname):
  12. return os.path.abspath(fname)
  13. response = self.session.get(clips[0]['url'], stream=True)
  14. if response.status_code == 200:
  15. with open(fname, 'wb') as f:
  16. response.raw.decode_content = True
  17. shutil.copyfileobj(response.raw, f)
  18. return os.path.abspath(fname)