-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.java
128 lines (109 loc) · 5 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.vss;
import com.google.gson.Gson;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.stream.Collectors;
public class Main {
// theoryExamination 1,exchangingDriverLicence 2,vehicleTranscription 4,vehicleFirstRegistration 8,temporaryPermit 16,
// vehicleDeactivation 32,driverLicenceDuplicate 64,vehicleRegistrationDuplicate 128,exchangingForeignDriverLicence 256,
public static final int CODE_SERVICE = 8; // activity code; (ex: vehicleFirstRegistration 8)
public static final int CODE_MUNICIPALITY = 24; // internal code for municipality; (ex: 24-Maramures)
public static final int INTERVAL_MILLIS = 90000; // 1.5 minutes in millis
public static final String REF_DATE = "2022-04-09"; // y-m-d format
public static final boolean KILL_IF_FOUND = false; // kill process if date has been found;
public static final boolean OPEN_URL = true; // if false url config can be null
public static final String URL_TO_OPEN = "https://www.youtube.com/watch?v=Gz2GVlQkn4Q";
public static final boolean SEND_EMAIL = true; // if false email config can be null
public static final String EMAIL_FROM = "[email protected]";
public static final String EMAIL_PASS = "password-banana";
public static final String EMAIL_TO = "[email protected]";
public static final String EMAIL_SMTP_HOST = "mail.test.test";
public static final String EMAIL_SMTP_PORT = "8889";
public static final String EMAIL_SUBJECT = "Hi from DRPCIV scan";
public static final String EMAIL_CONTENT = "We found a free date for ";
public static void main(String[] args) {
Gson gson = new Gson();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
boolean hasDate = doCheck(gson);
if (hasDate) {
if (OPEN_URL) {
openSound();
}
if (SEND_EMAIL) {
sendEmail();
}
if (KILL_IF_FOUND) {
System.out.println("Finished scanning..");
timer.cancel();
timer.purge();
}
}
} catch (Exception ex) {
// TODO split exception
System.out.println("Exception " + ex);
}
}
}, 0, INTERVAL_MILLIS);
}
private static boolean doCheck(Gson gson) throws IOException {
// TODO move url creation above;
URL url = new URL("https://www.drpciv.ro/drpciv-booking-api/getAvailableDaysForSpecificService/" + CODE_SERVICE + "/" + CODE_MUNICIPALITY);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("accept", "application/json");
String text = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
System.out.println(text);
List<String> dates = (List<String>) gson.fromJson(text, Object.class);
for (String item : dates) {
if (item.startsWith(REF_DATE)) {
System.out.println("Found date!");
return true;
}
}
return false;
}
private static void openSound() throws URISyntaxException, IOException {
Desktop d = Desktop.getDesktop();
d.browse(new URI(URL_TO_OPEN));
System.out.println("URL opened..");
}
private static void sendEmail() throws MessagingException {
Properties prop = new Properties();
prop.put("mail.smtp.host", EMAIL_SMTP_HOST);
prop.put("mail.smtp.port", EMAIL_SMTP_PORT);
prop.put("mail.smtp.auth", "true");
Session session = Session.getInstance(prop,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(EMAIL_FROM, EMAIL_PASS);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(EMAIL_FROM));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(EMAIL_TO)
);
message.setSubject(EMAIL_SUBJECT);
message.setText(EMAIL_CONTENT + REF_DATE);
Transport.send(message);
System.out.println("Email sent..");
}
}