|
|
@ -4,15 +4,22 @@ import app.entities.other.SteamID; |
|
|
|
import app.entities.steam.SteamData; |
|
|
|
import app.utils.SteamIDConverter; |
|
|
|
import app.utils.SteamUrlConverter; |
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException; |
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.cache.annotation.CacheEvict; |
|
|
|
import org.springframework.cache.annotation.Cacheable; |
|
|
|
import org.springframework.cache.annotation.Caching; |
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
import org.springframework.scheduling.annotation.Scheduled; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
@Service |
|
|
|
public class SteamWebApi { |
|
|
|
private RestTemplate restTemplate; |
|
|
@ -25,17 +32,30 @@ public class SteamWebApi { |
|
|
|
restTemplate = new RestTemplate(); |
|
|
|
} |
|
|
|
|
|
|
|
public SteamData getSteamData(long steam64){ |
|
|
|
String url = String.format("https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s", webapi_key, steam64); |
|
|
|
@Scheduled(cron = "0 */30 * * * *") |
|
|
|
@Caching(evict = { |
|
|
|
@CacheEvict(value = "steam_data", allEntries = true), |
|
|
|
@CacheEvict(value = "steam_search_data", allEntries = true) |
|
|
|
}) |
|
|
|
public void clearCache() {} |
|
|
|
|
|
|
|
public SteamData getSteamData(long steam64) { |
|
|
|
try { |
|
|
|
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); |
|
|
|
JsonNode player = (new ObjectMapper()).readTree(response.getBody()).get("response").get("players").get(0); |
|
|
|
return new SteamData(player); |
|
|
|
} catch (Exception err){ |
|
|
|
return this.getSteamDataCaching(steam64); |
|
|
|
} catch (Exception e) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Cacheable(value = "steam_data", key = "#steam64") |
|
|
|
public SteamData getSteamDataCaching(long steam64) throws Exception { |
|
|
|
String url = String.format("https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s", webapi_key, steam64); |
|
|
|
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); |
|
|
|
JsonNode player = (new ObjectMapper()).readTree(response.getBody()).get("response").get("players").get(0); |
|
|
|
return new SteamData(player); |
|
|
|
} |
|
|
|
|
|
|
|
@Cacheable(value = "steam_search_data", key = "#community_url") |
|
|
|
public SteamID getSteamID(String community_url) { |
|
|
|
return SteamUrlConverter.getSteamID(community_url, restTemplate); |
|
|
|
} |
|
|
|