|
1 | 1 | package ru.tinkoff.edu.java.scrapper.schedule; |
2 | 2 |
|
| 3 | +import org.springframework.jdbc.core.JdbcTemplate; |
3 | 4 | import org.springframework.scheduling.annotation.Scheduled; |
4 | 5 | import org.springframework.stereotype.Service; |
| 6 | +import org.springframework.web.reactive.function.client.WebClientResponseException; |
| 7 | +import ru.tinkoff.edu.java.linkparser.LinkParser; |
| 8 | +import ru.tinkoff.edu.java.scrapper.client.ClientConfiguration; |
| 9 | +import ru.tinkoff.edu.java.scrapper.jdbc.JdbcLinkService; |
| 10 | +import ru.tinkoff.edu.java.scrapper.jdbc.operations.LinkOperations; |
5 | 11 |
|
| 12 | +import java.lang.ref.Cleaner; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.LinkedHashSet; |
| 15 | +import java.util.Set; |
6 | 16 | import java.util.logging.Level; |
7 | 17 | import java.util.logging.Logger; |
8 | 18 |
|
9 | 19 | @Service |
10 | | -public class LinkUpdaterScheduler { |
| 20 | +public class LinkUpdaterScheduler implements LinkOperations { |
11 | 21 | private final static Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); |
12 | 22 |
|
| 23 | + private final JdbcTemplate jdbcTemplate; |
| 24 | + ArrayList<String> link_list = new ArrayList<>(); |
| 25 | + static int ix = 0; |
| 26 | + ClientConfiguration client = new ClientConfiguration(); |
| 27 | + LinkParser pars = new LinkParser(); |
| 28 | + boolean git_link_is_activity; |
| 29 | + boolean stack_link_is_activity; |
| 30 | + |
| 31 | + public LinkUpdaterScheduler(JdbcTemplate jdbcTemplate) { |
| 32 | + this.jdbcTemplate = jdbcTemplate; |
| 33 | + } |
| 34 | + |
13 | 35 | @Scheduled(fixedDelayString = "${app.scheduler.interval}") |
14 | 36 | public void update() { |
15 | | - LOGGER.log(Level.INFO, "Info scrapper called"); |
| 37 | + int i; |
| 38 | + for (i = 0; i < i_getAllIds(jdbcTemplate).size(); i++){ |
| 39 | + if (!link_list.contains(i_getAllIds(jdbcTemplate).get(i).url())) { |
| 40 | + link_list.add(i_getAllIds(jdbcTemplate).get(i).url()); |
| 41 | + } |
| 42 | + } |
| 43 | + try { |
| 44 | + client.gitHubClient(pars.getLink(link_list.get(ix))); |
| 45 | + git_link_is_activity = true; |
| 46 | + } catch (WebClientResponseException e) { |
| 47 | + git_link_is_activity = false; |
| 48 | + } |
| 49 | + try { |
| 50 | + client.stackOverflowClient(pars.getLink(link_list.get(ix))); |
| 51 | + stack_link_is_activity = true; |
| 52 | + } catch (WebClientResponseException ignored) { |
| 53 | + stack_link_is_activity = false; |
| 54 | + } |
| 55 | + if (!(git_link_is_activity || stack_link_is_activity)) { |
| 56 | + String link = "Ссылка %s устарела"; |
| 57 | + link = link.formatted(i_getAllIds(jdbcTemplate).get(ix).url()); |
| 58 | + LOGGER.log(Level.INFO, link); |
| 59 | + } |
| 60 | + ix++; |
| 61 | + if (ix == link_list.size()) ix = 0; |
16 | 62 | } |
17 | 63 | } |
0 commit comments