def post(self): status = self.request.get('status') phone = self.request.get('phone') count = int(self.request.get('count')) tuser = TwitterUser.get_by_phonenumber(phone) if tuser == None: logging.warning("Could not fetch tuser based on phone number %s",phone) return client = OAuthClient('twitter', self) try: info = client.post('/statuses/update', 'POST', (200,401,403), tuser, status=status) if 'error' in info: logging.warning("Submiting failed as credentials were incorrect (user:%s) %s", tuser.user, info['error']) tuser.lastError = "Twitter returned '%s' for your last update. You may be over limit or may have to register with SMSTweet again" % info['error'] tuser.put() else: logging.debug("updated the status for user %s", tuser.user) Tweet.save_tweet(info) except (urlfetch.DownloadError, ValueError), e: logging.warning("Update:update (%d) could not be fetched. %s " % (count,e)) if count > 10: logging.error("Tried updating the message 10 times. Finally giving up.") else: # Try again taskqueue.add(url = '/tasks/post_message', params = { 'phone' : phone, 'count' : count + 1, 'status' : status })
def fetch_status(self,type,key_name, url): phone = self.request.get('phone') tuser = TwitterUser.get_by_phonenumber(phone) if tuser == None: logging.warning("Could not fetch tuser based on phone number %s",phone) return since_id = memcache.get("%s%s" % (key_name,tuser.user)) if not since_id: since_id = -1 memcache.add("%s%s" % (key_name,tuser.user), since_id) client = OAuthClient('twitter', self) try: info = client.get(url, 'GET', (200,401,403), tuser, since_id=since_id, count = 10) if 'error' in info: logging.warning("%s Fetch failed for %s because of %s" % (type,tuser.user, info['error'])) elif len(info) > 0: logging.debug("fetched %d %s's for %s" % (len(info), type,tuser.user)) memcache.replace("%s%s" % (key_name,tuser.user), info[0]['id']) if type == 'DM': for dm in info: TweetDM.create(dm['sender_screen_name'],tuser.user, dm['text'], dm['created_at'], dm['id']) else: for dm in info: TweetMention.create(dm['user']['screen_name'],tuser.user, dm['text'], dm['created_at'], dm['id']) #endif #endif except (urlfetch.DownloadError, ValueError), e: logging.warning("%s: could not be fetched. %s " % (type,e))
def is_active_user(self): self.keyword = self.request.get('keyword') self.content = self.request.get('content') self.phoneno = self.request.get('msisdn') if self.phoneno == None or self.content == None: self.response.out.write("Please provide both msisdn and content") return False # Check if the phoneno is registerd and is active self.tuser = TwitterUser.get_by_phonenumber(self.phoneno) if self.tuser and self.tuser.active: return True logging.warning("Unregistered user tried to get their updates") self.response.out.write("SMSTweet: This command works only for registered users. Register at http://www.smstweet.in") return False
def get(self): phonecode = self.request.get('phonecode') keyword = self.request.get('keyword') location = self.request.get('location') carrier = self.request.get('carrier') self.content = self.request.get('content') self.phoneno = self.request.get('msisdn') if self.phoneno == None or self.content == None: self.response.out.write("Please provide both msisdn and content") return # Check if the phoneno is registerd and is active tuser = TwitterUser.get_by_phonenumber(self.phoneno) r = re.compile('^\s*twe*t\s*',re.I) self.content = r.sub('',self.content) if tuser and tuser.active: if len(self.content) == 0: self.response.out.write("Dude !! where is the message to be sent? Hit the send message too fast") return if re.match("^register", self.content, re.I): # if content starts with register self.response.out.write("Your message cannot start with register as it is a keyword\n") return updated = False status = self.content[0:139] # makes sure status is 140 chars long self.updateStatuswithToken(tuser, status) try: dstat = DailyStat.get_by_date() dstat.new_tweet() tuser.incr_counter(location,carrier) tuser.fetch_mentions_and_dms() Stats.updateCounter(tuser.user) except Timeout, e: logging.warning("Timed out logging the stats !! never mind") except DeadlineExceededError, e: logging.error("Deadline exceeded while logging the stats !! never mind")