def post_tweet_with_media(self, tweet, media_url, shorted_media_url, type): try: print("shorted url" + shorted_media_url) print("Downloading media...") arr = str(media_url).split('/') print(arr[len(arr)-1]) if type == 'video': arr = arr[len(arr)-1].split("?tag=1") arr = arr[0] elif type == 'photo': arr = arr[len(arr)-1] auth = OAuth1(client_key= constants.CONSUMER_KEY, client_secret= constants.CONSUMER_SCRET, resource_owner_secret= constants.ACCESS_SECRET, resource_owner_key= constants.ACCESS_KEY) r = requests.get(media_url, auth = auth) with open(arr, 'wb') as f: f.write(r.content) print("Media downloaded successfully!") if shorted_media_url in tweet: print("shorted url "+ str(shorted_media_url)) tweet = tweet.replace(shorted_media_url, "") else: print("No url in tweet") if type == 'video': try: videoTweet = VideoTweet(arr) videoTweet.upload_init() videoTweet.upload_append() videoTweet.upload_finalize() videoTweet.tweet(tweet) except ValueError as v: print(v) print("Exception happen") pass elif type == 'photo': self.api.update_with_media(filename=arr, status=tweet) os.remove(arr) print("Upload with media success!") except Exception as e: print(e) pass
def Thread(self, name, type, tweet): try: left = 0 right = 272 leftcheck = 260 check = tweet[leftcheck:right].split(' ') separator = len(check[-1]) tweet1 = tweet[left:right - separator] + '(cont..)' if type == 'photo' or type == 'animated_gif': complete = self.api.update_with_media(filename=name, status=tweet1).id elif type == 'video': videoTweet = VideoTweet(name) videoTweet.upload_init() videoTweet.upload_append() videoTweet.upload_finalize() complete = videoTweet.Tweet(tweet1) elif type == 'normal': complete = self.api.update_status(tweet1).id time.sleep(10) tweet2 = tweet[right - separator:len(tweet)] while len(tweet2) > 280: leftcheck += 272 left += 272 right += 272 check = tweet[leftcheck:right] separator = len(check[-1]) tweet2 = tweet[left:right - separator] + '(cont..)' complete = self.api.update_status( tweet2, in_reply_to_status_id=complete, auto_populate_reply_metadata=True).id time.sleep(10) tweet2 = tweet[right - separator:len(tweet)] self.api.update_status(tweet2, in_reply_to_status_id=complete, auto_populate_reply_metadata=True) time.sleep(10) except Exception as ex: print(ex) time.sleep(30) pass