Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Run in background:
- // java -jar FindKorTid.jar geckodriver [phoneno] [ssn] [textlocalkey] >FindKorTid.log 2>&1 &
- // Close:
- // killall java
- // killall geckoserver
- // top (to check)
- package findkortid;
- import java.time.*;
- import java.time.format.DateTimeFormatter;
- import java.util.List;
- import java.util.Map;
- import org.openqa.selenium.*;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.openqa.selenium.firefox.FirefoxOptions;
- import java.net.URLEncoder;
- import java.util.Random;
- public class FindKorTid {
- static final int TEN_MINUTES = 1000 * 60 * 10;
- static final int TWELVE_HOURS = 1000 * 60 * 60 * 12;
- static final int RANDOM_DELAY = 1000 * 10;
- static Random rand = new Random();
- static Integer parseKr(String kr)
- {
- return Integer.parseInt(kr.replaceAll("[^0-9]", ""));
- }
- static LocalDateTime parseDate(String date)
- {
- return LocalDateTime.parse(date, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
- }
- public static void main(String[] args) throws Exception
- {
- if (args.length < 4)
- {
- System.out.println("Please supply a geckodriver, phone number, social security number and textlocal key");
- return;
- }
- String url = "https://fp.trafikverket.se/Boka/#/";
- LocalDate earliestDate = LocalDate.of(2018, Month.DECEMBER, 3);
- LocalDate latestDate = LocalDate.of(2018, Month.DECEMBER, 20);
- LocalTime latestTime = LocalTime.of(13, 00);
- System.setProperty("webdriver.gecko.driver", args[0]);
- FirefoxOptions options = new FirefoxOptions();
- options.setHeadless(true);
- WebDriver driver = new FirefoxDriver(options);
- driver.get(url);
- System.out.println("[" + LocalDateTime.now() + "] Connecting to " + url);
- String script = "var myResult = [];\n" +
- "$.ajax({\n" +
- " type: 'POST',\n" +
- " url: 'https://fp.trafikverket.se/Boka/occasion-bundles',\n" +
- " dataType: 'json',\n" +
- " 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" +
- " success: function(data) { myResult = data.data; },\n" +
- " async: false\n" +
- "});\n" +
- "return myResult;\n";
- JavascriptExecutor jsEx = (JavascriptExecutor)driver;
- while (true)
- {
- List<Object> resultList = (List<Object>)jsEx.executeScript(script);
- System.out.println("[" + LocalDateTime.now() + "] Found times: " + resultList.size());
- for (Object obj: resultList)
- {
- Map<String, Object> map = (Map<String, Object>)obj;
- List<Object> occasions = (List<Object>)map.get("occasions");
- // Uppkörning
- Map<String, Object> bilMap = (Map<String, Object>)(occasions.get(1));
- Integer bilCost = parseKr((String)bilMap.get("cost"));
- Map<String, Object> bilDuration = (Map<String, Object>)(bilMap.get("duration"));
- LocalDateTime bilTimeStart = parseDate((String)bilDuration.get("start"));
- // Kolla tid
- LocalDate bilTimeDate = bilTimeStart.toLocalDate();
- LocalTime bilTimeTime = bilTimeStart.toLocalTime();
- if (bilTimeDate.isAfter(earliestDate) &&
- bilTimeDate.isBefore(latestDate) &&
- bilTimeTime.isBefore(latestTime))
- {
- System.out.println("[" + LocalDateTime.now() + "] Good time found! Sending SMS");
- // Teori
- Map<String, Object> teoriMap = (Map<String, Object>)(occasions.get(0));
- Integer teoriCost = parseKr((String)teoriMap.get("cost"));
- Map<String, Object> teoriDuration = (Map<String, Object>)(teoriMap.get("duration"));
- LocalDateTime teoriTimeStart = parseDate((String)teoriDuration.get("start"));
- LocalDate teoriTimeDate = teoriTimeStart.toLocalDate();
- LocalTime teoriTimeTime = teoriTimeStart.toLocalTime();
- String smsMessage = "Det finns en tid för uppkörning & prov!\n" +
- "Boka på https://fp.trafikverket.se/Boka/#/search/WOIimOwohPnwss/5/0\n" +
- "UPPKÖRNING (" + bilCost + ":-):\n" +
- " Datum: " + bilTimeDate + "\n" +
- " Tid: " + bilTimeTime + "\n" +
- "TEORI (" + teoriCost + ":-):\n" +
- " Datum: " + teoriTimeDate + "\n" +
- " Tid: " + teoriTimeTime + "\n";
- System.out.println(smsMessage);
- String smsApiKey = args[3];
- String smsTo = args[1];
- String smsSender = "Uppkörning";
- String smsScript = "var myResult = '';\n" +
- "$.ajax({\n" +
- " type: 'POST',\n" +
- " url: 'https://api.txtlocal.com/send/',\n" +
- " data: {'apiKey': '" + smsApiKey + "', 'numbers': '" + smsTo + "', 'sender': '" + smsSender + "', 'message': '" + URLEncoder.encode(smsMessage, "UTF-8") + "', 'unicode': 'true'},\n" + //, 'test': 'true'
- " success: function(data) { myResult = data; },\n" +
- " async: false\n" +
- "});\n" +
- "return myResult;";
- String smsResult = (String)jsEx.executeScript(smsScript);
- System.out.println("RESULT: " + smsResult);
- Thread.sleep(TWELVE_HOURS + randInt(-RANDOM_DELAY, RANDOM_DELAY));
- break;
- }
- }
- Thread.sleep(TEN_MINUTES + randInt(-RANDOM_DELAY, RANDOM_DELAY));
- }
- // driver.close();
- }
- public static int randInt(int min, int max) {
- return rand.nextInt((max - min) + 1) + min;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment