aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/utility/YouTubeAPI.java
blob: 4acecf5ea7d605f30889c3402afca88e72745cb6 (plain) (blame)
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
package utility;

import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

public class YouTubeAPI {
    private String ytapiKey = "";
    public YouTubeAPI(String ytapiKey){
        this.ytapiKey = ytapiKey;
    }
    public String returnTopVideoURL(String keyword )throws IOException {
        String url = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q="+ URLEncoder.encode(keyword, StandardCharsets.UTF_8)+"&type=video&key="+ytapiKey;
        System.out.println(url);
        String data = Jsoup.connect(url).ignoreContentType(true).execute().body();
        JSONObject obj = new JSONObject(data);
        JSONArray arr = obj.getJSONArray("items");
        String videoID = "";
        for (int i = 0; i < arr.length(); i++)
        {
            videoID = arr.getJSONObject(i).getJSONObject("id").getString("videoId");
            System.out.println("Parsed ID "+ videoID);
        }
        return "https://www.youtube.com/watch?v="+videoID;
    }
    public void getAllURLPlaylist(String playlistID){
        try {
            String url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId="+playlistID+"&key="+ytapiKey;
            System.out.println(url);
            url = url.replaceAll(" ", "%20");
            String data = Jsoup.connect(url).ignoreContentType(true).execute().body();
            JSONObject obj = new JSONObject(data);
            JSONArray arr = obj.getJSONArray("items");
            //print arr
            for (int i = 0; i < arr.length(); i++) {
                String videoID = arr.getJSONObject(i).getJSONObject("id").getString("videoId");
                System.out.println("Parsed ID " + videoID);
            }

            }
         catch (IOException e) {
            e.printStackTrace();
        }
    }
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage