DavidNorgren

Untitled

Nov 17th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.84 KB | None | 0 0
  1. // Run in background:
  2. //    java -jar FindKorTid.jar geckodriver [phoneno] [ssn] [textlocalkey] >FindKorTid.log 2>&1 &
  3. // Close:
  4. //    killall java
  5. //    killall geckoserver
  6. //    top (to check)
  7.  
  8. package findkortid;
  9.  
  10. import java.time.*;
  11. import java.time.format.DateTimeFormatter;
  12. import java.util.List;
  13. import java.util.Map;
  14. import org.openqa.selenium.*;
  15. import org.openqa.selenium.WebDriver;
  16. import org.openqa.selenium.firefox.FirefoxDriver;
  17. import org.openqa.selenium.firefox.FirefoxOptions;
  18. import java.net.URLEncoder;
  19. import java.util.Random;
  20.  
  21. public class FindKorTid {
  22.    
  23.     static final int TEN_MINUTES = 1000 * 60 * 10;
  24.     static final int TWELVE_HOURS = 1000 * 60 * 60 * 12;
  25.     static final int RANDOM_DELAY = 1000 * 10;
  26.    
  27.     static Random rand = new Random();
  28.  
  29.     static Integer parseKr(String kr)
  30.     {
  31.         return Integer.parseInt(kr.replaceAll("[^0-9]", ""));
  32.     }
  33.    
  34.     static LocalDateTime parseDate(String date)
  35.     {
  36.         return LocalDateTime.parse(date, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
  37.     }
  38.    
  39.     public static void main(String[] args) throws Exception
  40.     {
  41.         if (args.length < 4)
  42.         {
  43.             System.out.println("Please supply a geckodriver, phone number, social security number and textlocal key");
  44.             return;
  45.         }
  46.        
  47.         String url = "https://fp.trafikverket.se/Boka/#/";
  48.         LocalDate earliestDate = LocalDate.of(2018, Month.DECEMBER, 3);
  49.         LocalDate latestDate = LocalDate.of(2018, Month.DECEMBER, 20);
  50.         LocalTime latestTime = LocalTime.of(13, 00);
  51.        
  52.         System.setProperty("webdriver.gecko.driver", args[0]);
  53.  
  54.         FirefoxOptions options = new FirefoxOptions();
  55.         options.setHeadless(true);
  56.        
  57.         WebDriver driver = new FirefoxDriver(options);
  58.         driver.get(url);
  59.         System.out.println("[" + LocalDateTime.now() + "] Connecting to " + url);
  60.        
  61.         String script = "var myResult = [];\n" +
  62.                         "$.ajax({\n" +
  63.                         "   type: 'POST',\n" +
  64.                         "   url: 'https://fp.trafikverket.se/Boka/occasion-bundles',\n" +
  65.                         "       dataType: 'json',\n" +
  66.                         "   data: {'bookingSession':{'socialSecurityNumber':'" + args[2] + "','licenceId':5,'bookingModeId':0,'ignoreDebt':false,'examinationTypeId':0,'rescheduleTypeId':'0'},'occasionBundleQuery':{'startDate':'" + earliestDate + "T00:00:00.000Z','locationId':1000038,'languageId':13,'vehicleTypeId':2,'tachographTypeId':1,'occasionChoiceId':1,'examinationTypeId':0}},\n" +
  67.                         "   success: function(data) { myResult = data.data; },\n" +
  68.                         "   async: false\n" +
  69.                         "});\n" +
  70.                         "return myResult;\n";
  71.        
  72.         JavascriptExecutor jsEx = (JavascriptExecutor)driver;
  73.        
  74.         while (true)
  75.         {
  76.             List<Object> resultList = (List<Object>)jsEx.executeScript(script);
  77.             System.out.println("[" + LocalDateTime.now() + "] Found times: " + resultList.size());
  78.  
  79.             for (Object obj: resultList)
  80.             {
  81.                 Map<String, Object> map = (Map<String, Object>)obj;
  82.                 List<Object> occasions = (List<Object>)map.get("occasions");
  83.  
  84.                 // Uppkörning
  85.                 Map<String, Object> bilMap = (Map<String, Object>)(occasions.get(1));
  86.                 Integer bilCost = parseKr((String)bilMap.get("cost"));
  87.                 Map<String, Object> bilDuration = (Map<String, Object>)(bilMap.get("duration"));
  88.                 LocalDateTime bilTimeStart = parseDate((String)bilDuration.get("start"));
  89.  
  90.                 // Kolla tid
  91.                 LocalDate bilTimeDate = bilTimeStart.toLocalDate();
  92.                 LocalTime bilTimeTime = bilTimeStart.toLocalTime();
  93.                 if (bilTimeDate.isAfter(earliestDate) &&
  94.                     bilTimeDate.isBefore(latestDate) &&
  95.                     bilTimeTime.isBefore(latestTime))
  96.                 {
  97.                     System.out.println("[" + LocalDateTime.now() + "] Good time found! Sending SMS");
  98.                    
  99.                     // Teori
  100.                     Map<String, Object> teoriMap = (Map<String, Object>)(occasions.get(0));
  101.                     Integer teoriCost = parseKr((String)teoriMap.get("cost"));
  102.                     Map<String, Object> teoriDuration = (Map<String, Object>)(teoriMap.get("duration"));
  103.                     LocalDateTime teoriTimeStart = parseDate((String)teoriDuration.get("start"));
  104.                     LocalDate teoriTimeDate = teoriTimeStart.toLocalDate();
  105.                     LocalTime teoriTimeTime = teoriTimeStart.toLocalTime();
  106.  
  107.                     String smsMessage = "Det finns en tid för uppkörning & prov!\n" +
  108.                                         "Boka på https://fp.trafikverket.se/Boka/#/search/WOIimOwohPnwss/5/0\n" +
  109.                                         "UPPKÖRNING (" + bilCost + ":-):\n" +
  110.                                         "  Datum: " + bilTimeDate + "\n" +
  111.                                         "  Tid: " + bilTimeTime + "\n" +
  112.                                         "TEORI (" + teoriCost + ":-):\n" +
  113.                                         "  Datum: " + teoriTimeDate + "\n" +
  114.                                         "  Tid: " + teoriTimeTime + "\n";
  115.                     System.out.println(smsMessage);
  116.  
  117.                     String smsApiKey = args[3];
  118.                     String smsTo = args[1];
  119.                     String smsSender = "Uppkörning";
  120.                     String smsScript = "var myResult = '';\n" +
  121.                                        "$.ajax({\n" +
  122.                                         "   type: 'POST',\n" +
  123.                                         "   url: 'https://api.txtlocal.com/send/',\n" +
  124.                                         "   data: {'apiKey': '" + smsApiKey + "', 'numbers': '" + smsTo + "', 'sender': '" + smsSender + "', 'message': '" + URLEncoder.encode(smsMessage, "UTF-8") + "', 'unicode': 'true'},\n" + //, 'test': 'true'
  125.                                         "   success: function(data) { myResult = data; },\n" +
  126.                                         "   async: false\n" +
  127.                                         "});\n" +
  128.                                         "return myResult;";
  129.                    
  130.                     String smsResult = (String)jsEx.executeScript(smsScript);
  131.  
  132.                     System.out.println("RESULT: " + smsResult);
  133.                     Thread.sleep(TWELVE_HOURS + randInt(-RANDOM_DELAY, RANDOM_DELAY));
  134.                     break;
  135.                 }
  136.             }
  137.  
  138.             Thread.sleep(TEN_MINUTES + randInt(-RANDOM_DELAY, RANDOM_DELAY));
  139.         }
  140.        
  141.       // driver.close();
  142.     }
  143.    
  144.     public static int randInt(int min, int max) {
  145.         return rand.nextInt((max - min) + 1) + min;
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment