forked from nilesh-kaizen/CovidVaccinationCheckSlot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckSlot.java
More file actions
141 lines (116 loc) · 5.91 KB
/
CheckSlot.java
File metadata and controls
141 lines (116 loc) · 5.91 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;
import java.util.Scanner;
public class CheckSlot {
public static void main(String[] args) throws Exception {
//URL district_id = new URL("https://cdn-api.co-vin.in/api/v2/admin/location/districts/21");
//Mumbai - 395 thane - 392
String mainUrl = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id=XXX&date=";
String districtID = args[0];
int ageLimit = Integer.parseInt(args[1]);
String emailId = args[2];
String password = args[3];
String toEmailId = args[4];
int minAvailability = 1;
StringBuilder message = new StringBuilder("");
boolean flag = false;
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
//Date checkDate = df.parse("03-05-2021");
Date checkDate = new Date();
String modifiedURL = mainUrl.replace("XXX", districtID) + "" + df.format(checkDate);
System.out.println(modifiedURL);
URL url = new URL(modifiedURL);
String oldMessage = "";
while (true) {
{
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
Scanner sc = new Scanner(url.openStream());
StringBuilder input = new StringBuilder("");
while (sc.hasNext()) {
input.append(sc.nextLine());
}
sc.close();
JSONParser parse = new JSONParser();
JSONObject jObj = (JSONObject) parse.parse(input.toString());
JSONArray centers = (JSONArray) jObj.get("centers");
Iterator<JSONObject> centerItr = centers.iterator();
while (centerItr.hasNext()) {
JSONObject centerObj = centerItr.next();
JSONArray sessions = (JSONArray) centerObj.get("sessions");
Iterator<JSONObject> sessionItr = sessions.iterator();
while (sessionItr.hasNext()) {
JSONObject sessionObj = sessionItr.next();
long age = (Long) sessionObj.get("min_age_limit");
long availability = (Long) sessionObj.get("available_capacity");
if (availability >= minAvailability && age == ageLimit) {
flag = true;
message.append("\n");
String centerID = centerObj.get("center_id").toString();
String name = centerObj.get("name").toString();
String date = sessionObj.get("date").toString();
String pincode = centerObj.get("pincode").toString();
String blockName = centerObj.get("block_name").toString();
message.append("\n-----------------------------------------------------\n");
message.append(centerID + " : " + name + " --- " + date + "-- Slots - ");
message.append(sessionObj.get("available_capacity"));
message.append("\n" + pincode + " : " + blockName);
message.append("\n-----------------------------------------------------\n");
}
}
}
if (flag) {
if (!oldMessage.equals(message.toString())) {
sendEmail(message, emailId, password, toEmailId);
}
oldMessage = message.toString();
flag = false;
message = new StringBuilder("");
//System.exit(1);
}
}
System.out.print(".");
Thread.sleep(300000);
}
}
private static void sendEmail(StringBuilder message, String emailId, String password, String toEmailId) throws MessagingException {
final String[] receiver = toEmailId.split(","); //receiver's email address
Properties pr = new Properties();
pr.put("mail.smtp.auth", "true"); //for username and password authentication
pr.put("mail.smtp.starttls.enable", "true");
pr.put("mail.smtp.host", "smtp.gmail.com"); //here host is gmail.com
pr.put("mail.smtp.port", "587"); //port no.
Session gs = Session.getInstance(pr, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailId, password); //pass your email id and password here
}
});
for (String s : receiver) {
messageContent(gs, emailId, s, message.toString());
System.out.println("Sending Email from " + emailId + " to " + s);
}
System.out.println("Message sent! ");
}
private static Message messageContent(Session gs, String emailId, String reciever, String message) throws MessagingException {
Message msg = new MimeMessage(gs);
msg.setFrom(new InternetAddress(emailId));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(reciever));
msg.setSubject("Slots for vaccine"); //to set the subject (not mandatory)
msg.setText(message);
Transport.send(msg);
return msg;
}
}