class GamePull(object): ''' This class handles pulling in all of the steam games and putting them into the dataset. ''' def __init__(self): ''' Constructs the new GamePull object. Since the GamePull is intended to be run on its own, it has its own apihandler. The limiting functionality is redundant here because the call is only ever made once; however, the handler is still used so as to not repeat code. ''' self.api = APIHandler(timer_reset_seconds=300, limit=175) def pull_games(self, test_mode=False): ''' Pulls the game information. If test_mode is True, then two test games are returned to simulate what would be returned from the api. If test_mode is False, then the actual api is called with the real results from Steam. @param test_mode : whether or not to pull games in test mode @return the steam games. ''' service = _FakeGamePullService() if test_mode else _GamePullService(self.api) return service.make_pull() def cleanup(self): self.api.stop_timer()
def test_attempt_get_info_without_price_overview(self, mock): handler = APIHandler(0, 1) service = _GameRequestService(handler) mock.return_value = {'was_successful': True, 'json': {'4': {'success': True, 'data': {'notprice_overview': {'final': 3999, 'initial': 2999, 'discount_percent': 5}}}}} result = service.attempt_get_info(4); handler.stop_timer() self.assertEqual(result, None)
class WatchlistUpdater(object): ''' The watchlist updater looks at all games on all watchlists - meaning all stored user's watchlists. Then, for each game not already updated, fetches new updated information and stores that information for that game in the game table. ''' def __init__(self): ''' Constructs the updater with its own api handler. This is to ensure that the api call limit is not exceeded. ''' self.api = APIHandler(timer_reset_seconds=300, limit=175) 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 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 _read_data(self, file): return json.load(file) def _write_data(self, data, file): json.dump(data, file)
def test_attempt_get_info_with_valid_data(self, mock): handler = APIHandler(0, 1) service = _GameRequestService(handler) mock.return_value = {'was_successful': True, 'json': {'4': {'success': True, 'data': {'price_overview': {'final': 3999, 'initial': 2999, 'discount_percent': 5}}}}} result = service.attempt_get_info(4); handler.stop_timer() self.assertEqual(result, {'steamid': 4, 'initialprice': 2999 / 100, 'actualprice': 3999 / 100, 'onsale': 50 > 0})
def test_does_nothing_if_limit_met(self): handler = APIHandler(2934932, 0) result = handler.make_request(None) self.assertFalse(result['was_successful']) handler.stop_timer()