def handle(self, *args, **options): username = options["username"] if username: user = User.objects.get(username=username) else: user = None for file in options["file"]: print("Uploading %r" % (file)) metadata = { "build": 0, "match_start": now().isoformat(), } event = UploadEvent( upload_ip="127.0.0.1", metadata=json.dumps(metadata), ) event.file = file event.save() with open(file, "r") as f: event.file = File(f) event.save() event.process() self.stdout.write("%r: %s" % (event, event.get_absolute_url())) if event.game: event.game.user = user event.game.save() self.stdout.write("%r: %s" % (event.game, event.game.get_absolute_url())) self.stdout.write("Replay: %s" % (event.game.replay_xml.url))
def test_should_load_into_redshift(auth_token): shortid = shortuuid.uuid() n = timezone.now() upload_event = UploadEvent( descriptor_data="{}", file=f"uploads/%04d/%02d/%02d/%02d/%02d/%s.power.log" % (n.year, n.month, n.day, n.hour, n.minute, shortid), shortid=shortid, token_uuid=auth_token.key, user_agent="RandoTracker.com") upload_event.save() global_game = GlobalGame(game_type=BnetGameType.BGT_RANKED_STANDARD, format=FormatType.FT_STANDARD, match_start=n, match_end=n) mock_exporter = Mock() mock_exporter.configure_mock(is_valid_final_state=True) assert should_load_into_redshift(upload_event, {}, global_game, mock_exporter)
def test_should_load_into_redshift_false(auth_token): shortid = shortuuid.uuid() upload_event = UploadEvent( descriptor_data="{}", file=f"uploads/2018/11/20/17/44/{shortid}.power.log", shortid=shortid, token_uuid=auth_token.key, user_agent="RandoTracker.com") upload_event.save() global_game = GlobalGame(game_type=BnetGameType.BGT_RANKED_STANDARD, format=FormatType.FT_STANDARD, match_start=timezone.now(), match_end=timezone.now()) mock_exporter = Mock() mock_exporter.configure_mock(is_valid_final_state=True) assert not should_load_into_redshift(upload_event, {"reconnecting": True}, global_game, mock_exporter) mock_exporter.configure_mock(is_valid_final_state=False) assert not should_load_into_redshift(upload_event, {}, global_game, mock_exporter)
def test_twitch_vod(self): obj = UploadEvent() obj.save() serializer = UploadEventSerializer(obj, data=TEST_TWITCH_VOD_METADATA) assert serializer.is_valid() serializer.save() actual_metadata = json.loads(obj.metadata) for key, value in TEST_TWITCH_VOD_METADATA.items(): assert actual_metadata[key] == value
def create(self, data): request = self.context["request"] ret = UploadEvent( file=data.pop("file"), token=request.auth_token, api_key=request.api_key, type=data.pop("type"), upload_ip=get_client_ip(request), ) ret.metadata = json.dumps(data, cls=DjangoJSONEncoder) ret.save() return ret
def test_update_last_replay_upload_non_hdt(user, auth_token): upload_event = UploadEvent(token_uuid=auth_token.key, user_agent="RandoTracker.com") update_last_replay_upload(upload_event) user.refresh_from_db() assert user.last_replay_upload is None
def test_update_last_replay_upload(user, auth_token): upload_event = UploadEvent(token_uuid=auth_token.key, user_agent="HDT/1.7.0") update_last_replay_upload(upload_event) user.refresh_from_db() assert user.last_replay_upload is not None
def handle(self, *args, **options): for file in options["file"]: print("Uploading %r" % (file)) metadata = { "build": 0, "match_start": now().isoformat(), } event = UploadEvent( type=UploadEventType.POWER_LOG, upload_ip="127.0.0.1", metadata=json.dumps(metadata), ) event.file = file event.save() with open(file, "r") as f: event.file = File(f) event.save() event.process() self.stdout.write("%r: %s" % (event, event.get_absolute_url()))
def handle(self, *args, **options): username = options["username"] username_to_pick_from = options["pick_token"] raw_token = options["token"] if username: user = User.objects.get(username=username) if not user: raise Exception("User not found") self.stdout.write(" ".join(( "Warning: Will only attach to user after processing and not fire webhooks.", "Use --pick-token instead.", ))) token = None elif username_to_pick_from: user = User.objects.get(username=username_to_pick_from) # pick the user's first token token = user.auth_tokens.first() if token: self.stdout.write( "Picked auth token %s (owned by %s)" % (token, token.user) ) else: raise Exception("No auth token found") # should already be attached by token user = None elif raw_token: user = None token = AuthToken.objects.get(key=raw_token) if not token: raise Exception("Auth token not found") else: user = None token = None for file in options["file"]: print("Uploading %r" % (file)) metadata = { "build": 0, "match_start": now().isoformat(), } event = UploadEvent( upload_ip="127.0.0.1", metadata=json.dumps(metadata), token=token, ) event.file = file event.save() with open(file, "r") as f: event.file = File(f) event.save() event.process() self.stdout.write("%r: %s" % (event, event.get_absolute_url())) if event.game: if user: # only overwrite user if forced (via --username) event.game.user = user event.game.save() self.stdout.write("%r: %s" % (event.game, event.game.get_absolute_url())) self.stdout.write("Replay: %s" % (event.game.replay_xml.url))
def handle(self, *args, **options): username = options["username"] if username: user = User.objects.get(username=username) else: user = None for file in options["file"]: print("Uploading %r" % (file)) metadata = { "build": 0, "match_start": now().isoformat(), } event = UploadEvent( upload_ip="127.0.0.1", metadata=json.dumps(metadata), ) event.file = file event.save() with open(file, "r") as f: event.file = File(f) event.save() event.process() self.stdout.write("%r: %s" % (event, event.get_absolute_url())) if event.game: event.game.user = user event.game.save() self.stdout.write("%r: %s" % (event.game, event.game.get_absolute_url())) self.stdout.write("Replay: %s" % (event.game.replay_xml.url))
def handle(self, *args, **options): if options["force_attach"]: user = User.objects.get(username=options["force_attach"]) if not user: raise CommandError( f"User {repr(options['force_attach'])} not found") else: user = None if options["pick_token"]: user = User.objects.get(username=options["pick_token"]) if not user: raise CommandError( f"User {repr(options['pick_token'])} not found") token = user.auth_tokens.first() else: token = None for file in options["file"]: self.stdout.write(f"Uploading {repr(file)}") metadata = { "build": 0, "match_start": now().isoformat(), "game_type": enums.BnetGameType.BGT_RANKED_STANDARD, "format": enums.FormatType.FT_STANDARD, } event = UploadEvent( upload_ip="127.0.0.1", metadata=json.dumps(metadata), token_uuid=token.key if token else None, ) event.file = file event.save() with open(file, "r") as f: event.file = File(f) event.save() event.process() self.stdout.write("%r: %s" % (event, event.get_absolute_url())) if event.game: if user: # only overwrite user if forced (via --username) event.game.user = user event.game.save() self.stdout.write("%r: %s" % (event.game, event.game.get_absolute_url())) self.stdout.write("Replay: %s" % (event.game.replay_xml.url))
def test_create_dynamodb_game_replay(auth_token): upload_event = UploadEvent( id="1", shortid="ccSgiGQaenVzXzwGYbaUTPGrv", token_uuid=auth_token.key, ) meta = { "game_type": enums.BnetGameType.BGT_RANKED_STANDARD, "ladder_season": 42, "format": enums.FormatType.FT_STANDARD, "friendly_player": 1, "reconnecting": False, "scenario_id": 2, "start_time": datetime(year=2018, month=8, day=8, hour=19, minute=20, second=2, microsecond=606936), "end_time": datetime(year=2018, month=8, day=8, hour=19, minute=31, second=10), "player1": { "rank": 20, "stars": 30, "deck": [ "BOT_447", "BOT_447", "EX1_319", "EX1_319", "LOOT_014", "LOOT_014", "BOT_263", "BOT_263", "BOT_568", "CS2_065", "CS2_065", "EX1_596", "EX1_596", "BOT_443", "BOT_443", "LOOT_013", "LOOT_013", "BOT_224", "BOT_224", "BOT_226", "BOT_226", "ICC_466", "ICC_466", "ICC_075", "ICC_075", "EX1_310", "EX1_310", "BOT_521", "BOT_521", "ICC_831", ], "deck_id": 1337, "cardback": 136, }, "player2": { "rank": 19, "cardback": 138, }, } path = os.path.join(LOG_DATA_DIR, "hsreplaynet-tests", "replays", "whizbang_friendly.annotated.xml") with open(path, "r") as f: replay = HSReplayDocument.from_xml_file(f) packet_tree = replay.to_packet_tree()[0] entity_tree = EntityTreeExporter(packet_tree).export().game replay_xml = "foo.xml" replay = create_dynamodb_game_replay(upload_event, meta, entity_tree, replay_xml) assert replay assert replay.user_id == auth_token.user.id assert replay.match_start == 1533756002606 assert replay.match_end == 1533756670000 assert replay.short_id == upload_event.shortid assert replay.digest is None assert replay.game_type == enums.BnetGameType.BGT_RANKED_STANDARD assert replay.format_type == enums.FormatType.FT_STANDARD assert replay.game_type_match_start == "2:1533756002606" assert replay.ladder_season == 42 assert replay.brawl_season is None assert replay.scenario_id == 2 assert replay.num_turns == 31 assert replay.friendly_player_account_hilo == "144115193835963207_127487329" assert replay.friendly_player_battletag == "Masture#1176" assert replay.friendly_player_is_first assert replay.friendly_player_rank == 20 assert replay.friendly_player_rank_stars == 30 assert replay.friendly_player_legend_rank is None assert replay.friendly_player_wins is None assert replay.friendly_player_losses is None assert replay.friendly_player_class == enums.CardClass.WARLOCK assert replay.friendly_player_deck == \ "AAECAf0GApfTAo+CAw4w9wTCCPYIm8sC980C8dAC8tAC9PcC0/gCqvkCt/0Cw/0C+v4CAA==" assert replay.friendly_player_blizzard_deck_id == 1337 assert replay.friendly_player_cardback_id == 136 assert replay.friendly_player_final_state == enums.PlayState.WON assert replay.opponent_account_hilo == "144115193835963207_50318740" assert replay.opponent_battletag == "GinyuGamer#1677" assert not replay.opponent_is_ai assert replay.opponent_rank == 19 assert replay.opponent_legend_rank is None assert replay.opponent_class == enums.CardClass.PALADIN assert replay.opponent_hero == 671 assert replay.opponent_revealed_deck == \ "AAECAZ8FDYoGlgm5wQLjywKc4gKL5QKb8AKl9QKE/ALW/gKggAPMgQPeggMEiMcC/PwC4f4CkYADAA==" assert replay.opponent_predicted_deck is None assert replay.opponent_final_state == enums.PlayState.LOST assert replay.replay_xml == replay_xml assert not replay.disconnected assert not replay.reconnecting assert replay.hslog_version assert replay.visibility == auth_token.user.default_replay_visibility assert replay.views == 0
def test_replays_api(auth_token, client, mocker): mocker.patch("hsreplaynet.api.serializers.replays.classify_deck", return_value=1) upload_event = UploadEvent( id="1", shortid="ccSgiGQaenVzXzwGYbaUTPGrv", token_uuid=auth_token.key, ) path = os.path.join(LOG_DATA_DIR, "hsreplaynet-tests", "replays", "whizbang_friendly.annotated.xml") with open(path, "r") as f: replay = HSReplayDocument.from_xml_file(f) packet_tree = replay.to_packet_tree()[0] meta = { "game_type": enums.BnetGameType.BGT_RANKED_STANDARD, "ladder_season": 42, "format": enums.FormatType.FT_STANDARD, "friendly_player": 1, "reconnecting": False, "scenario_id": 2, "start_time": packet_tree.start_time, "end_time": packet_tree.end_time, "player1": { "rank": 20, "stars": 30, "deck": [ "BOT_447", "BOT_447", "EX1_319", "EX1_319", "LOOT_014", "LOOT_014", "BOT_263", "BOT_263", "BOT_568", "CS2_065", "CS2_065", "EX1_596", "EX1_596", "BOT_443", "BOT_443", "LOOT_013", "LOOT_013", "BOT_224", "BOT_224", "BOT_226", "BOT_226", "ICC_466", "ICC_466", "ICC_075", "ICC_075", "EX1_310", "EX1_310", "BOT_521", "BOT_521", "ICC_831", ], "deck_id": 1337, "cardback": 136, }, "player2": { "rank": 19, "cardback": 138, }, } entity_tree = EntityTreeExporter(packet_tree).export().game replay_xml = "foo.xml" replay = create_dynamodb_game_replay(upload_event, meta, entity_tree, replay_xml) replay.save() user_id = auth_token.user.id response = client.get("/api/v1/replays/?user_id=%d" % (user_id)) assert response.status_code == status.HTTP_200_OK payload = _parse_streaming_json(response.streaming_content)[0] assert payload["user_id"] == user_id assert payload["match_start"] == "2018-08-08T21:20:02.610000Z" assert payload["match_end"] == "2018-08-08T21:31:10.236000Z" assert payload["shortid"] == "ccSgiGQaenVzXzwGYbaUTPGrv" assert payload["game_type"] == enums.BnetGameType.BGT_RANKED_STANDARD assert payload["format_type"] == enums.FormatType.FT_STANDARD assert payload["friendly_player_account_hi"] == "144115193835963207" assert payload["friendly_player_account_lo"] == "127487329" assert payload["friendly_player_battletag"] == "Masture#1176" assert payload["friendly_player_rank"] == 20 assert payload["friendly_player_archetype_id"] == 1 assert payload["opponent_account_hi"] == "144115193835963207" assert payload["opponent_account_lo"] == "50318740" assert payload["opponent_battletag"] == "GinyuGamer#1677" assert payload["opponent_rank"] == 19 assert payload["opponent_archetype_id"] is None assert payload["replay_xml"] == "foo.xml" assert payload["disconnected"] is False assert payload["reconnecting"] is False assert payload["visibility"] == Visibility.Public assert payload["views"] == 0
def handle(self, *args, **options): if options["force_attach"]: user = User.objects.get(username=options["force_attach"]) if not user: raise CommandError(f"User {repr(options['force_attach'])} not found") else: user = None for file in options["file"]: self.stdout.write(f"Uploading {repr(file)}") metadata = { "build": 0, "match_start": now().isoformat(), } event = UploadEvent( upload_ip="127.0.0.1", metadata=json.dumps(metadata), ) event.file = file event.save() with open(file, "r") as f: event.file = File(f) event.save() event.process() self.stdout.write("%r: %s" % (event, event.get_absolute_url())) if event.game: if user: # only overwrite user if forced (via --username) event.game.user = user event.game.save() self.stdout.write("%r: %s" % (event.game, event.game.get_absolute_url())) self.stdout.write("Replay: %s" % (event.game.replay_xml.url))