예제 #1
0
 def store_token(self, data):
     Core.debug("storing OAuth token for %r", self.domain)
     self._store_token_libsecret(data)
     try:
         self._store_token_file(data)
     except Exception as e:
         Core.warn("could not write %r: %r", self.token_path, e)
예제 #2
0
    def __init__(self):
        self.ua = requests.Session()
        self.ua.mount("http://", requests.adapters.HTTPAdapter(max_retries=3))
        self.ua.mount("https://", requests.adapters.HTTPAdapter(max_retries=3))

        self.api = pixivpy3.PixivAPI()
        if not hasattr(self.api, "client_secret"):
            Core.warn("this pixivpy3.PixivAPI version does not allow overridding client_secret; OAuth won't work properly")
        self.api.client_id = "MOBrBDS8blbauoSck0ZfDbtuzpyT"
        self.api.client_secret = "lsACyCD94FhDUtGTXi3QzcFE2uU1hqtDaKeqrdwj"
        self.api.requests = self.ua
예제 #3
0
 def _store_token(self, token):
     data = {
         "access_token": token.response.access_token,
         "refresh_token": token.response.refresh_token,
         "expires_at": int(time.time() + token.response.expires_in),
         "user_id": token.response.user.id,
     }
     Core.debug("storing OAuth tokens")
     nullroute.sec.store_libsecret("Pixiv OAuth token",
                                   json.dumps(data),
                                   {"xdg:schema": self.TOKEN_SCHEMA,
                                    "domain": "pixiv.net",
                                    "userid": token.response.user.id,
                                    "username": token.response.user.account})
     try:
         with open(self.TOKEN_PATH, "w") as fh:
             json.dump(data, fh)
         return True
     except Exception as e:
         Core.warn("could not write %r: %r", self.TOKEN_PATH, e)
         return False
예제 #4
0
파일: pixiv.py 프로젝트: shaneschulte/code
    def _authenticate(self):
        if self.api.user_id:
            Core.warn("BUG: _authenticate() called twice")
            return True

        data = self._load_token()
        if data:
            Core.trace("loaded token: %r", data)
            if os.environ.get("FORCE_TOKEN_REFRESH"):
                token_valid = False
            else:
                token_valid = data["expires_at"] > time.time()

            if token_valid:
                Core.debug("access token within expiry time, using as-is")
                self.api.user_id = data["user_id"]
                self.api.access_token = data["access_token"]
                self.api.refresh_token = data["refresh_token"]
                return True
            else:
                Core.debug("access token has expired, renewing")
                try:
                    token = self.api.auth(refresh_token=data["refresh_token"])
                    Core.trace("retrieved token: %r", token)
                except Exception as e:
                    Core.warn("could not refresh access token: %r", e)
                    #self._forget_token()
                else:
                    self._store_token(token)
                    return True

        data = self._load_creds()
        if data:
            Core.info("logging in to Pixiv as %r", data["login"])
            try:
                token = self.api.auth(username=data["login"],
                                      password=data["password"])
            except Exception as e:
                Core.warn("could not log in using username & password: %r", e)
            else:
                self._store_token(token)
                return True

        Core.die("could not log in to Pixiv (no credentials)")
        return False