HouseSearchApplication.java (2444B)
1 package xyz.kebigon.housesearch; 2 3 import java.io.IOException; 4 import java.util.Collection; 5 import java.util.stream.Collectors; 6 7 import org.apache.commons.mail.EmailException; 8 import org.springframework.util.StringUtils; 9 10 import lombok.extern.slf4j.Slf4j; 11 import xyz.kebigon.housesearch.browser.suumo.SuumoBrowser; 12 import xyz.kebigon.housesearch.browser.yahoo.transit.YahooTransitBrowser; 13 import xyz.kebigon.housesearch.domain.Posting; 14 import xyz.kebigon.housesearch.domain.SearchConditions; 15 import xyz.kebigon.housesearch.domain.SearchConditionsValidator; 16 import xyz.kebigon.housesearch.file.SentPostingsCache; 17 import xyz.kebigon.housesearch.mail.EmailSender; 18 19 @Slf4j 20 public class HouseSearchApplication 21 { 22 public static void main(String[] args) throws IOException, EmailException 23 { 24 final SentPostingsCache sentPostings = SentPostingsCache.load(); 25 26 try 27 { 28 for (final SearchConditions conditions : SearchConditions.load()) 29 processConditions(conditions, sentPostings); 30 } 31 catch (final Throwable t) 32 { 33 log.error("Unrecoverable exception", t); 34 } 35 finally 36 { 37 sentPostings.save(); 38 } 39 } 40 41 private static void processConditions(SearchConditions conditions, SentPostingsCache sentPostings) throws IOException, EmailException 42 { 43 Collection<Posting> postings; 44 45 try (final SuumoBrowser suumo = new SuumoBrowser()) 46 { 47 postings = suumo.search(conditions, sentPostings); 48 } 49 50 if (postings.isEmpty()) 51 { 52 log.info("No postings found on Suumo, terminating"); 53 return; 54 } 55 56 if (StringUtils.hasLength(conditions.getExpression())) 57 { 58 try (final YahooTransitBrowser yahooTransit = new YahooTransitBrowser()) 59 { 60 ApplicationContext.setYahooTransitBrowser(yahooTransit); 61 62 postings = postings.stream() // Do not parallel here 63 .filter(property -> SearchConditionsValidator.validateExpression(property, conditions)).collect(Collectors.toList()); 64 } 65 66 if (postings.isEmpty()) 67 { 68 log.info("No postings left after applying expression filter, terminating"); 69 return; 70 } 71 } 72 73 log.info("=======[ RESULTS ]======="); 74 log.info("Found {} postings", postings.size()); 75 76 for (final Posting posting : postings) 77 log.info("-> {}", posting); 78 79 log.info("Sending email notification"); 80 81 final EmailSender sender = new EmailSender(); 82 sender.send(postings, conditions); 83 84 // Register sent postings 85 postings.forEach(sentPostings::add); 86 87 log.info("Email notification sent, terminating"); 88 } 89 }