Beispiel #1
0
    def get(self, action=""):
        if action == "messages":
            type = self.request.get('type')
            query = {"key":settings.GOOGLEPLUS_API_KEY}
            pageToken = self.request.get('pageToken')
            if pageToken:
                query["pageToken"] = pageToken
            if type.startswith("search/"):
                url = "https://www.googleapis.com/plus/v1/activities"
                query["query"] = type.split('/',1)[1]
                #url += "?" + urllib.urlencode(query)念のためコメントアウト
                url += "?" + utils.encoded_urlencode(query)
                json = memcache.get(url)
                if json is None:
                    response = urlfetch.fetch(url)
                    logging.info(response.content)
                    json = simplejson.loads(response.content)
#                    memcache.set(url, json, 2*60) #2分キャッシュ
                result_json = {
                    'service': 'googleplus',
                    'nextPageToken': json.get('nextPageToken'),
                    'messages': json.get("items")
                }
                self.response.headers["Cache-Control"] = "public, max-age=120"
                self.response.headers["Content-Type"] = "application/json"
                return self.response.out.write(simplejson.dumps(result_json))
            self.error(400)
        elif action == "add_column":
            tmpl = os.path.join(os.path.dirname(__file__), "../view/googleplus_add_column.html")
            return self.response.out.write(template.render(tmpl, {}))
        self.error(400)
Beispiel #2
0
    def get(self, action=""):
        if action == "messages":
            type = self.request.get('type')
            query = {"key": settings.GOOGLEPLUS_API_KEY}
            pageToken = self.request.get('pageToken')
            if pageToken:
                query["pageToken"] = pageToken
            if type.startswith("search/"):
                url = "https://www.googleapis.com/plus/v1/activities"
                query["query"] = type.split('/', 1)[1]
                #url += "?" + urllib.urlencode(query)念のためコメントアウト
                url += "?" + utils.encoded_urlencode(query)
                json = memcache.get(url)
                if json is None:
                    response = urlfetch.fetch(url)
                    logging.info(response.content)
                    json = simplejson.loads(response.content)
#                    memcache.set(url, json, 2*60) #2分キャッシュ
                result_json = {
                    'service': 'googleplus',
                    'nextPageToken': json.get('nextPageToken'),
                    'messages': json.get("items")
                }
                self.response.headers["Cache-Control"] = "public, max-age=120"
                self.response.headers["Content-Type"] = "application/json"
                return self.response.out.write(simplejson.dumps(result_json))
            self.error(400)
        elif action == "add_column":
            tmpl = os.path.join(os.path.dirname(__file__),
                                "../view/googleplus_add_column.html")
            return self.response.out.write(template.render(tmpl, {}))
        self.error(400)
Beispiel #3
0
 def request_with_account(cls, account, url, method="GET", params={}, deadline=2):
     mkey = "error_" + cls.service
     error_count = memcache.get(mkey) or 0
     if int(error_count) == ERROR_COUNT_LIMIT: #連続して規定回数のエラーが起きたらそのサービスはダウンしていると判断
         memcache.set(mkey, error_count+1, ERROR_COUNT_RESET_TIME) #5分間は停止
     if int(error_count) >= ERROR_COUNT_LIMIT: #連続して規定回数のエラーが起きたらそのサービスはダウンしていると判断
         logging.warn("Maybe %s is down now." % cls.service)
         return {"status":"999"}, ""
     
     params = utils.encoded_urlencode(params)
     
     token = oauth.Token(account.access_token, account.secret)
     client = cls.newOAuthClient(token)
     
     try:
         resp, content = client.request(url, method, params, deadline=deadline)
         status = int(resp["status"])
         if status == 200:
             memcache.set(mkey, 0, ERROR_COUNT_RESET_TIME) #成功したらカウントリセット
         elif status >= 500:
             memcache.set(mkey, error_count+1, ERROR_COUNT_RESET_TIME) #エラー回数をカウント
             logging.warn('error count=%s, service=%s', error_count+1, cls.service)
         return resp, content
     except DownloadError, e:
         memcache.set(mkey, error_count+1, ERROR_COUNT_RESET_TIME) #1分間エラー回数をカウント
         logging.warn('error count=%s, service=%s', error_count+1, cls.service)
         raise e
Beispiel #4
0
    def call(self,
             url,
             account_name=None,
             params={},
             method="get",
             body=None,
             content_type=None,
             access_token=None,
             account=None):
        user = self.session.get_user()
        is_retry = account is not None
        if access_token is None:
            if account is None:
                account = Account.gql(
                    "WHERE service = :1 and account_name = :2 and user_ref = :3",
                    "chatter", account_name, user.key()).get()
            access_token = account.access_token
            account_info = simplejson.loads(account.account_info)
            if not url.startswith("http"):
                url = account_info["urls"]["rest"].replace(
                    "{version}", "22.0") + url
        headers = {"Authorization": "OAuth " + access_token}

        #query = urllib.urlencode(params) 念のためコメントアウト
        query = utils.encoded_urlencode(params)
        response = None

        if method == "get":
            response = urlfetch.fetch(url + "?" + query, headers=headers)
        elif method == "post":
            if body:
                query = body
            headers[
                'Content-Type'] = content_type or 'application/x-www-form-urlencoded; charset=utf-8'
            response = urlfetch.fetch(url,
                                      payload=query,
                                      method=urlfetch.POST,
                                      headers=headers)
        elif method == "delete":
            response = urlfetch.fetch(url + "?" + query,
                                      method=urlfetch.DELETE,
                                      headers=headers)
        logging.info(url)
        logging.info(query)
        logging.info(str(response.status_code))
        logging.info(response.content)
        if response.status_code == 401 and not is_retry:
            args = dict(client_id=settings.CHATTER_CLIENT_ID,
                        client_secret=settings.CHATTER_CLIENT_SECRET,
                        grant_type='refresh_token',
                        refresh_token=account.refresh_token)
            account = self.get_token(args)
            response = self.call(url,
                                 account=account,
                                 params=params,
                                 method=method)
        return response
Beispiel #5
0
    def call(
        self,
        url,
        account_name=None,
        params={},
        method="get",
        body=None,
        content_type=None,
        access_token=None,
        account=None,
    ):
        user = self.session.get_user()
        is_retry = account is not None
        if access_token is None:
            if account is None:
                account = Account.gql(
                    "WHERE service = :1 and account_name = :2 and user_ref = :3", "chatter", account_name, user.key()
                ).get()
            access_token = account.access_token
            account_info = simplejson.loads(account.account_info)
            if not url.startswith("http"):
                url = account_info["urls"]["rest"].replace("{version}", "22.0") + url
        headers = {"Authorization": "OAuth " + access_token}

        # query = urllib.urlencode(params) 念のためコメントアウト
        query = utils.encoded_urlencode(params)
        response = None

        if method == "get":
            response = urlfetch.fetch(url + "?" + query, headers=headers)
        elif method == "post":
            if body:
                query = body
            headers["Content-Type"] = content_type or "application/x-www-form-urlencoded; charset=utf-8"
            response = urlfetch.fetch(url, payload=query, method=urlfetch.POST, headers=headers)
        elif method == "delete":
            response = urlfetch.fetch(url + "?" + query, method=urlfetch.DELETE, headers=headers)
        logging.info(url)
        logging.info(query)
        logging.info(str(response.status_code))
        logging.info(response.content)
        if response.status_code == 401 and not is_retry:
            args = dict(
                client_id=settings.CHATTER_CLIENT_ID,
                client_secret=settings.CHATTER_CLIENT_SECRET,
                grant_type="refresh_token",
                refresh_token=account.refresh_token,
            )
            account = self.get_token(args)
            response = self.call(url, account=account, params=params, method=method)
        return response
Beispiel #6
0
def call(user, account, url, params={}, method="get"):
    account = Account.gql(
            "WHERE service = :1 and account_name = :2 and user_ref = :3",
            "facebook",
            account,
            user.key()).get()
    params["access_token"] = account.access_token
    
    query = utils.encoded_urlencode(params)
    response = None
    
    if method == "get":
        response = urlfetch.fetch(url+"?"+query, deadline=3)
    elif method == "post":
        response = urlfetch.fetch(url, payload=query, method=urlfetch.POST, deadline=10)
    elif method == "delete":
        response = urlfetch.fetch(url+"?"+query, method=urlfetch.DELETE, deadline=10)
    return response
Beispiel #7
0
def call(user, account, url, params={}, method="get"):
    account = Account.gql(
        "WHERE service = :1 and account_name = :2 and user_ref = :3",
        "facebook", account, user.key()).get()
    params["access_token"] = account.access_token

    query = utils.encoded_urlencode(params)
    response = None

    if method == "get":
        response = urlfetch.fetch(url + "?" + query, deadline=3)
    elif method == "post":
        response = urlfetch.fetch(url,
                                  payload=query,
                                  method=urlfetch.POST,
                                  deadline=10)
    elif method == "delete":
        response = urlfetch.fetch(url + "?" + query,
                                  method=urlfetch.DELETE,
                                  deadline=10)
    return response