Feature/parsing-service: save

This commit is contained in:
danil.markov 2024-11-13 13:59:52 +04:00
parent c4bb7a5ffa
commit 59c41a4912
5 changed files with 22 additions and 3157 deletions

View File

@ -31,7 +31,7 @@ ext {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.liquibase:liquibase-core' // implementation 'org.liquibase:liquibase-core'
implementation 'org.springframework.kafka:spring-kafka' implementation 'org.springframework.kafka:spring-kafka'
implementation "org.jsoup:jsoup:${jsoupVesion}" implementation "org.jsoup:jsoup:${jsoupVesion}"
implementation "org.seleniumhq.selenium:selenium-java:${seleniumVersion}" implementation "org.seleniumhq.selenium:selenium-java:${seleniumVersion}"

View File

@ -1,9 +1,14 @@
package ru.pricepulse.parsingservice.config; package ru.pricepulse.parsingservice.config;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -14,19 +19,26 @@ import static ru.pricepulse.parsingservice.wildberries_parser.proxy.ProxyChecker
@Component @Component
public class ProxyProvider { public class ProxyProvider {
private List<String> workingProxies; private List<String> workingProxies;
private static final String PROXY_FILE_PATH = "/home/forever/УлГТУ/Платформы/parsing-service/src/main/resources/proxy.txt";
private static final String WORKING_PROXY_FILE_PATH = "/home/forever/УлГТУ/Платформы/parsing-service/src/main/resources/ok-proxy.txt";
private final AtomicInteger currentProxyIndex = new AtomicInteger(0); private final AtomicInteger currentProxyIndex = new AtomicInteger(0);
private final ResourceLoader resourceLoader;
public ProxyProvider(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
@PostConstruct @PostConstruct
public void init() { public void init() throws IOException {
List<String> proxies = readProxiesFromFile(PROXY_FILE_PATH); Resource proxy = resourceLoader.getResource("classpath:proxy.txt");
Resource okProxy = resourceLoader.getResource("classpath:ok-proxy.txt");
List<String> proxies = Files.readAllLines(Path.of(proxy.getURI()));
System.out.println("Начата проверка проксей"); System.out.println("Начата проверка проксей");
workingProxies = checkProxies(proxies); workingProxies = checkProxies(proxies);
System.out.println("Закончена проверка проксей"); System.out.println("Закончена проверка проксей");
saveProxiesToFile(workingProxies, WORKING_PROXY_FILE_PATH);
saveProxiesToFile(workingProxies, Path.of(okProxy.getURI()));
if (workingProxies.isEmpty()) { if (workingProxies.isEmpty()) {
throw new RuntimeException("Нет доступных рабочих прокси."); throw new RuntimeException("Нет доступных рабочих прокси.");

View File

@ -8,6 +8,7 @@ import java.net.InetSocketAddress;
import java.net.Proxy; import java.net.Proxy;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -101,8 +102,8 @@ public class ProxyChecker {
} }
} }
public static void saveProxiesToFile(List<String> proxies, String filePath) { public static void saveProxiesToFile(List<String> proxies, Path filePath) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) { try (BufferedWriter writer = Files.newBufferedWriter(filePath)) {
for (String proxy : proxies) { for (String proxy : proxies) {
writer.write(proxy); writer.write(proxy);
writer.newLine(); writer.newLine();

View File

@ -17,7 +17,7 @@ spring:
url: jdbc:clickhouse://${CLICKHOUSE_JDBC_URL} url: jdbc:clickhouse://${CLICKHOUSE_JDBC_URL}
username: ${CLICKHOUSE_JDBC_USERNAME} username: ${CLICKHOUSE_JDBC_USERNAME}
password: ${CLICKHOUSE_JDBC_PASSWORD} password: ${CLICKHOUSE_JDBC_PASSWORD}
liquibase: liquibase:
change-log: classpath:/db/changelog/master.yml change-log: classpath:/db/changelog/master.yml
marketplace: marketplace:

File diff suppressed because it is too large Load Diff