def get(self): """ call this method to get a token:: token = GithubToken().get() what the get() does: 1) read from "~/.todo/token" 2) check if the token read is empty. (yes)-> 1) if empty, ask user for user&passwd to access api.github.com 2) fetch the token, set to this instance and store it. 3) return the token (a string) """ if self.is_empty: user = ask_input.text("Github user:"******"Password for %s:" % user) log.info("Authorize to github.com..") response = Github().authorize(user, password) if response.status_code == 201: # 201 created log.ok("Authorized success.") # get token from dict token = response.json()["token"] self.save(token) else: log.error("Authorization failed. %d" % response.status_code) return self.content
def get(self, answer=None): # add answer to figure which approach to go """ call this method to get a gist_id:: gist_id = GistId().get() what the get() dose: 1) read the gist_id from file "~/.todo/gist_id" 2) check if the gist_id if empty (yes)-> 1) if the gist_id is empty, ask user to input it or new a gist directly. 2) save the new gist_id 3) return the gist_id """ if self.is_empty: print "Tell me the gist's id you want to push to:" print " 1. New a gist right now." print " 2. Let me input a gist's id." if answer is None: answer = ask_input.text("Input your answer(1/2):") if answer == '2': self.save(ask_input.text("Gist id:")) elif answer == '1': # new a gist todo_content = TodoTxt().read() todo = parser.parse(todo_content) if not todo.name: name = "Todo" else: name = todo.name files = { name: { "content": todo_content } } resp = None github = Github() token = GithubToken().get() github.login(token) # need to login log.info("Create a new gist..") resp = github.create_gist(files=files, description="Todo") if resp.status_code == 201: dct = resp.json() html_url = dct["html_url"].encode("utf8") log.ok("Create success:%s ," "pushed at file '%s'" % (html_url, name)) self.save(dct["id"]) sys.exit() elif resp.status_code == 401: log.warning("Github access denied, empty the old token") GithubToken().save('') # empty the token! # and re create self.get(answer='1') else: log.error("Create gist failed. %d" % resp.status_code) else: # exit if else input log.error("Invalid answer.") return self.content