parent
3da7ef8b7f
commit
174c48658a
@ -0,0 +1,41 @@
|
||||
package com.wbq.zuoye;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class BilibiliCidFetcher {
|
||||
public static String getCid(String bvid) {
|
||||
String apiUrl = "https://api.bilibili.com/x/player/pagelist?bvid=" + bvid;
|
||||
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
HttpGet request = new HttpGet(apiUrl);
|
||||
request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36");
|
||||
try (CloseableHttpResponse response = httpClient.execute(request)) {
|
||||
String jsonResponse = EntityUtils.toString(response.getEntity(), "UTF-8");
|
||||
JsonObject jsonObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
|
||||
JsonArray dataArray = jsonObject.getAsJsonArray("data");
|
||||
|
||||
if (dataArray.size() > 0) {
|
||||
JsonObject firstPage = dataArray.get(0).getAsJsonObject();
|
||||
String cid = firstPage.get("cid").getAsString();
|
||||
System.out.println("Found cid: " + cid);
|
||||
return cid;
|
||||
} else {
|
||||
System.out.println("No data found.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue