package app.updates; import app.entities.Stats; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.annotation.PostConstruct; import org.jobrunr.scheduling.JobScheduler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.time.Instant; @Component public class SocialUpdater { RestTemplate restTemplate; ObjectMapper objectMapper; Stats stats; JobScheduler jobScheduler; @Value("${backend.social.discord}") private String discord_url = ""; @Value("${backend.social.vk}") private String vk_url; @Autowired public SocialUpdater(Stats stats, JobScheduler jobScheduler) { restTemplate = new RestTemplate(); objectMapper = new ObjectMapper(); this.stats = stats; this.jobScheduler = jobScheduler; } @PostConstruct public void SetUpdater(){ if(!discord_url.isEmpty()) { jobScheduler.enqueue(() -> UpdateDiscordCount()); jobScheduler.scheduleRecurrently("*/5 * * * *", () -> UpdateDiscordCount()); jobScheduler.enqueue(() -> UpdateVKCount()); jobScheduler.scheduleRecurrently("*/5 * * * *", () -> UpdateVKCount()); } } public void UpdateDiscordCount() throws IOException { stats.setDiscord_users(objectMapper.readTree(new URL(discord_url)).get("approximate_member_count").asInt()); stats.getUpdates().merge("discord_count", Instant.now().getEpochSecond(), (x, y) -> y); } public void UpdateVKCount() { int count = 0; String response = restTemplate.getForEntity(vk_url, String.class).getBody(); int k_start = response.indexOf("

") + "

".length(); int k_end = response.indexOf("<", k_start); count += Integer.valueOf(response.substring(k_start, k_end)) * 1000; int s_start = response.indexOf("", k_end) + "".length(); int s_end = response.indexOf(" ", s_start); count += Integer.valueOf(response.substring(s_start, s_end)); stats.setVk_users(count); stats.getUpdates().merge("vk_count", Instant.now().getEpochSecond(), (x, y) -> y); } }