def receive(self, mail_message): try: email = mail_message.to.split('@') except: email = None email_prefix = email[0].split('.') screen_name_temp = email_prefix[0].split('<') try: screen_name = screen_name_temp[1] except: screen_name = screen_name_temp[0] email_key = email_prefix[1] email_user = User.find_by_screen_name(screen_name.lower()); try: if(email_key == email_user.email_key): # Set Up Twitter Client callback_url = "%s/verify" % self.request.host_url client = oauth.TwitterClient(MainHandler.application_key, MainHandler.application_secret, callback_url) #Get Post plaintext_bodies = mail_message.bodies('text/plain') for content_type, body in plaintext_bodies: decoded_text = body.decode() content = decoded_text try: subject = mail_message.subject + "\n\r" except: subject = '' if content is not None: body = functions.strip_tags(content) else: body = '' #Save Post post_info = {} post_info['content'] = subject + body post_key = Post.save_post(post_info) #Save Tweetc user = User.get_by_key_name(str(email_user.twitter_id)) tweetc_info = {} tweetc_info['tweet_id'] = None tweetc_info['tweet'] = None tweetc_info['user'] = user.key() tweetc_info['post'] = post_key tweetc_info['client'] = 'email' tweetc_key = Tweetc.save_tweetc(tweetc_info) #Send Tweet tweetc = Tweetc.get(tweetc_key) tweet = functions.word_wrap(content,108,prefix ="||") tweet = tweet.split("||") tweetc_code = tweetc.code() del tweet[0] if len(tweet) == 1: i = 1 elif len(tweet) == 2: i = 2 else: i = 3 count = 0 tweet_list = [] while (count < i): last = i - 1 if count == last: tweetout = tweet[count] + ' ' + str(count + 1) + '/' + str(i) if len(tweet) > 3: tweetout += '...' tweetout += ' http://tweetc.com/' + tweetc_code else: tweetout = tweet[count] + ' ' + str(count + 1) + '/' + str(i) self.response.out.write(tweetout) tweet_list.append(tweetout) count = count + 1 first_tweet = tweet_list[0] + ' http://tweetc.com/' + tweetc_code tweet_list.reverse() count = 0 while (count < i): self.response.out.write(tweet_list[count] + '<br>') additional_params={"status":tweet_list[count]} url = "http://twitter.com/statuses/update.json" result = client.make_request(url=url, token=user.token,secret=user.secret, additional_params=additional_params, method=urlfetch.POST) rsp = json.loads(result.content) if count == 0: status_id = rsp['id'] count = count + 1 #Update Tweetc with Tweet ID tweetc = Tweetc.get(tweetc_key) tweetc.tweet_id = status_id tweetc.tweet = first_tweet tweetc.put() except: logging.info(screen_name) logging.info(email_key) logging.info("Sending Email Error")
def post(self, action): #Start Session self.session = sessions.Session() if (action == "settings"): key =self.request.get('email_key') if(len(key) is 0): return self.redirect('/settings' ) user_info = {} user_info['id'] = str(self.session["twitter_id"]) user_info['email_key'] = key User.update_settings(user_info) return self.redirect('/settings' + '?p=1' ) if (action == "post"): # Set Up Twitter Client callback_url = "%s/verify" % self.request.host_url client = oauth.TwitterClient(MainHandler.application_key, MainHandler.application_secret, callback_url) #Get Post content = self.request.get('content') contet = functions.strip_tags(content) #Save Post post_info = {} post_info['content'] = content post_key = Post.save_post(post_info) #Save Tweetc user = User.get_by_key_name(str(self.session["twitter_id"])) tweetc_info = {} tweetc_info['tweet_id'] = None tweetc_info['tweet'] = None tweetc_info['user'] = user.key() tweetc_info['post'] = post_key tweetc_info['client'] = 'web' tweetc_key = Tweetc.save_tweetc(tweetc_info) #Send Tweet tweetc = Tweetc.get(tweetc_key) tweet = functions.word_wrap(content,108,prefix ="||") tweet = tweet.split("||") tweetc_code = tweetc.code() del tweet[0] if len(tweet) == 1: i = 1 elif len(tweet) == 2: i = 2 else: i = 3 count = 0 tweet_list = [] while (count < i): last = i - 1 if count == last: tweetout = tweet[count] + ' ' + str(count + 1) + '/' + str(i) if len(tweet) > 3: tweetout += '...' tweetout += ' http://tweetc.com/' + tweetc_code else: tweetout = tweet[count] + ' ' + str(count + 1) + '/' + str(i) self.response.out.write(tweetout) tweet_list.append(tweetout) count = count + 1 first_tweet = tweet_list[0] + ' http://tweetc.com/' + tweetc_code tweet_list.reverse() count = 0 while (count < i): self.response.out.write(tweet_list[count] + '<br>') additional_params={"status":tweet_list[count]} url = "http://twitter.com/statuses/update.json" result = client.make_request(url=url, token=user.token,secret=user.secret, additional_params=additional_params, method=urlfetch.POST) rsp = json.loads(result.content) if count == 0: status_id = rsp['id'] count = count + 1 #Update Tweetc with Tweet ID tweetc = Tweetc.get(tweetc_key) tweetc.tweet_id = status_id tweetc.tweet = first_tweet tweetc.put() return self.redirect('/user/' + user.screen_name + '?p=1' )