YouTube ๊ฒ์ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ - YouTube Data Api ์ฐ๋
์ ํ๋ธ ์ฑ๋ ๊ฒ์์ ๋ํ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ์คํธ๋ก ๊ฐ์ ธ์ค๋ ์์ ์ฝ๋์
๋๋ค.
์๋ ์์ค์๋ [๊ฒ์์ด][๋๋ณด๊ธฐ] ๊ธฐ๋ฅ์ด ์ถ๊ฐ๋์ด ํด๋น ๊ฐ๋ค๋ ํ๋ผ๋ฏธํฐ๋ก ๋๊ฒจ ๋ฐ๊ณ ์์ต๋๋ค.
์ ์ด์ ํด๋น API์์ ๊ฒ์์ด๊ฐ ์กด์ฌํด์ผ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๊ณ , ๊ฒฐ๊ณผ์๋ pageToken ์ด ํญ์ ๋์ด์ค๊ณ ํด๋น ํ ํฐ์ ๋ ๋ ค์ ํ์ด์ง ์กฐํ๋ ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์, ํ์ด์ง ๋ํ ๊ตฌํ์ด ๊ฐ๋ฅํฉ๋๋ค.
์์ธํ ํ๋ผ๋ฏธํฐ ๋ฐ ์๋ด ์ฌํญ์ ๋ค์ ์ฃผ์๋ฅผ ์ฐธ๊ณ ํด์ฃผ์ธ์.
developers.google.com/youtube/v3/docs/search/list?hl=ko
Search: list | YouTube Data API | Google Developers
API ์์ฒญ์ ์ง์ ๋ ์ฟผ๋ฆฌ ๋งค๊ฐ๋ณ์์ ์ผ์นํ๋ ๊ฒ์๊ฒฐ๊ณผ์ ๋ชจ์์ ๋ฐํํฉ๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฒ์๊ฒฐ๊ณผ์ ์งํฉ์ ์ฟผ๋ฆฌ ๋งค๊ฐ๋ณ์์ ์ผ์นํ๋ video, channel, playlist ๋ฆฌ์์ค๋ฅผ ์๋ณํ์ง๋ง, ํน์ ์ ํ์ ๋ฆฌ
developers.google.com
package src.lib.youtubeSearchApi;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import src.lib.myMap.MyCamelMap;
import src.lib.myMap.MyMap;
public class youtubeSearchApi {
public static MyMap youtubeSearch(MyMap paramMap) {
// ๊ฒ์๊ฒฐ๊ณผ list
MyMap result = new MyMap();
MyMap info = new MyMap();
List<MyCamelMap> resultDataList = new ArrayList<MyCamelMap>();
List<MyCamelMap> list = new ArrayList<MyCamelMap>();
// key setting
String apiKey = "{API ํค๊ฐ}";
// oauth setting
String oauthKey = "{Oauth ํค๊ฐ}"; // ๋ฏธ์ฌ์ฉ
// request parameter setting
// ์ฐธ๊ณ ์ฌ์ดํธ : https://developers.google.com/youtube/v3/docs/search/list?hl=ko
// ๊ฒ์์ด
String q = paramMap.getStr("searchValue", "");
// ๋ค์ ํ์ด์ง ํ ํฐ
String nextPageToken = paramMap.getStr("nextPageToken", "");
// ๊ฒ์ ์ ํ ํ์
String type = "video"; // ๋น๋์ค๋ง ๊ฒ์, ํ์ฉ ๊ฐ : [channel|playlist|video]
// request
String apiUrl = "https://www.googleapis.com/youtube/v3/search?key=" + apiKey + "&part=snippet" + "&q=" + q + "&type=" + type;
if (!nextPageToken.equals("") && !nextPageToken.equals(null)) {
// ๋ค์ ํ์ด์ง ํธ์ถ์ธ ๊ฒฝ์ฐ์๋ง ๋ค์ ํ์ด์ง ํ ํฐ ๊ฐ ํฌํจ.
apiUrl += "&pageToken=" + nextPageToken;
}
if (paramMap.getStr("searchValue", "").equals("") || paramMap.getStr("searchValue", "").equals(null)) {
// searchValue ์์ผ๋ฉด ๋์ํ์ง ์์.
} else {
try {
// url += URLEncoder.encode(searchValue, "UTF-8");
// Connect to the URL using Java's native library
URL url = new URL(apiUrl);
URLConnection request = url.openConnection();
request.connect();
// Convert to a JSON object to print data
JsonParser jp = new JsonParser(); // from gson
JsonReader jr = new JsonReader(new InputStreamReader((InputStream)request.getContent()));
jr.setLenient(true);
JsonElement root = jp.parse(jr); // Convert the input stream to a json element
JsonObject rootobj = root.getAsJsonObject(); // May be an array, may be an object.
// info parsing
String getNextPageToken = "noMoreResult";
// next page token ์ด ์๋ ๊ฒฝ์ฐ ๊ฐ์ ธ์ค๊ณ ์๋ ๊ฒฝ์ฐ ์๋ค๊ณ ํ์.
if (!rootobj.has("nextPageToken")) {
getNextPageToken = "noMoreResult";
} else {
getNextPageToken = rootobj.get("nextPageToken").getAsString();
}
JsonObject pageInfo = rootobj.get("pageInfo").getAsJsonObject(); // May be an array, may be an object.
String totalResult = pageInfo.get("totalResults").getAsString();
String resultsPerPage = pageInfo.get("resultsPerPage").getAsString();
info.put("nextPageToken", getNextPageToken);
info.put("totalResult", totalResult);
info.put("resultsPerPage", resultsPerPage);
// items parsing
JsonArray dataList = rootobj.get("items").getAsJsonArray();
// map setting
// 1. title 2. id 3. description 4. published date 5. thumburl
Gson gson = new Gson();
Type listType = new TypeToken<ArrayList<MyCamelMap>>() {}.getType();
resultDataList = gson.fromJson(dataList.toString(), listType);
for (int i = 0; i < resultDataList.size(); i++) {
MyCamelMap data = new MyCamelMap();
data.put("title", dataList.get(i).getAsJsonObject().get("snippet").getAsJsonObject().get("title").getAsString());
data.put("videoId", dataList.get(i).getAsJsonObject().get("id").getAsJsonObject().get("videoId").getAsString());
data.put("des", dataList.get(i).getAsJsonObject().get("snippet").getAsJsonObject().get("description").getAsString());
data.put("date", dataList.get(i).getAsJsonObject().get("snippet").getAsJsonObject().get("publishedAt").getAsString());
data.put("thumbUrl", dataList.get(i).getAsJsonObject().get("snippet").getAsJsonObject().get("thumbnails").getAsJsonObject().get("default").getAsJsonObject().get("url").getAsString());
list.add(data);
}
} catch (Exception e) {
e.printStackTrace();
}
result.put("info", info);
result.put("list", list);
result.put("paramMap", paramMap);
}
return result;
}
}
* ํด๋น ์์ค์์ MyMap ๊ณผ MyCamelMap์ map ํ์์ ๊ฐ์ฒด๋ฅผ ๋ฐ๋ก ๊ตฌํํ ๊ฒ์ ๋๋ค. ํด๋น ๋ถ๋ถ์ ๋ฐ๊พธ์ ์ ์ฌ์ฉํ์๋ฉด ๋ฉ๋๋ค.