def remove_account(self, username, password, filename): ''' Removes the account with the specified username and password from the system. If the username is not found or the password is incorrect for the given username, then the account is not removed. @param username : the desired username of the account to remove @param password : the password of the account to remove @return the resulting dictionary. {'result': True/False} If the result is true, then the account was removed. If the result is false, then the account was not removed. ''' can_remove = False user_data = FileAccess.read_user_table( lambda jsonfile: self._read_user_table(jsonfile), filename) if username in user_data and user_data[username][ 'password'] == password: can_remove = True if can_remove: user_data.pop(username, None) FileAccess.write_user_table( lambda user_data, jsonfile: self._write_user_table( user_data, jsonfile), filename, user_data) return {'result': can_remove}
def attempt_create_account(self, user_name, password, email, filename): ''' Attempts to create an account for the user. The data is retrieved from the database. @param user_name : string - The username for the login. @param password : string - The password for the login. @param email : string - The user's email. @return: The json response object. ''' user_data = FileAccess.read_user_table( lambda file: self._read_user_table(file), filename) username_already_exists = user_name in user_data username_doesnt_already_exist = not username_already_exists if username_doesnt_already_exist: user_data[user_name] = { 'password': password, 'email': email, 'steamid': 0 } FileAccess.write_user_table( lambda user_data, file: self._write_user_table(user_data, file), filename, user_data) details = 'Creation Successful.' if username_doesnt_already_exist else 'Creation Unsuccessful: Username Already Taken.' json_response = { "result": username_doesnt_already_exist, "details": details } return json_response
def perform_game_update(self, steamid, test_mode=False): game_filename = 'game_table_test.json' if test_mode else 'game_table.json' game_data = FileAccess.read_game_table( lambda game_jsonfile: self._read_data(game_jsonfile), game_filename) request = GameRequestAPI(steamid, self.api) result = request.get_info(test_mode) if result != None and str(steamid) in game_data: game_data[str(steamid)]['initialprice'] = result['initialprice'] game_data[str(steamid)]['actualprice'] = result['actualprice'] game_data[str(steamid)]['onsale'] = result['onsale'] FileAccess.write_game_table( lambda game_data, jsonfile: self._write_data(game_data, jsonfile), game_filename, game_data)
def fetch_games_on_watchlist(self, username, watchlist_path, game_table_path): ''' Fetches all games on the specified username's watchlist. The returned dictionary is of the format {'username': username, 'games_on_watchlist': [{'steamid': steamid, 'title': title, 'initialprice': initialprice, 'actualprice': actualprice, 'onsale': onsale, 'targetprice_criteria': targetprice_criteria, 'onsale_selected': onsale_selected, 'targetprice_selected': targetprice_selected}, ...]} @param username : the username of the account to get the watchlist @return: the dictionary in the format described above. ''' watchlist_info = [] watchlist_table = FileAccess.read_watchlist_table( lambda jsonfile: self._read_data(jsonfile), watchlist_path) for key in watchlist_table: if watchlist_table[key]['username'] == username: watchlist_info.append({ 'steamid': watchlist_table[key]['steamid'], 'targetprice_criteria': watchlist_table[key]['targetprice_criteria'], 'onsale_selected': watchlist_table[key]['onsale_selected'], 'targetprice_selected': watchlist_table[key]['targetprice_selected'] }) games = [] game_data = FileAccess.read_watchlist_table( lambda jsonfile: self._read_data(jsonfile), game_table_path) for info in watchlist_info: current_game = {} current_game['steamid'] = info['steamid'] current_game['title'] = game_data[str(info['steamid'])]['title'] current_game['initialprice'] = game_data[str( info['steamid'])]['initialprice'] current_game['actualprice'] = game_data[str( info['steamid'])]['actualprice'] current_game['onsale'] = game_data[str(info['steamid'])]['onsale'] current_game['targetprice_criteria'] = info['targetprice_criteria'] current_game['onsale_selected'] = info['onsale_selected'] current_game['targetprice_selected'] = info['targetprice_selected'] games.append(current_game) return {'username': username, 'games_on_watchlist': games}
def attempt_login(self, user_name, password, filename): ''' Fetches the watchlist data for the given username and password pair. The data is retrieved from the data stored in the database. @param user_name : string - The username for the login. @param password : string - The password for the login. @return: The json response object. ''' is_valid = False user_data = FileAccess.read_user_table( lambda user_json: self._read_user_table(user_json), filename) if user_name in user_data and user_data[user_name][ 'password'] == password: is_valid = True watchlist_game_fetch = WatchlistGameFetcher(user_name) results = watchlist_game_fetch.process_service( test_mode=(filename == 'user_table_test.json')) return { "result": is_valid, "watchlist": results['games_on_watchlist'] if is_valid else [] }
def send_emails(self, test_mode=False): ''' Sends emails for all notifications that meet watchlist criteria. @param test_mode : whether or not to run the service in test mode. ''' print("Starting to Send E-Mails") user_data = FileAccess.read_user_table( lambda f: self._user_data_read(f), 'user_table_test.json' if test_mode else 'user_table.json') for username in user_data: notification_service = Notification(username) notification_data = notification_service.process_service( test_mode)['notifications'] print(notification_data) for data in notification_data: print(data) formatter = EmailFormatter(data['steamid'], data['actualprice'], data['initialprice'], test_mode) formatted_email = formatter.format_for_email() sender = EmailSender(user_data[username]['email'], formatted_email) sender.send_email() print("Finished Sending E-Mails")
def attempt_addition(self, username, game_steamid, watchlist_path, game_table_path): ''' Attempts to make the addition for the game with the spcified steamid to the account with the specified username. There are a couple of cases in which the game will not be added. However, these cases can be removed if we enforce that they do not happen on the client side. For one, if the game that they are trying to add does not exist in the game_table, then the game is not added. For two, if they already have the game on their watchlist, then it is not added again. @param username : the username of the account to perform the watchlist addition. @param game_steamid : the id of the game to add. @return: the new watchlist for the user that contains the results after the addition has been processed. ''' does_game_exist = self._does_game_exist(game_steamid, game_table_path) not_already_on_watchlist = self._is_not_already_on_watchlist( username, game_steamid, watchlist_path) should_add = not_already_on_watchlist and does_game_exist if should_add: watchlist_data = FileAccess.read_watchlist_table( lambda file: self._read_data(file), watchlist_path) keys = map(lambda x: int(x), watchlist_data.keys()) watchlist_data[max(keys, default=0) + 1] = { 'steamid': game_steamid, 'username': username, 'targetprice_criteria': 0.0, 'onsale_selected': False, 'targetprice_selected': False } FileAccess.write_watchlist_table( lambda watchlist_data, file: self._write_data( watchlist_data, file), watchlist_path, watchlist_data) watchlist_game_fetcher = WatchlistGameFetcher(username) results = watchlist_game_fetcher.process_service( test_mode=(watchlist_path == 'watchlist_table_test.json')) addition_result = {'result': should_add} addition_result.update(results) return addition_result
def _is_not_already_on_watchlist(self, username, game_steamid, watchlist_path): not_already_on_watchlist = True watchlist_data = FileAccess.read_watchlist_table( lambda file: self._read_data(file), watchlist_path) for key in watchlist_data: if watchlist_data[key]['username'] == username: if watchlist_data[key]['steamid'] == game_steamid: not_already_on_watchlist = False return not_already_on_watchlist
def attempt_fetch_games(self, filename): ''' Fetches all games in the system. The data is pulled from the json files that do not end in _test.json in the test_data directory. @return: The json response object. ''' return FileAccess.read_game_table( lambda file: self._parse_games_file(file), filename)
def attempt_fetch_notifications(self, user_name, watchlist_filename, game_filename): ''' Attempts to fetch notifications for the specified user. This is the production implementation. Therefore, all data is read from the database. @param user_name : string - the user's username. @return: The json string to send back to the user. ''' id_info = [] watchlist_data = FileAccess.read_watchlist_table( lambda watchlist_file: self._read_data(watchlist_file), watchlist_filename) for key in watchlist_data: if watchlist_data[key]['username'] == user_name: id_info.append({ 'steamid': watchlist_data[key]['steamid'], 'onsale_selected': watchlist_data[key]['onsale_selected'], 'targetprice_selected': watchlist_data[key]['targetprice_selected'], 'targetprice_criteria': watchlist_data[key]['targetprice_criteria'] }) game_notifications = [] game_data = FileAccess.read_watchlist_table( lambda game_file: self._read_data(game_file), game_filename) for info in id_info: onsale = info['onsale_selected'] and game_data[str( info['steamid'])]['onsale'] below_criteria = info['targetprice_selected'] and game_data[str( info['steamid'])]['actualprice'] < info['targetprice_criteria'] if onsale or below_criteria: game = {} game['steamid'] = info['steamid'] game.update(game_data[str(info['steamid'])]) game_notifications.append(game) return {'notifications': game_notifications}
def make_watchlist_modification(self, user_name, steam_id, on_sale_selected, price_threshold, target_price_selected, filename): '''' Takes in username, steam id, whether or not the user wants to be notified when the game goes on sale, price threshold at which the user should be notified, and whether or not the price threshold should be acknowledged. The service will open the watchlist_table.json file, load it, and then search for the game related to the username and steam id. Then the watchlist will update the values of the table to be those passed in, and finally return the new watchlist table including whether or not the watchlist table was altered. @param user_name : string - the user's username. @param steam_id : int - the steam id of the game to change. @param on_sale_selected : boolean - whether or not the user should be notified when the game goes on sale. @param price_threshold : double - the price threshold at which the user should be notified @param target_price_selected : boolean - whether or not the user wants to be notified when the game is below the price threshold. @param filename : the filename of the table to be used @return the updated watchlist data including if the table was altered. ''' was_modified = False watchlist_table = FileAccess.read_watchlist_table(lambda watchlist_file: self._read_data(watchlist_file), filename) for key in watchlist_table: if watchlist_table[key]['username'] == user_name and watchlist_table[key]['steamid'] == steam_id: watchlist_table[key]['targetprice_criteria'] = price_threshold watchlist_table[key]['onsale_selected'] = on_sale_selected watchlist_table[key]['targetprice_selected'] = target_price_selected was_modified = True FileAccess.write_watchlist_table(lambda watchlist_table, write_watchlist_file: self._write_data(watchlist_table, write_watchlist_file), filename, watchlist_table) watchlist_fetcher = WatchlistGameFetcher(user_name) watchlist_data = watchlist_fetcher.process_service(test_mode = (filename == 'watchlist_table_test.json')) watchlist_data.update({'result' : was_modified}) return watchlist_data
def attempt_removal(self, username, game_steamid, filename): ''' Removes the game from the user's watchlist and commits the changes. Then, fetches the new watchlist data after the processed removal and returns it in dictionary format. @param username : the username of the account to remove the game @param game_steamid : the steamid of the game to remove @param filename : the filename of the table to be used @return the new watchlist data for the account. @see: server_requests.gamefetcher.WatchlistGameFetcher for the returned results. ''' key_to_remove = None watchlist_data = FileAccess.read_watchlist_table(lambda jsonfile: self._read_data(jsonfile), filename) for key in watchlist_data: if watchlist_data[key]['username'] == username and watchlist_data[key]['steamid'] == game_steamid: key_to_remove = key watchlist_data.pop(key_to_remove, None) FileAccess.write_watchlist_table(lambda watchlist_data, jsonfile: self._write_data(watchlist_data, jsonfile), filename, watchlist_data) watchlist_game_fetcher = WatchlistGameFetcher(username) return watchlist_game_fetcher.process_service(test_mode=(filename == 'watchlist_table_test.json'))
def __init__(self, steam_id, current_price, initial_price, test_mode): ''' Creates a new EmailFormatter @precondition none @postcondition self.steam_id == steam_id, self.current_price == current_price, self.initial_price == initial_price, self.steam_game_name == name pulled from game_table with the steam_id. ''' self.steam_id = steam_id self.steam_game_name = "game not found" game_table = FileAccess.read_game_table( lambda jsonfile: self._read_data(jsonfile), 'game_table_test.json' if test_mode else 'game_table.json') self.steam_game_name = game_table[str(steam_id)]['title'] self.current_price = current_price self.initial_price = initial_price
def perform_updates(self, test_mode=False): ''' Performs the updates. Reads in all data from the watchlist and game tables. If test_mode is true, then it is read from the _test.json files instead. For each game in the watchlist table, if it has not already been updated, then it gets the new information and updates it. @param test_mode : Whether or not to run the update in test mode. ''' watchlist_filename = 'watchlist_table_test.json' if test_mode else 'watchlist_table.json' updated_ids = [] watchlist_data = FileAccess.read_watchlist_table( lambda watchlist_jsonfile: self._read_data(watchlist_jsonfile), watchlist_filename) for key in watchlist_data: current_steamid = watchlist_data[key]['steamid'] if current_steamid in updated_ids: continue updated_ids.append(current_steamid) self.perform_game_update(current_steamid, test_mode) self.api.stop_timer()
def _does_game_exist(self, game_steamid, game_table_path): game_data = FileAccess.read_watchlist_table( lambda file: self._read_data(file), game_table_path) return str(game_steamid) in game_data