class useAPI(object): def __init__(self, conf): self.app_key = conf.app_key self.app_secret = conf.app_secret self.callback_url = conf.callback_url self.token_file = conf.token_file self.token_file = os.path.join(os.path.dirname(__file__), self.token_file).replace('\\', '/') self.checked = self.check() def check(self): if os.path.isfile(self.token_file): try: token = pkl.load(open(self.token_file, 'r')) api = Client(self.app_key, self.app_secret, self.callback_url, token) try: api.get('statuses/user_timeline') self.api = api return True except: print("token maybe out of time!") except: print("The token file error") return False def token(self, CODE=''): client, url = self.getCODE() if self.checked == False: if CODE == '': webbrowser.open_new(url) try: # for python2.x CODE = raw_input("Please Input the Code: ").strip() except: # for python3.x CODE = input("Please Input the Code: ").strip() try: client.set_code(CODE) except: print("Maybe wrong CODE") return token = client.token pkl.dump(token, open(str(self.token_file), 'wb')) self.api = Client(self.app_key, self.app_secret, self.callback_url, token) self.checked = True def getCODE(self): client = Client(self.app_key, self.app_secret, self.callback_url) return client, client.authorize_url def getURL(self): client = Client(self.app_key, self.app_secret, self.callback_url) return client.authorize_url def get(self, url, count=200, **kwargs): kwargs['count'] = count if self.checked == False: self.token() return self.api.get(url, **kwargs) def post(self, url, **kwargs): if self.checked == False: self.token() return self.api.post(url, **kwargs)
class useAPI(object): def __init__(self, conf, rm=False): self.app_key = conf.app_key self.app_secret = conf.app_secret self.callback_url = conf.callback_url self.token_file = conf.token_file if not self.token_file: self.token_file = TMP_TOKEN if os.path.exists(self.token_file) and rm: os.remove(self.token_file) self.checked = self.check() def check(self): if os.path.isfile(self.token_file): try: token = pkl.load(open(self.token_file, 'r')) api = Client(self.app_key, self.app_secret, self.callback_url, token) try: api.get('statuses/user_timeline') self.api = api return True except: LOG.error(_("token maybe out of time!")) except: LOG.error(_("The token file error")) return False def token(self, CODE=''): client, url = self.getCODE() if not self.checked: if CODE == '': is_open = webbrowser.open_new(url) if not is_open: LOG.info(_("Please use the browser to open %(url)s"), {'url': url}) try: # for python2.x CODE = raw_input(_("Please Input the Code: ")).strip() except: # for python3.x CODE = input(_("Please Input the Code: ")).strip() try: client.set_code(CODE) except: LOG.error(_("Maybe wrong CODE")) return token = client.token pkl.dump(token, open(str(self.token_file), 'wb')) self.api = Client(self.app_key, self.app_secret, self.callback_url, token) self.checked = True def getCODE(self): client = Client(self.app_key, self.app_secret, self.callback_url) return client, client.authorize_url def getURL(self): client = Client(self.app_key, self.app_secret, self.callback_url) return client.authorize_url def get(self, url, count=200, **kwargs): kwargs['count'] = count if not self.checked: self.token() return self.api.get(url, **kwargs) def post(self, url, **kwargs): if not self.checked: self.token() return self.api.post(url, **kwargs)