|
|
|
@ -1,35 +1,29 @@
|
|
|
|
|
package ru.ulstu.grant.service;
|
|
|
|
|
|
|
|
|
|
import org.openqa.selenium.WebDriver;
|
|
|
|
|
import org.openqa.selenium.WebElement;
|
|
|
|
|
import org.openqa.selenium.chrome.ChromeDriver;
|
|
|
|
|
import org.openqa.selenium.chrome.ChromeOptions;
|
|
|
|
|
import com.gargoylesoftware.htmlunit.WebClient;
|
|
|
|
|
import com.gargoylesoftware.htmlunit.html.DomElement;
|
|
|
|
|
import com.gargoylesoftware.htmlunit.html.DomNode;
|
|
|
|
|
import com.gargoylesoftware.htmlunit.html.HtmlElement;
|
|
|
|
|
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import ru.ulstu.configuration.ApplicationProperties;
|
|
|
|
|
import ru.ulstu.grant.model.GrantDto;
|
|
|
|
|
import ru.ulstu.grant.page.KiasPage;
|
|
|
|
|
import ru.ulstu.user.service.UserService;
|
|
|
|
|
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import static org.apache.commons.lang3.StringUtils.isEmpty;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class KiasService {
|
|
|
|
|
private final static String BASE_URL = "https://www.rfbr.ru/rffi/ru/contest_search?CONTEST_STATUS_ID=%s&CONTEST_TYPE=%s&CONTEST_YEAR=%s";
|
|
|
|
|
private final static String CONTEST_STATUS_ID = "1";
|
|
|
|
|
private final static String CONTEST_TYPE = "-1";
|
|
|
|
|
|
|
|
|
|
private final static String DRIVER_LOCATION = "drivers/%s";
|
|
|
|
|
private final static String WINDOWS_DRIVER = "chromedriver.exe";
|
|
|
|
|
private final static String LINUX_DRIVER = "chromedriver";
|
|
|
|
|
private final static String DRIVER_TYPE = "webdriver.chrome.driver";
|
|
|
|
|
|
|
|
|
|
private final UserService userService;
|
|
|
|
|
private final ApplicationProperties applicationProperties;
|
|
|
|
|
|
|
|
|
@ -39,22 +33,22 @@ public class KiasService {
|
|
|
|
|
this.applicationProperties = applicationProperties;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<GrantDto> getNewGrantsDto() throws ParseException {
|
|
|
|
|
WebDriver webDriver = getDriver();
|
|
|
|
|
public List<GrantDto> getNewGrantsDto() throws ParseException, IOException {
|
|
|
|
|
Integer leaderId = userService.findOneByLoginIgnoreCase("admin").getId();
|
|
|
|
|
List<GrantDto> grants = new ArrayList<>();
|
|
|
|
|
for (Integer year : generateGrantYears()) {
|
|
|
|
|
webDriver.get(String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, year));
|
|
|
|
|
grants.addAll(getKiasGrants(webDriver));
|
|
|
|
|
try (final WebClient webClient = new WebClient()) {
|
|
|
|
|
for (Integer year : generateGrantYears()) {
|
|
|
|
|
final HtmlPage page = webClient.getPage(String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, year));
|
|
|
|
|
grants.addAll(getKiasGrants(page));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
grants.forEach(grantDto -> grantDto.setLeaderId(leaderId));
|
|
|
|
|
webDriver.quit();
|
|
|
|
|
return grants;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<GrantDto> getKiasGrants(WebDriver webDriver) throws ParseException {
|
|
|
|
|
public List<GrantDto> getKiasGrants(HtmlPage page) throws ParseException {
|
|
|
|
|
List<GrantDto> newGrants = new ArrayList<>();
|
|
|
|
|
KiasPage kiasPage = new KiasPage(webDriver);
|
|
|
|
|
KiasPage kiasPage = new KiasPage(page);
|
|
|
|
|
do {
|
|
|
|
|
newGrants.addAll(getGrantsFromPage(kiasPage));
|
|
|
|
|
} while (kiasPage.goToNextPage()); //проверка существования следующей страницы с грантами
|
|
|
|
@ -63,11 +57,13 @@ public class KiasService {
|
|
|
|
|
|
|
|
|
|
private List<GrantDto> getGrantsFromPage(KiasPage kiasPage) throws ParseException {
|
|
|
|
|
List<GrantDto> grants = new ArrayList<>();
|
|
|
|
|
for (WebElement grantElement : kiasPage.getPageOfGrants()) {
|
|
|
|
|
GrantDto grantDto = new GrantDto(
|
|
|
|
|
kiasPage.getGrantTitle(grantElement),
|
|
|
|
|
kiasPage.parseDeadLineDate(grantElement));
|
|
|
|
|
grants.add(grantDto);
|
|
|
|
|
for (DomNode grantElement : kiasPage.getPageOfGrants()) {
|
|
|
|
|
if (kiasPage.isTrGrantLine(grantElement)) {
|
|
|
|
|
GrantDto grantDto = new GrantDto(
|
|
|
|
|
kiasPage.getGrantTitle(grantElement),
|
|
|
|
|
kiasPage.parseDeadLineDate(grantElement));
|
|
|
|
|
grants.add(grantDto);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return grants;
|
|
|
|
|
}
|
|
|
|
@ -76,28 +72,4 @@ public class KiasService {
|
|
|
|
|
return Arrays.asList(Calendar.getInstance().get(Calendar.YEAR),
|
|
|
|
|
Calendar.getInstance().get(Calendar.YEAR) + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private WebDriver getDriver() {
|
|
|
|
|
System.setProperty(DRIVER_TYPE, getDriverExecutablePath());
|
|
|
|
|
System.out.println("drive search path: " + getDriverExecutablePath());
|
|
|
|
|
final ChromeOptions chromeOptions = new ChromeOptions();
|
|
|
|
|
chromeOptions.setBinary("/usr/bin/google-chrome");
|
|
|
|
|
chromeOptions.addArguments("--headless");
|
|
|
|
|
return new ChromeDriver(chromeOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getDriverExecutablePath() {
|
|
|
|
|
return isEmpty(applicationProperties.getDriverPath())
|
|
|
|
|
? KiasService.class.getClassLoader()
|
|
|
|
|
.getResource(String.format(DRIVER_LOCATION, getDriverExecutable(isWindows()))).getFile()
|
|
|
|
|
: Paths.get(applicationProperties.getDriverPath(), getDriverExecutable(isWindows())).toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getDriverExecutable(boolean isWindows) {
|
|
|
|
|
return isWindows ? WINDOWS_DRIVER : LINUX_DRIVER;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isWindows() {
|
|
|
|
|
return System.getProperty("os.name").toLowerCase().contains("windows");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|