diff --git a/instabot/api/api.py b/instabot/api/api.py index 70a1923bd..304708628 100644 --- a/instabot/api/api.py +++ b/instabot/api/api.py @@ -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) diff --git a/instabot/api/api_photo.py b/instabot/api/api_photo.py index 9e6ba95a7..f34642108 100644 --- a/instabot/api/api_photo.py +++ b/instabot/api/api_photo.py @@ -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: @@ -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 diff --git a/instabot/api/api_video.py b/instabot/api/api_video.py index 9ae6cf932..2a9d23125 100644 --- a/instabot/api/api_video.py +++ b/instabot/api/api_video.py @@ -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, @@ -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 @@ -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 @@ -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 diff --git a/instabot/bot/bot.py b/instabot/bot/bot.py index 849c928eb..57df2487f 100755 --- a/instabot/bot/bot.py +++ b/instabot/bot/bot.py @@ -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) diff --git a/instabot/bot/bot_photo.py b/instabot/bot/bot_photo.py index 90718019e..533749f7e 100644 --- a/instabot/bot/bot_photo.py +++ b/instabot/bot/bot_photo.py @@ -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): diff --git a/instabot/bot/bot_video.py b/instabot/bot/bot_video.py index a6516650d..29daa4f16 100644 --- a/instabot/bot/bot_video.py +++ b/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):