fix torgi.gov infinite failing
This commit is contained in:
@@ -47,7 +47,7 @@ public class DownloadHeadlessController {
|
||||
private void addNewToDiff() {
|
||||
logger.error("startProcessing");
|
||||
DownloadHeadlessService downloadHeadlessService = new DownloadHeadlessService();
|
||||
Map<String, Lot> currentContent = downloadHeadlessService.downloadExcel();
|
||||
Map<String, Lot> currentContent = downloadHeadlessService.downloadExcel(0);
|
||||
List<TorgiEvent> newEvents = new ArrayList<>();
|
||||
|
||||
for (String key : currentContent.keySet()) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.pledge.torgi.model.TorgiEvent;
|
||||
import com.pledge.torgi.repositories.TorgiEventRepository;
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
@@ -38,7 +39,8 @@ public class DownloadHeadlessService {
|
||||
Pattern cadastralPattern = Pattern.compile("\\d{1,2}:\\d{1,2}:\\d{1,7}:\\d{1,9}");
|
||||
Pattern vinPattern = Pattern.compile("\\b[A-HJ-NPR-Z0-9-]{12,17}\\b");
|
||||
|
||||
public Map<String, Lot> downloadExcel() {
|
||||
@SneakyThrows
|
||||
public Map<String, Lot> downloadExcel(int retryCount) {
|
||||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(EXCEL_LINK).openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
@@ -64,12 +66,17 @@ public class DownloadHeadlessService {
|
||||
return data;
|
||||
} else {
|
||||
logger.error("Error in line 99 ");
|
||||
return downloadExcel();
|
||||
Thread.sleep(1000);
|
||||
return downloadExcel(retryCount + 1);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Error in line 103 " + e);
|
||||
if (retryCount > 10) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
return downloadExcel();
|
||||
Thread.sleep(1000);
|
||||
return downloadExcel(retryCount + 1);
|
||||
}
|
||||
|
||||
private Lot extractDataFromRow(Row row, String cadastral) {
|
||||
|
||||
@@ -5,11 +5,13 @@ import com.pledge.torgi.model.Emails;
|
||||
import com.pledge.torgi.repositories.EmailsRepository;
|
||||
import com.pledge.torgi.utils.StartParams;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class EmailService {
|
||||
@@ -18,16 +20,21 @@ public class EmailService {
|
||||
private final EmailsRepository emailsRepository;
|
||||
|
||||
public void processEmails(StartParams params) {
|
||||
log.error("Start email processing");
|
||||
List<Emails> alreadyProcessed = emailsRepository.findTop10ByOrderByIdDesc();
|
||||
List<EmailData> emailData = new ReceiveEmailService("imap.gmail.com", 993, params.getFrom(), params.getToken(), params.getSpecialEmail())
|
||||
.downloadEmails(alreadyProcessed);
|
||||
int sendLetters = 0;
|
||||
log.error("Extracted letters = {}", emailData.size());
|
||||
for (EmailData data : emailData) {
|
||||
if (alreadyProcessed(data.getEmailId())) {
|
||||
continue;
|
||||
}
|
||||
fileService.processSync(data.getFile(), data.getEmails());
|
||||
saveProcessedEmail(data);
|
||||
sendLetters++;
|
||||
}
|
||||
log.error("End email processing, send letters = {}", sendLetters);
|
||||
}
|
||||
|
||||
private boolean alreadyProcessed(String emailId) {
|
||||
|
||||
@@ -49,19 +49,22 @@ public class ReceiveEmailService {
|
||||
Message[] messages = folderInbox.getMessages(messageCount - 50, messageCount);
|
||||
|
||||
for (int i = 0; i < messages.length; i++) {
|
||||
Message msg = messages[i];
|
||||
Address[] toList = msg.getRecipients(Message.RecipientType.TO);
|
||||
boolean specialAddress = Arrays.stream(toList).anyMatch(addr -> ((InternetAddress) addr).getAddress().equals(specialEmail));
|
||||
if (!specialAddress) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String messageID = ((IMAPMessage) msg).getMessageID();
|
||||
if (alreadyProcessed.stream().anyMatch(processed -> processed.getMailId().equals(messageID))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
Message msg = messages[i];
|
||||
Address[] toList = msg.getRecipients(Message.RecipientType.TO);
|
||||
if (toList == null) {
|
||||
continue;
|
||||
}
|
||||
boolean specialAddress = Arrays.stream(toList).anyMatch(addr -> ((InternetAddress) addr).getAddress().equals(specialEmail));
|
||||
if (!specialAddress) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String messageID = ((IMAPMessage) msg).getMessageID();
|
||||
if (alreadyProcessed.stream().anyMatch(processed -> processed.getMailId().equals(messageID))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
MimeMultipart content = (MimeMultipart) msg.getDataHandler().getContent();
|
||||
if (content.getCount() == 1 || !Part.ATTACHMENT.equalsIgnoreCase(content.getBodyPart(1).getDisposition())) {
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user