def fetch_ah_data(self) -> dict: # try each realm id in list of connected realms until we get one that doesn't error this_attempt = None self.log.info('###################### AUCTION ACCESS START ######################') self.log.info("TIME: " + gu.get_timestamp(human_readable=True)) for realm_id in self.realm_id_list: ah_url = self.build_url(url_type="auction", data=realm_id) this_attempt = requests.get(ah_url) this_attempt.close() self.log.info("Attempted call to: " + ah_url) if this_attempt.status_code not in _error_codes: # We hit a good one self.log.info("Realm ID " + str(realm_id) + " returned a valid code: " + str(this_attempt.status_code)) break else: self.log.info("Error code received: " + str(this_attempt.status_code)) if this_attempt.status_code in _error_codes: self.log.warning("While retrieving auction data, the following code was received:") self.log.warning(str(this_attempt.status_code)) data = {} # return an empty dict if we never accepted data other than error codes else: data = json.loads(this_attempt.text) self.log.info("TIME: " + gu.get_timestamp(human_readable=True)) self.log.info('####################### AUCTION ACCESS END #######################') return data
def check_connection(self, message: str) -> bool: if self.conn is None or self.cursor is None: self.log.warning("Attempted to {} without a connection at {}.".format( message, gu.get_timestamp(human_readable=True)) ) return False return True
def connect_to_db(self): try: time = "TIME: " + gu.get_timestamp() if not os.path.exists(_db_path): os.makedirs(_db_path) db = os.path.join(_db_path, _db_name) db = os.path.normpath(db) self.conn = sqlite3.connect(db) self.cursor = self.conn.cursor() self.log.info("###################### DATABASE ACCESS START ######################") self.log.info(time) except Exception as e: self.log.error("FAILED TO CONNECT TO DATABASE: " + db) self.log.error(time) self.log.error("Exception: " + str(e))
def fetch_item_data(self, item_id) -> dict: if not self.check_token_status("gather item names"): return {} else: item_url = self.build_url(url_type="item", data=item_id) self.log.info('###################### ITEM ACCESS START ######################') self.log.info("Reaching out to item api at: " + gu.get_timestamp(human_readable=True)) this_item = requests.get(item_url) this_item.close() self.log.info("Attempted call to: " + item_url) if this_item.status_code not in _error_codes: item_data = json.loads(this_item.text) item_name = item_data["name"] item_quality = item_data["quality"]["type"] self.log.info("Name collected: " + item_name) else: self.log.warning("Received error code: " + str(this_item.status_code)) item_name = "UNKNOWN" self.log.info('####################### ITEM ACCESS END #######################') useful_item_data = { "name": item_name, "quality": item_quality } return useful_item_data
def close_connection(self): if self.check_connection("close connection to " + _listings): self.log.info("TIME: {}".format(gu.get_timestamp())) self.log.info("####################### DATABASE ACCESS END #######################") self.conn.close()
####################################################################################################################### # # The following exists for demonstration purposes only. # The true main entry point for this program is located in __main__.py # Relies on a file named "my_config.json" - a sample is available in the sample_config directory. # More information on the expected config can be found in the README # ####################################################################################################################### if __name__ == "__main__": # Writes to the auction_sample_data directory found in the data directory # This is where the main for database_gateway.py checks for data # Recreates the directory if missing config_filepath = os.path.join(os.path.dirname(__file__), "data", "config") config_filename = os.path.join(config_filepath, "my_config.json") sample_data_folder = os.path.join(os.path.dirname(__file__), "data", "auction_sample_data") if not os.path.exists(sample_data_folder): os.makedirs(sample_data_folder) if not os.path.exists(config_filepath): os.makedirs(config_filepath) if os.path.isfile(config_filename): ag = APIGateway(config_file=config_filename) cleaned_data = ag.gather_clean_data() data_outfile = "sample." + gu.get_timestamp(human_readable=False) + ".json" data_filename = os.path.join(sample_data_folder, data_outfile) data_filename = os.path.normpath(data_filename) with open(data_filename, 'w') as wf: json.dump(cleaned_data, wf, indent=4, sort_keys=True)