blob: 61b48ed17d96397eb135a3b03e0703b8d43a7ae5 (
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
|
package utility;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import java.io.IOException;
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="+keyword+"&type=video&key="+ytapiKey;
url = url.replaceAll(" ", "%20");
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();
}
}
}
|