Skip to content
This repository has been archived by the owner on Feb 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #904 from adw0rd/master
Browse files Browse the repository at this point in the history
Optimize generation thumbnail, return media info instead True, added configure_photo_timeout
  • Loading branch information
ohld committed May 17, 2019
2 parents cb27b73 + 1951b9a commit 74f8e9e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
4 changes: 2 additions & 2 deletions instabot/api/api.py
Expand Up @@ -312,8 +312,8 @@ def expose(self):
})
return self.send_request('qe/expose/', data)

def upload_photo(self, photo, caption=None, upload_id=None, from_video=False):
return upload_photo(self, photo, caption, upload_id, from_video)
def upload_photo(self, photo, caption=None, upload_id=None, from_video=False, configure_photo_timeout=15):
return upload_photo(self, photo, caption, upload_id, from_video, configure_photo_timeout)

def download_photo(self, media_id, filename, media=False, folder='photos'):
return download_photo(self, media_id, filename, media, folder)
Expand Down
16 changes: 10 additions & 6 deletions instabot/api/api_photo.py
Expand Up @@ -86,7 +86,7 @@ def configure_photo(self, upload_id, photo, caption=''):


def upload_photo(self, photo, caption=None, upload_id=None, from_video=False,
force_rezize=False):
force_rezize=False, configure_photo_timeout=15):
if upload_id is None:
upload_id = str(int(time.time() * 1000))
if not photo:
Expand Down Expand Up @@ -121,11 +121,15 @@ def upload_photo(self, photo, caption=None, upload_id=None, from_video=False,
config.API_URL + "upload/photo/", data=m.to_string())

if response.status_code == 200:
if self.configure_photo(upload_id, photo, caption):
self.expose()
from os import rename
rename(photo, "{}.REMOVE_ME".format(photo))
return True
for attempt in range(4):
if configure_photo_timeout:
time.sleep(configure_photo_timeout)
if self.configure_photo(upload_id, photo, caption):
media = self.last_json.get('media')
self.expose()
from os import rename
rename(photo, "{}.REMOVE_ME".format(photo))
return media
return False


Expand Down
18 changes: 9 additions & 9 deletions instabot/api/api_video.py
Expand Up @@ -73,9 +73,7 @@ def get_video_info(filename):
def upload_video(self, video, caption=None, upload_id=None, thumbnail=None, configure_video_timeout=15):
if upload_id is None:
upload_id = str(int(time.time() * 1000))
video, thumbnail_path, width, height, duration = resize_video(video)
if not thumbnail:
thumbnail = thumbnail_path
video, thumbnail, width, height, duration = resize_video(video, thumbnail)
data = {
'upload_id': upload_id,
'_csrftoken': self.token,
Expand Down Expand Up @@ -138,10 +136,11 @@ def upload_video(self, video, caption=None, upload_id=None, thumbnail=None, conf
if configure_video_timeout:
time.sleep(configure_video_timeout)
if self.configure_video(upload_id, video, thumbnail, width, height, duration, caption):
media = self.last_json.get('media')
self.expose()
from os import rename
rename(video, "{}.REMOVE_ME".format(video))
return True
return media
return False


Expand Down Expand Up @@ -171,7 +170,7 @@ def configure_video(self, upload_id, video, thumbnail, width, height, duration,
return self.send_request('media/configure/?video=1', data)


def resize_video(fname):
def resize_video(fname, thumbnail=None):
from math import ceil
try:
import moviepy.editor as mp
Expand Down Expand Up @@ -232,7 +231,8 @@ def resize_video(fname):
new_fname = "{}.CONVERTED.mp4".format(fname)
print("Saving new video w:{w} h:{h} to `{f}`".format(w=w, h=h, f=new_fname))
vid.write_videofile(new_fname, codec="libx264", audio_codec="aac")
print("Generating thumbnail...")
thumbnail_name = "{}.jpg".format(fname)
vid.save_frame(thumbnail_name, t=(vid.duration / 2))
return new_fname, thumbnail_name, w, h, vid.duration
if not thumbnail:
print("Generating thumbnail...")
thumbnail = "{}.jpg".format(fname)
vid.save_frame(thumbnail, t=(vid.duration / 2))
return new_fname, thumbnail, w, h, vid.duration
4 changes: 2 additions & 2 deletions instabot/bot/bot.py
Expand Up @@ -532,8 +532,8 @@ def upload_photo(self, photo, caption=None, upload_id=None, from_video=False):

# video

def upload_video(self, video, caption=''):
return upload_video(self, video, caption)
def upload_video(self, video, caption='', thumbnail=None):
return upload_video(self, video, caption, thumbnail)

def download_video(self, media_id, folder='videos', filename=None, save_description=False):
return download_video(self, media_id, folder, filename, save_description)
Expand Down
11 changes: 6 additions & 5 deletions instabot/bot/bot_photo.py
Expand Up @@ -6,11 +6,12 @@

def upload_photo(self, photo, caption=None, upload_id=None, from_video=False):
self.small_delay()
if self.api.upload_photo(photo, caption, upload_id, from_video):
self.logger.info("Photo '{}' is uploaded.".format(photo))
return True
self.logger.info("Photo '{}' is not uploaded.".format(photo))
return False
result = self.api.upload_photo(photo, caption, upload_id, from_video)
if not result:
self.logger.info("Photo '{}' is not uploaded.".format(photo))
return False
self.logger.info("Photo '{}' is uploaded.".format(photo))
return result


def download_photo(self, media_id, folder='photos', filename=None, save_description=False):
Expand Down
7 changes: 4 additions & 3 deletions instabot/bot/bot_video.py
@@ -1,14 +1,15 @@
import os


def upload_video(self, video, caption=''):
def upload_video(self, video, caption='', thumbnail=None):
self.small_delay()
self.logger.info("Started uploading '{video}'".format(video=video))
if not self.api.upload_video(video, caption):
result = self.api.upload_video(video, caption, thumbnail)
if not result:
self.logger.info("Video '%s' is not %s ." % (video, 'uploaded'))
return False
self.logger.info("Video '{video}' uploaded".format(video=video))
return True
return result


def download_video(self, media_id, folder='videos', filename=None, save_description=False):
Expand Down

0 comments on commit 74f8e9e

Please sign in to comment.