抖音分享链接信息爬取
package com.chuji.util.DOUYIN;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.chuji.model.VO.TiktokVO;
import com.chuji.model.VO.UrlResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
/**
* @Author: tangziqian
* @Description: 抖音分享链接信息爬取
* @Date: Created in 17:30 2021/3/15
* @Modified By:
*/
public class DouYinShareUtil {
private Logger logger = LoggerFactory.getLogger(DouYinShareUtil.class);
/**
* 根据视频分享的url获取抖音用户信息
* @param dyUrl 抖音视频链接
* @return
* @throws Exception
*/
public static TiktokVO getDouYinMessage(String dyUrl) throws Exception{
JSONObject jsonObj2 = getjsonobj(dyUrl);
String author = jsonObj2.getString("author");
JSONObject jsonObject3 =JSONObject.parseObject(author);
System.out.println("抖音名:"+jsonObject3.getString("nickname"));
System.out.println("抖音号:"+jsonObject3.getString("unique_id"));
System.out.println("抖音ID:"+jsonObject3.getString("uid"));
System.out.println("抖音short_id:"+jsonObject3.getString("short_id"));
System.out.println("抖音signature:"+jsonObject3.getString("signature"));
System.out.println("抖音视频标题:"+jsonObj2.getString("desc"));
TiktokVO tiktokVO = new TiktokVO();
tiktokVO.setTiktokName(jsonObject3.getString("nickname"));
tiktokVO.setTiktokNumber(jsonObject3.getString("unique_id"));
tiktokVO.setTiktokId(jsonObject3.getString("uid"));
tiktokVO.setTiktokShortId(jsonObject3.getString("short_id"));
tiktokVO.setTiktokSignature(jsonObject3.getString("signature"));
tiktokVO.setTiktokTitle(jsonObj2.getString("desc"));
return tiktokVO;
}
/**
* 根据视频分享的url获取抖音用户信息
* @param dyUrl 抖音视频链接
* @return
* @throws Exception
*/
public static String getDouYinVideoImage(String dyUrl) throws Exception{
JSONObject jsonObj2 = getjsonobj(dyUrl);
String video = jsonObj2.getString("video");
JSONObject videoObj =JSONObject.parseObject(video);
Object obj2 = videoObj.get("cover");
JSONObject videoObj2 = JSON.parseObject(obj2.toString());
JSONArray videoJson = videoObj2.getJSONArray("url_list");
Object image = videoJson.get(0);
return image.toString();
}
public static JSONObject getjsonobj(String dyUrl) throws Exception{
URL url = new URL(dyUrl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.getResponseCode();
//重定向之后的url
String realUrl=conn.getURL().toString();
conn.disconnect();
String[] strs = realUrl.split("/");
String splitUrl = "";
boolean boo = false;
for (String aa:strs) {
if(boo){
splitUrl = aa;
break;
}
if("video".equals(aa)){
boo = true;
}
}
//splitUrl 视频id
String jsonUrl = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=" + splitUrl;
StringBuilder json = new StringBuilder();
URL urlObject = new URL(jsonUrl);
URLConnection uc = urlObject.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(uc
.getInputStream(), "utf-8"));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
json.append(inputLine);
}
JSONObject jsonObject1 =JSONObject.parseObject(json.toString());
JSONArray array = jsonObject1.getJSONArray("item_list");
Object obj2 = array.get(0);
JSONObject jsonObj2 = JSON.parseObject(obj2.toString());
return jsonObj2;
}
/**
* 根据直播分享的url获取抖音用户信息
* @param dyUrl 抖音直播链接
* @return
* @throws Exception
*/
public static TiktokVO getLiveMessage(String dyUrl) throws Exception {
JSONObject jsonObject = liveImage(dyUrl);
JSONObject owner = (JSONObject) jsonObject.get("owner");
String nikeName = owner.getString("nickname");
String id = owner.getString("id");
String short_id = owner.getString("short_id");
System.out.println("抖音名:" + nikeName);
System.out.println("抖音ID:" + id);
System.out.println("抖音short_id:" + short_id);
System.out.println("抖音标题:" + jsonObject.getString("title"));
TiktokVO tiktokVO = new TiktokVO();
tiktokVO.setTiktokName(nikeName);
tiktokVO.setTiktokId(id);
tiktokVO.setTiktokShortId(short_id);
tiktokVO.setTiktokTitle(jsonObject.getString("title"));
return tiktokVO;
}
/**
* 根据直播分享的url获取抖音截图
* @param dyUrl 抖音直播链接
* @return
* @throws Exception
*/
public static String getLiveImage(String dyUrl) throws Exception {
JSONObject jsonObject = liveImage(dyUrl);
JSONObject coverObj = (JSONObject) jsonObject.get("cover");
JSONArray coverArray = coverObj.getJSONArray("url_list");
Object obj = coverArray.get(0);
// System.out.println(obj.toString());
return obj.toString();
}
public static JSONObject liveImage(String dyUrl) throws Exception{
URL url = new URL(dyUrl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.getResponseCode();
//重定向之后的url
String realUrl=conn.getURL().toString();
conn.disconnect();
String[] strs = realUrl.split("/");
String splitUrl = "";
boolean boo = false;
for (String aa:strs) {
if(boo){
splitUrl = aa;
break;
}
if("reflow".equals(aa)){
boo = true;
}
}
//splitUrl 视频id
String jsonUrl = "https://webcast.amemv.com/webcast/reflow/" + splitUrl;
StringBuilder json = new StringBuilder();
URL urlObject = new URL(jsonUrl);
URLConnection uc = urlObject.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(uc
.getInputStream(), "utf-8"));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
json.append(inputLine);
}
String jsonStr = json.substring((json.indexOf("{",json.indexOf("{")+1 )),json.lastIndexOf("}"));
JSONObject jsonObject1 =JSONObject.parseObject(jsonStr.toString());
JSONObject obj1 = (JSONObject) jsonObject1.get("room");
return obj1;
}
/**
* 调用传视频url获取视频信息
* @param url 视频url
* @return
* @throws Exception
*/
public static TiktokVO getVideo(String url) throws Exception {
TiktokVO tiktokVO = getDouYinMessage(url);
String image = getDouYinVideoImage(url);
tiktokVO.setVideoImage(image);
return tiktokVO;
}
/**
* 调用传直播url获取直播信息
* @param url 直播url
* @return
* @throws Exception
*/
public static TiktokVO getLive(String url) throws Exception {
TiktokVO tiktokVO = getLiveMessage(url);
String liveImage = getLiveImage(url);
tiktokVO.setLiveImage(liveImage);
return tiktokVO;
}
public static UrlResultVO getTiktokLive(String Url) throws Exception {
JSONObject jsonObject = liveImage(Url);
JSONObject owner = (JSONObject) jsonObject.get("owner");
String nikeName = owner.getString("nickname");
String id = owner.getString("id");
String short_id = owner.getString("short_id");
UrlResultVO resultVO = new UrlResultVO();
resultVO.setMessageType("抖音直播");
resultVO.setName(nikeName);
resultVO.setId(id);
resultVO.setShortId(short_id);
resultVO.setTitle(jsonObject.getString("title"));
resultVO.setCoverUrl(getLiveImage(Url));
return resultVO;
}
public static UrlResultVO getTiktokVideo(String Url) throws Exception {
JSONObject jsonObj2 = getjsonobj(Url);
String author = jsonObj2.getString("author");
JSONObject jsonObject3 =JSONObject.parseObject(author);
System.out.println("抖音名:"+jsonObject3.getString("nickname"));
System.out.println("抖音号:"+jsonObject3.getString("unique_id"));
System.out.println("抖音ID:"+jsonObject3.getString("uid"));
System.out.println("抖音short_id:"+jsonObject3.getString("short_id"));
System.out.println("抖音signature:"+jsonObject3.getString("signature"));
System.out.println("抖音视频标题:"+jsonObj2.getString("desc"));
//抖音签名
jsonObject3.getString("signature");
UrlResultVO resultVO = new UrlResultVO();
resultVO.setMessageType("抖音视频");
resultVO.setName(jsonObject3.getString("nickname"));
resultVO.setCode(jsonObject3.getString("unique_id"));
resultVO.setId(jsonObject3.getString("uid"));
resultVO.setShortId(jsonObject3.getString("short_id"));
resultVO.setTitle(jsonObj2.getString("desc"));
resultVO.setCoverUrl(getDouYinVideoImage(Url));
return resultVO;
}
public static void main(String[] args) throws Exception {
//调用抖音分享的视频链接
getTiktokVideo("https://v.douyin.com/eL6p16f/");
//调用抖音分享的直播链接
getTiktokLive("https://v.douyin.com/eL6p16f/");
}
}
爬取的抖音数据实体
package com.chuji.model.VO;
/**
* @Author: tangziqian
* @Description: 爬取的抖音数据
* @Date: Created in 17:54 2021/3/16
* @Modified By:
*/
public class TiktokVO {
/**
* 抖音名
*/
private String tiktokName;
/**
* 抖音号
*/
private String tiktokNumber;
/**
* 抖音ID
*/
private String tiktokId;
/**
* 抖音short_id
*/
private String tiktokShortId;
/**
* 抖音signature(签名)
*/
private String tiktokSignature;
/**
* 视频截图
*/
private String videoImage;
/**
* 直播截图
*/
private String liveImage;
/**
* 抖音视频或者直播标题
*/
private String tiktokTitle;
public String getTiktokTitle() {
return tiktokTitle;
}
public void setTiktokTitle(String tiktokTitle) {
this.tiktokTitle = tiktokTitle;
}
public String getTiktokName() {
return tiktokName;
}
public void setTiktokName(String tiktokName) {
this.tiktokName = tiktokName;
}
public String getTiktokNumber() {
return tiktokNumber;
}
public void setTiktokNumber(String tiktokNumber) {
this.tiktokNumber = tiktokNumber;
}
public String getTiktokId() {
return tiktokId;
}
public void setTiktokId(String tiktokId) {
this.tiktokId = tiktokId;
}
public String getTiktokShortId() {
return tiktokShortId;
}
public void setTiktokShortId(String tiktokShortId) {
this.tiktokShortId = tiktokShortId;
}
public String getTiktokSignature() {
return tiktokSignature;
}
public void setTiktokSignature(String tiktokSignature) {
this.tiktokSignature = tiktokSignature;
}
public String getVideoImage() {
return videoImage;
}
public void setVideoImage(String videoImage) {
this.videoImage = videoImage;
}
public String getLiveImage() {
return liveImage;
}
public void setLiveImage(String liveImage) {
this.liveImage = liveImage;
}
}
抖音快手公共实体
package com.chuji.model.VO;
/**
* @Author: tangziqian
* @Description:
* @Date: Created in 10:59 2021/3/18
* @Modified By:
*/
public class UrlResultVO {
/**
* 表明是抖音还是快手
*/
private String messageType;
/**
* 快手号或抖音号
*/
private String code;
/**
* 快手id或者抖音id
*/
private String id;
/**
* 快手名称或者抖音名称
*/
private String name;
/**
* 抖音短id
*/
private String shortId;
/**
* 快手头像
*/
private String ksHeaderUrl;
/**
* 快手/抖音视频截图
*/
private String coverUrl;
/**
* 快手/抖音视频的标题
*/
private String title;
public String getMessageType() {
return messageType;
}
public void setMessageType(String messageType) {
this.messageType = messageType;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getShortId() {
return shortId;
}
public void setShortId(String shortId) {
this.shortId = shortId;
}
public String getKsHeaderUrl() {
return ksHeaderUrl;
}
public void setKsHeaderUrl(String ksHeaderUrl) {
this.ksHeaderUrl = ksHeaderUrl;
}
public String getCoverUrl() {
return coverUrl;
}
public void setCoverUrl(String coverUrl) {
this.coverUrl = coverUrl;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}