Skip to content

Instantly share code, notes, and snippets.

@kshitijvarshne1
Created May 5, 2021 10:56
Show Gist options
  • Save kshitijvarshne1/3a8b60572315e12ea1169dfdecf2997f to your computer and use it in GitHub Desktop.
Save kshitijvarshne1/3a8b60572315e12ea1169dfdecf2997f 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=204101&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");
String result = "";
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");
}
result += "Vaccination+centres+for+18-44+groups:+" + state_name[0].replace(" ","+") +'+'+
"+state+address:-++" + state_address[0].replace(" ","+")+"+"+availableCapacity[0]+"+slots+are+available+on+";
System.out.println(state_name[0] + state_name[0]);
}
bufferedReader.close();
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------");
}
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