Skip to content

Instantly share code, notes, and snippets.

@kshitijvarshne1
Created May 5, 2021 11:11
Show Gist options
  • Save kshitijvarshne1/1f78aa77bf7051371b72f3d369ceefd6 to your computer and use it in GitHub Desktop.
Save kshitijvarshne1/1f78aa77bf7051371b72f3d369ceefd6 to your computer and use it in GitHub Desktop.
/* Created by IntelliJ IDEA.
* Author: Kshitij Varshney (kshitijvarshne1)
* Date: 05-May-21
* Time: 1:45 PM
* File: WebController.java
*/
package com.example.aaaa;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
@RestController
public class WebController {
@GetMapping("/")
public String working() {
return "working";
}
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String writeSession(String paramString) {
try {
//https://cdn-api.co-vin.in/api/v2/admin/location/states
URL uRL = new URL("https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=202001&date=05-05-2021");
String string3 = "";
HttpURLConnection httpURLConnection = (HttpURLConnection) uRL.openConnection();
httpURLConnection.setRequestMethod("GET");
int n = httpURLConnection.getResponseCode();
if (n == 200) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
StringBuffer stringBuffer = new StringBuffer();
while ((string3 = bufferedReader.readLine()) != null) {
stringBuffer.append(string3);
}
JSONObject jsonObject = new JSONObject(stringBuffer.toString());
JSONArray array = jsonObject.getJSONArray("sessions");
if (array != null) {
String[] state_name = new String[array.length()];
String[] state_address = new String[array.length()];
long[] availableCapacity= new long[array.length()];
for (int i = 0; i < array.length(); i++) {
state_name[i] = array.getJSONObject(i).getString("name");
state_address[i] = array.getJSONObject(i).getString("address");
availableCapacity[i]=array.getJSONObject(i).getLong("available_capacity");
String result = "Vaccination+centres+for+18-44+groups:+" + state_name[i].replace(" ","+") +'+'+
"+state+address:-++" + state_address[i].replace(" ","+")+"+"+availableCapacity[i]+"+slots+are+available+on+";
System.out.println(state_name[i] +" " +state_address[i]);
String apiURL="https://api.telegram.org/bot1604017651:AAEWlp-OllFlAF4clHk4WPnUBVmtf0SR1b8/sendMessage?chat_id=@bnbotttt&text=";
apiURL+=result;
System.out.println(apiURL);
URL uRL2 = new URL(apiURL);
HttpURLConnection httpURLConnection2 = (HttpURLConnection) uRL2.openConnection();
httpURLConnection2.setRequestMethod("GET");
int n2 = httpURLConnection2.getResponseCode();
System.out.println(n2 + "result------");
}
}
bufferedReader.close();
}
return "true";
} catch (Exception exception) {
System.out.println(exception);
return "false";
}
}
}
//https://api.telegram.org/bot1604017651:AAEWlp-OllFlAF4clHk4WPnUBVmtf0SR1b8/sendMessage?chat_id=@bnbotttt&text=hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment