def get(self, service): # self.response.headers['Content-Type'] = 'text/plain' # self.response.out.write('Hello, webapp!') self.cache = {} self.statusCache = {} self.longUrls = {} self.template_values = {} self.client = OAuthClient('twitter', self) link = self.request.get('url') if not self.client.get_cookie(): self.template_values['logged'] = False else: self.template_values['logged'] = True info = self.client.get('/account/verify_credentials') self.template_values['info'] = info # rate_info = self.client.get('/account/rate_limit_status') # write("<strong>API Rate Limit Status:</strong> %r" % rate_info) if "" != link: self.template_values['link'] = link template = jinja_environment.get_template('index.html') self.response.out.write(template.render(self.template_values))
def get(self, link): self.cache = {} self.client = OAuthClient("twitter", self) if not self.client.get_cookie(): self.response.out.write("NEEDLOGIN") return page = num(self.request.get("page")) if isinstance(page, (int, long)): self.queryTwitter(link, page) else: self.queryTwitter(link, 1)
class CheckLink(webapp2.RequestHandler): # def __init__(self): def get(self, link): self.cache = {} self.client = OAuthClient("twitter", self) if not self.client.get_cookie(): self.response.out.write("NEEDLOGIN") return page = num(self.request.get("page")) if isinstance(page, (int, long)): self.queryTwitter(link, page) else: self.queryTwitter(link, 1) def queryTwitter(self, link, x): write = self.response.out.write print "looking up, Page " + str(x) + " for " + link try: statuses = self.client.get("/statuses/home_timeline", page=x, include_entities="true") # except DeadlineExceededError: except Exception: print "Big bad exception. Trying again" self.queryTwitter(link, x) return # there are 20 tweets in a page # print "Status # " + str(len(statuses)) for status in statuses: entities = status["entities"] # print status['id'] urls = entities["urls"] # print urls if urls: for url in urls: # print url # statusId = status['id'] # self.statusCache[statusId] = status # BAD OPTIMISATION ! # if the net location of the url is longer than 20 chars # no need to check if the url is a redirect # if len(urlparse(url['expanded_url']).netloc) > 20 : unshortenThis = url["expanded_url"] if unshortenThis is not None: print "- to unshorten: " + unshortenThis expandedUrl = self.unshorten_url(unshortenThis) # else: # expandedUrl = url['expanded_url'] print "-> " + expandedUrl if self.same_urls(expandedUrl, link): print "YAY" # USER/status/STATUS_ID tweet = status["user"]["screen_name"] + "/status/" + str(status["id"]) write('{"page":"' + str(x) + '","url":"' + str(tweet) + '"}') return # self.longUrls[expandedUrl] = statusId write('{"page":"' + str(x) + '","url":""}') def unshorten(self, url, trialNumber=1): if trialNumber > 3: return "" try: result = self.cache.get(url) if result is None: result = urllib2.urlopen(url, timeout=10).geturl() self.cache[url] = result except urllib2.HTTPError, error: print "HTTPError " + error.read() result = "HTTPError" except httplib.HTTPException: # in this case we should probably just try again result = "no clue what is going on, tried " + str(trialNumber) + " times." self.unshorten(url, trialNumber + 1)