def send_to_twitter(self, twitter_token, twitter_secret, session): api = twitter.Api( consumer_key=self.twitter_consumer_key, consumer_secret=self.twitter_consumer_secret, access_token_key=twitter_token, access_token_secret=twitter_secret ) twit = Twit() lu=LinkUtils() link = lu.getAllData(session.url) if link.overall_score == 0: logging.debug('skipping to send twitt since content is not popular %s ' % session.url) return twit.generate_content(link, session.title, "via:@instaright") logging.info('twit: %s' % twit.text) if twit.text is None: logging.info('twit has no text ... skipping') return try: api.PostUpdate(twit.text) except: logging.error('Error while sending tweet %s: %s => %s ' % (twit.text, sys.exc_info()[0],sys.exc_info()[1]))
def post(self): url = self.request.get("url", None) url_hash = LinkUtils.getUrlHash(url) user = self.request.get("user", None) title = self.request.get("title", None) if url is None: logging.info("no url detected. skipping...") return count = 1 url = urllib2.unquote(url) domain = RequestUtils.getDomain(url) if not domain or len(domain) == 0: self.response.out.write("not url: %s skipping!\n" % url) return if domain in self.skip_domains: logging.info("filering out %s" % url) return lu = LinkUtils() link = lu.getAllData(url, count) logging.info("link overall score: %s" % link.overall_score) existingLink = None try: existingLink = Links.gql("WHERE url_hash = :1", url_hash).get() if existingLink is None: existingLink = Links.gql("WHERE url = :1", url).get() except BadValueError: logging.info("bad value url %s" % url) klout_score = UserUtils.getKloutScore(user, self.klout_api_key) share_margin = self.tw_margin if klout_score is not None: link.overall_score = link.overall_score * int(klout_score) logging.info("adjusted overall score %s" % link.overall_score) share_margin = share_margin * self.klout_correction logging.info("adjusting twit margin: %s" % share_margin) logging.info("link score %s tweet margin %s ( existing %s )" % (link.overall_score, share_margin, existingLink)) if link.overall_score > share_margin and (existingLink is None or not existingLink.shared): t = Twit() t.generate_content(link, title, "") # skip tweets is text emtpy and for root domains if t.text is None or LinkUtils.isRootDomain(link.url): logging.info("twit with no body. aborting") return execute_time = TaskUtil.execution_time() logging.info("scheduling tweet for %s" % str(execute_time)) mail.send_mail( sender="*****@*****.**", to="*****@*****.**", subject="Twit to queue!", html="Twitt: %s <br> score: %s" % (t.text, link.overall_score), body="Twitt: %s <br> score: %s" % (t.text[:500], link.overall_score), ) # taskqueue.add(url='/util/twitter/twit/task', eta=execute_time, queue_name='twit-queue', params={'twit':t.text}) taskqueue.add(url="/util/twitter/twit/task", queue_name="twit-queue", params={"twit": t.text}) # update article shared status if existingLink is not None: existingLink.shared = True existingLink.put() logging.info("updated link share status") else: logging.info("not scheduled for tweeting") lh = LinkHandler() lh.update_link(url, link)