1234567891011121314151617181920 |
- def download_video(self, media_id, filename, media=False, folder='videos'):
- if not media:
- self.media_info(media_id)
- media = self.last_json['items'][0]
- filename = '{0}_{1}.mp4'.format(media['user']['username'], media_id) if not filename else '{0}.mp4'.format(filename)
- try:
- clips = media['video_versions']
- except Exception:
- return False
- fname = os.path.join(folder, filename)
- if os.path.exists(fname):
- return os.path.abspath(fname)
- response = self.session.get(clips[0]['url'], stream=True)
- if response.status_code == 200:
- with open(fname, 'wb') as f:
- response.raw.decode_content = True
- shutil.copyfileobj(response.raw, f)
- return os.path.abspath(fname)
|