93 lines
3.5 KiB
Java
93 lines
3.5 KiB
Java
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 org.springframework.stereotype.Service;
|
||
import ru.ulstu.grant.model.GrantDto;
|
||
import ru.ulstu.grant.page.KiasPage;
|
||
import ru.ulstu.user.service.UserService;
|
||
|
||
import java.text.ParseException;
|
||
import java.util.ArrayList;
|
||
import java.util.Arrays;
|
||
import java.util.Calendar;
|
||
import java.util.List;
|
||
|
||
@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;
|
||
|
||
public KiasService(UserService userService) {
|
||
this.userService = userService;
|
||
}
|
||
|
||
public List<GrantDto> getNewGrantsDto() throws ParseException {
|
||
WebDriver webDriver = getDriver();
|
||
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));
|
||
}
|
||
grants.forEach(grantDto -> grantDto.setLeaderId(leaderId));
|
||
webDriver.quit();
|
||
return grants;
|
||
}
|
||
|
||
public List<GrantDto> getKiasGrants(WebDriver webDriver) throws ParseException {
|
||
List<GrantDto> newGrants = new ArrayList<>();
|
||
KiasPage kiasPage = new KiasPage(webDriver);
|
||
do {
|
||
newGrants.addAll(getGrantsFromPage(kiasPage));
|
||
} while (kiasPage.goToNextPage()); //проверка существования следующей страницы с грантами
|
||
return newGrants;
|
||
}
|
||
|
||
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);
|
||
}
|
||
return grants;
|
||
}
|
||
|
||
private List<Integer> generateGrantYears() {
|
||
return Arrays.asList(Calendar.getInstance().get(Calendar.YEAR),
|
||
Calendar.getInstance().get(Calendar.YEAR) + 1);
|
||
}
|
||
|
||
private WebDriver getDriver() {
|
||
System.setProperty(DRIVER_TYPE, getDriverExecutablePath());
|
||
final ChromeOptions chromeOptions = new ChromeOptions();
|
||
chromeOptions.addArguments("--headless");
|
||
return new ChromeDriver(chromeOptions);
|
||
}
|
||
|
||
private String getDriverExecutablePath() {
|
||
return KiasService.class.getClassLoader().getResource(
|
||
String.format(DRIVER_LOCATION, getDriverExecutable(isWindows()))).getFile();
|
||
}
|
||
|
||
private String getDriverExecutable(boolean isWindows) {
|
||
return isWindows ? WINDOWS_DRIVER : LINUX_DRIVER;
|
||
}
|
||
|
||
private boolean isWindows() {
|
||
return System.getProperty("os.name").toLowerCase().contains("windows");
|
||
}
|
||
}
|