1

I'm making my own API from Instagram's public API. So basically what my api will do is it will show user all the slide-videos and the user can decide which one to download. In my API i just want all videos url and shortcode.

This is how my API will looks like.

info : [
{
  shortcode : "",
  video_url :  "",  
},
{
  shortcode : "",
  video_url :  "",  
  
},
{
  shortcode : "",
  video_url :  "",  
  
}]

and this is how I'm reading the data of Instagram's API.

URL url = new URL("https://www.instagram.com" + link + "?__a=1");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

    connection.setRequestMethod("GET");
    connection.setRequestProperty("User-Agent", "Chrome/86.0.4240.75");
    BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
    String inputLine;
    StringBuilder stream = new StringBuilder();
    while ((inputLine = br.readLine()) != null) {
        stream.append(inputLine);
        pojo.data(stream.toString()); 
    }
    System.out.println(pojo.data());
    
    JSONObject obj = new JSONObject(pojo.data());
    JSONObject obj1 = obj.getJSONObject("graphql");
    JSONObject obj2 = obj1.getJSONObject("shortcode_media");
    String id = obj2.getString("id");
    JSONObject obj3 = obj2.getJSONObject("edge_sidecar_to_children");
    JSONArray array = obj3.getJSONArray("edges");
    
    if (obj2.getBoolean("is_video") == false) {
        System.out.println("is video false");

        for (int i = 0; i < array.length(); i++) {
            JSONObject json = array.getJSONObject(i);
            JSONObject json1 = json.getJSONObject("node");
            String video_urls = json1.getString("video_url");
            String shortcode= json1.getString("shortcode");
            System.out.println(video_urls);
            System.out.println(shortcode);
            getInfo(shortcode);
           }}

private static void getInfo(String data) {

    ArrayList<JSONObject> arrayList = new ArrayList<>();
    JSONObject postedJSON = new JSONObject();
    JSONObject ob = new JSONObject();

    postedJSON.put("shortcode", data);  
    arrayList.add(postedJSON);
    ob.put("info", arrayList);
    String variable = arrayList.toString();
}

In case if you want URL here it is : https://www.instagram.com/p/CH16_AtFZwu/?__a=1

I'm able to get all videos url and shortcode. You can see that i'm calling getInfo method inside the loop but getInfo method return me data something like this i know this is because of getInfo method is calling inside the loop.

{"info":[{"shortcode":"CHpRpJKJaov"}]}
{"info":[{"shortcode":"CHpRpJJJR2n"}]}
{"info":[{"shortcode":"CHpRpJoJgak"}]}
{"info":[{"shortcode":"CHpRpKLprpB"}]}
{"info":[{"shortcode":"CHpRpKnJbtC"}]}
{"info":[{"shortcode":"CHpRtH9Jbpy"}]}
{"info":[{"shortcode":"CHpRtUsJkng"}]}
{"info":[{"shortcode":"CHpRyzgJnMf"}]}
{"info":[{"shortcode":"CHpR9dYJdGB"}]}
{"info":[{"shortcode":"CHpR9s1Jj-2"}]}

In the end i just want to ask how can i make it without repeating the info array name like in the API which i given in the beginning.

Nakul
  • 23
  • 1
  • 4
  • why don't you use JSONArray? ex: https://stackoverflow.com/questions/18983185/how-to-create-correct-jsonarray-in-java-using-jsonobject – Qwer Izuken Nov 22 '20 at 18:33

1 Answers1

1

Your problem lies into getinfo method. you did not add the videoUrl properly and not putting a into final json object. here is how to do it properly:

       // declare it before the loop.
       ArrayList<JSONObject> arrayList = new ArrayList<>();
       JSONObject ob = new JSONObject();

       if (obj2.getBoolean("is_video") == false) {
          System.out.println("is video false");

        for (int i = 0; i < array.length(); i++) {
            JSONObject json = array.getJSONObject(i);
            JSONObject json1 = json.getJSONObject("node");
            String video_urls = json1.getString("video_url");
            String shortcode= json1.getString("shortcode");
            System.out.println(video_urls);
            System.out.println(shortcode);

            // adding data into array list.
            arrayList.add(getInfo(shortcode, video_urls));
           }
       // finally put info into the final element.  
          ob.put("info", arrayList);
         // Now ob contains your actual data.
       }

private static JSONObject getInfo(String shortcode, String video_url) {
    JSONObject ob = new JSONObject();
    postedJSON.put("shortcode", shortcode); 
    postedJSON.put("video_url", video_url);  
    return postedJSON;
}
MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37