def jwt_init(username, password): """ initialize through the user name and password, write jwt to the local configuration file, the expiration time is about 12 hours, so it is recommended to initialize through the api key. :param username: str, login zoomeye account :param password: str, login zoomeye account password :return: """ file.check_exist(zoomeye_dir) try: zoom = ZoomEye(username=username, password=password) access_token = zoom.login() except Exception: return jwt_file = zoomeye_dir + "/jwt" if access_token: # display the remaining resources of the current account user_data = zoom.resources_info() show.printf("Role: {}".format(user_data["plan"])) show.printf("Quota: {}".format(user_data["resources"].get("search"))) with open(jwt_file, 'w') as f: f.write(access_token) show.printf("successfully initialized", color="green") # change the permission of the configuration file to read-only os.chmod(jwt_file, 0o600) else: show.printf("failed initialized!", color="red")
def key_init(key): """ initialize through the api key, write the api key to the local configuration file, theoretically it will never expire unless you remake the api key :param key: user input API key :return: """ file.check_exist(zoomeye_dir) key = key.strip() try: zoom = ZoomEye(api_key=key) except Exception: return # api key save path key_file = zoomeye_dir + "/apikey" # display the remaining resources of the current account user_data = zoom.resources_info() show.printf("Role: {}".format(user_data["plan"])) show.printf("Quota: {}".format(user_data["resources"].get("search"))) # save api key with open(key_file, 'w') as f: f.write(key) show.printf("successfully initialized", color="green") # change the permission of the configuration file to read-only os.chmod(key_file, 0o600)
def get_data(self): """ get user level and IP historical data """ normal_user = ['user', 'developer'] api_key, access_token = file.get_auth_key() zm = ZoomEye(api_key=api_key, access_token=access_token) role = zm.resources_info() # permission restrictions if role["plan"] in normal_user: show.printf( "this function is only open to advanced users and VIP users.", color='red') exit(0) # the user chooses to force data from the API if self.force: history_data = zm.history_ip(self.ip) else: # from local cache get data history_data_str = self.get_data_from_cache() # local cache not exists from API get data if history_data_str is None: history_data = zm.history_ip(self.ip) else: history_data = json.loads(history_data_str) # cache data self.cache_data(history_data) return history_data
def info(args): """ used to print the current identity of the user and the remaining data quota for the month :param args: :return: """ api_key, access_token = file.get_auth_key() zm = ZoomEye(api_key=api_key, access_token=access_token) # get user information user_data = zm.resources_info() if user_data: # show in the terminal show.printf("Role: {}".format(user_data["plan"])) show.printf("Quota: {}".format(user_data["resources"].get("search")))