新建:File–new–project
下载1.8jdk 以及 Maven 我这里下载的是 Maven 3.6.1
GroupID是项目组织唯一的标识符:一般书写格式:com.公司.项目名
ArtifactID就是项目的唯一的标识符:一般书写格式:项目名
需要提前配置maven 还要修改settings.xml文件 这里就不多说了
这是我new好的文件 在这里我就不多做解释了
在JsonUtil这个java文件里代码内容如下
package com.rjt.common.util;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSON;
public class JsonUtil {
/**
* 将json格式化为字符串,然后根据key取值
* @param jsonStr
* @param key
* @return
*/
public static String getJsonValue(String jsonStr,String key) {
if(StringUtils.isEmpty(jsonStr) || StringUtils.isEmpty(key)) {
return "";
}
if(key.indexOf(".")==-1) {
if(key.indexOf("[")!=-1) {
int num =Integer.parseInt(TextUtil.getSubString(key, "[", "]"));
key=key.substring(0, key.indexOf("["));
return JSON.parseObject(jsonStr).getJSONArray(key).getString(num);
}else {
return JSON.parseObject(jsonStr).getString(key);
}
}else {
String[] keys = key.split("\\.");
for(int i=0;i
package com.rjt.common.util;
import blade.kit.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Random;
public class TextUtil {
private static Logger logger = LoggerFactory.getLogger(TextUtil.class);
/**
* 取两个文本之间的文本值
*
* @param text
* @param left
* @param right
* @return
*/
public static String getSubString(String text, String left, String right) {
String result = "";
int zLen;
if (left == null || left.isEmpty()) {
zLen = 0;
} else {
zLen = text.indexOf(left);
if (zLen > -1) {
zLen += left.length();
} else {
zLen = 0;
}
}
int yLen = text.indexOf(right, zLen);
if (yLen < 0 || right == null || right.isEmpty()) {
yLen = text.length();
}
result = text.substring(zLen, yLen);
return result;
}
/**
* 获取ip
*
* @param request
* @return
*/
public static String getRemortIP(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
// 多次反向代理后会有多个ip值,第一个ip才是真实ip
if( ip.indexOf(",")!=-1 ){
ip = ip.split(",")[0];
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
/**
* 根据ip获取地址
*
* @param
* @return
* @throws UnsupportedEncodingException
*/
public static String getPlace(String ip) throws UnsupportedEncodingException {
if (ip.indexOf(", ") != -1) {
ip = ip.split(", ")[1];
}
String url = "https://ip.cn/index.php?ip=" + ip;
HttpRequest request = HttpRequest.get(url, true).header("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0");
String res = request.body();
String place = TextUtil.getSubString(res, "置:", "
");
request.disconnect();
return place;
}
/**
* 根据地址获取纬度
*
* @param
* @return
* @throws UnsupportedEncodingException
*/
public static String getWd(String place) throws UnsupportedEncodingException {
String url = "http://apis.map.qq.com/jsapi?qt=poi&wd=" + URLEncoder.encode(place, "utf8");
HttpRequest request = HttpRequest.get(url, true);
String res = request.body();
request.disconnect();
return handResult(res);
}
/**
* 处理数据
*
* @param res
* @return
*/
private static String handResult(String res) {
JSONObject json = JSON.parseObject(res);
json = JSON.parseObject(json.get("detail").toString());
JSONArray jsonArray = JSON.parseArray((json.get("pois").toString()));
int ranNum = getRandomNumRang(jsonArray.size());
json = JSON.parseObject(jsonArray.get(ranNum).toString());
return json.get("pointx") + "," + json.get("pointy");
}
public static void main(String[] args) throws UnsupportedEncodingException {
int a=0;
for (int i=0;i<100;i++){
String ee= getRandomNum(1);
if(ee.equals("8")){
System.out.println(a++);
}
}
}
/**
* 根据ip获取维度和精度
* @param
* @return
* @throws UnsupportedEncodingException
*/
public static String getWd(HttpServletRequest request) throws UnsupportedEncodingException {
String remortIP = getRemortIP(request);
logger.info("remortIP 为{}",remortIP);
String place = getPlace(remortIP);
logger.info("place 为{}",place);
String wd="112.537170,37.874690";
try {
wd = getWd(place);
} catch (Exception e) {
// TODO: handle exception
logger.info("e 为{}",e);
}
logger.info("wd 为{}",wd);
return wd;
}
/**
* 取随机数字
*
* @param num
* @return
*/
public static String getRandomNum(int num) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < num; i++) {
Random rnd = new Random();
sb.append(rnd.nextInt(9));
}
return sb.toString();
}
/**
* 根据范围取随机数字
*
* @param
* @return
*/
public static int getRandomNumRang(int end) {
Random rnd = new Random();
return rnd.nextInt(end);
}
public static String makeImei () {
String imeiString=TextUtil.getRandomNum(14);
char[] imeiChar=imeiString.toCharArray();
int resultInt=0;
for (int i = 0; i < imeiChar.length; i++) {
int a=Integer.parseInt(String.valueOf(imeiChar[i]));
i++;
final int temp=Integer.parseInt(String.valueOf(imeiChar[i]))*2;
final int b=temp<10?temp:temp-9;
resultInt+=a+b;
}
resultInt%=10;
resultInt=resultInt==0?0:10-resultInt;
return imeiString+resultInt;
}
}
package com.rjt.video.controller;
import java.util.Objects;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.rjt.video.model.VideoModel;
import com.rjt.video.service.VideoService;
import com.rjt.video.service.impl.VideoFactory;
/**
* @comment
* @author tanran
* @date 2019年6月14日
* @version 1.0
*/
@RestController
@RequestMapping("video")
@CrossOrigin
public class VideoController {
@GetMapping(value = "parse")
public VideoModel parse(String url) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
VideoService videoService = VideoFactory.getVideo(url);
if(Objects.isNull(videoService)) {
return new VideoModel();
}
return videoService.parseUrl(url);
}
}
package com.rjt.video.model;
public class VideoModel {
//视频名
private String name;
//视频背景
private String cover;
//无水印地址
private String playAddr;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the cover
*/
public String getCover() {
return cover;
}
/**
* @param cover the cover to set
*/
public void setCover(String cover) {
this.cover = cover;
}
/**
* @return the playAddr
*/
public String getPlayAddr() {
return playAddr;
}
/**
* @param playAddr the playAddr to set
*/
public void setPlayAddr(String playAddr) {
this.playAddr = playAddr;
}
@Override
public String toString() {
return "VideoModel [name=" + name + ", cover=" + cover + ", playAddr=" + playAddr + "]";
}
}
service文件下的impl文件包含7个java文件:
DouyinServiceImpl
package com.rjt.video.service.impl;
import org.springframework.stereotype.Service;
import com.rjt.common.util.JsonUtil;
import com.rjt.common.util.TextUtil;
import com.rjt.video.model.VideoModel;
import com.rjt.video.service.VideoService;
import blade.kit.http.HttpRequest;
/**
* @comment
* @author tanran
* @date 2019年6月14日
* @version 1.0
*/
@Service
public class DouyinServiceImpl implements VideoService{
@Override
public VideoModel parseUrl(String url) {
// TODO Auto-generated method stub
VideoModel videoModel=new VideoModel();
HttpRequest request = HttpRequest.get(url);
String res = request.body();
String awemeId=TextUtil.getSubString(res, "https://www.iesdouyin.com/share/video/", "/?");
url ="https://api-hl.amemv.com/aweme/v1/aweme/detail/?aid=1128&app_name=aweme&version_code=251&aweme_id="+awemeId;
request=HttpRequest.get(url).header("User-Agent","Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
res = request.body();
System.out.println(res);
String title = JsonUtil.getJsonValue(res, "aweme_detail.share_info.share_title");
String playAddr = JsonUtil.getJsonValue(res, "aweme_detail.video.play_addr.url_list[0]");
String cover = JsonUtil.getJsonValue(res, "aweme_detail.video.origin_cover.url_list[0]");
videoModel.setName(title);
videoModel.setPlayAddr(playAddr);
videoModel.setCover(cover);
return videoModel;
}
public static void main(String[] args) {
System.out.println(new DouyinServiceImpl().parseUrl("http://v.douyin.com/r2w3sN/"));
}
}
HuoShanServiceImpl
package com.rjt.video.service.impl;
import org.springframework.stereotype.Service;
import com.rjt.common.util.JsonUtil;
import com.rjt.common.util.TextUtil;
import com.rjt.video.model.VideoModel;
import com.rjt.video.service.VideoService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* @comment
* @author tanran
* @date 2019年6月14日
* @version 1.0
*/
@Service
public class HuoShanServiceImpl implements VideoService {
@Override
public VideoModel parseUrl(String url) {
// TODO Auto-generated method stub
VideoModel videoModel=new VideoModel();
/*HttpRequest request = HttpRequest.get(url);
String res = request.body();
System.out.println(res);*/
try {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Response response = okHttpClient.newCall(request).execute();
String result=response.body().string();
System.out.println(result);
result=TextUtil.getSubString(result, "create({d:", "});");
String videoId=JsonUtil.getJsonValue(result, "video.uri");
videoModel.setPlayAddr("http://hotsoon.snssdk.com/hotsoon/item/video/_playback/?video_id="+videoId);
videoModel.setCover(JsonUtil.getJsonValue(result, "video.cover.url_list[0]"));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return videoModel;
}
public static void main(String[] args) {
System.out.println(new HuoShanServiceImpl().parseUrl("https://reflow.huoshan.com/hotsoon/s/th01P3Eu700/"));
}
}
KuaiShouServiceImpl
package com.rjt.video.service.impl;
import org.springframework.stereotype.Service;
import com.rjt.common.util.JsonUtil;
import com.rjt.common.util.TextUtil;
import com.rjt.video.model.VideoModel;
import com.rjt.video.service.VideoService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* @comment
* @author tanran
* @date 2019年6月14日
* @version 1.0
*/
@Service
public class KuaiShouServiceImpl implements VideoService {
@Override
public VideoModel parseUrl(String url) {
// TODO Auto-generated method stub
VideoModel videoModel=new VideoModel();
/*HttpRequest request = HttpRequest.get(url);
String res = request.body();
System.out.println(res);*/
try {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Response response = okHttpClient.newCall(request).execute();
String result=response.body().string();
String photoId=TextUtil.getSubString(result, "\\\"photoId\\\":\\\"", "\\\"");
System.out.println(photoId);
url="https://api.kmovie.gifshow.com/rest/n/kmovie/app/photo/getPhotoById?WS&jjh_yqc&ws&photoId="+photoId;
request=new Request.Builder().url(url).build();
response=okHttpClient.newCall(request).execute();
result=response.body().string();
System.out.println(result);
videoModel.setName(JsonUtil.getJsonValue(result, "photo.caption"));
videoModel.setPlayAddr(JsonUtil.getJsonValue(result, "photo.mainUrl"));
videoModel.setCover(JsonUtil.getJsonValue(result, "photo.coverUrl"));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return videoModel;
}
public static void main(String[] args) {
System.out.println(new KuaiShouServiceImpl().parseUrl("http://m.chenzhongtech.com/s/mhn5haAq/"));
}
}
PiPiXServiceImpl
package com.rjt.video.service.impl;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.stereotype.Service;
import com.rjt.common.util.JsonUtil;
import com.rjt.video.model.VideoModel;
import com.rjt.video.service.VideoService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* @comment
* @author tanran
* @date 2019年6月14日
* @version 1.0
*/
@Service
public class PiPiXServiceImpl implements VideoService{
@Override
public VideoModel parseUrl(String url) {
// TODO Auto-generated method stub
VideoModel videoModel=new VideoModel();
try {
Matcher matcher = Pattern.compile("\\d{19}").matcher(url);
if(matcher.find()) {
String itemId=matcher.group(0);
url="https://is.snssdk.com/bds/item/detail/?app_name=super&aid=1319&item_id="+itemId;
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Response response = okHttpClient.newCall(request).execute();
String result=response.body().string();
System.out.println(result);
videoModel.setName(JsonUtil.getJsonValue(result, "data.data.share.title"));
videoModel.setPlayAddr(JsonUtil.getJsonValue(result, "data.data.video.video_fallback.url_list[0].url"));
videoModel.setCover(JsonUtil.getJsonValue(result, "data.data.video.video_fallback.cover_image.url_list[0].url"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return videoModel;
}
public static void main(String[] args) {
System.out.println(new PiPiXServiceImpl().parseUrl("https://h5.pipix.com/item/6677539389557446916"));
}
}
VideoFactory
package com.rjt.video.service.impl;
import com.rjt.video.service.VideoService;
public class VideoFactory {
public static VideoService getVideo(String type)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
if (type.indexOf("douyin.com") != -1 || type.indexOf("iesdouyin.com") != -1 ) {
return DouyinServiceImpl.class.newInstance();
} else if (type.indexOf("huoshan.com") != -1 ) {
return HuoShanServiceImpl.class.newInstance();
} else if (type.indexOf("kuaishou.com") != -1 || type.indexOf("gifshow.com") != -1 || type.indexOf("chenzhongtech.com") != -1) {
return KuaiShouServiceImpl.class.newInstance();
} else if (type.indexOf("pipix.com") != -1) {
return PiPiXServiceImpl.class.newInstance();
} else if (type.indexOf("weishi.qq.com")!= -1 ) {
return WeiShiServiceImpl.class.newInstance();
}else if (type.indexOf("izuiyou.com") != -1) {
return ZuiYouServiceImpl.class.newInstance();
} else {
System.out.println("哎呀!找不到相应的实例化类啦!");
return null;
}
}
}
WeiShiServiceImpl
package com.rjt.video.service.impl;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.stereotype.Service;
import com.rjt.common.util.JsonUtil;
import com.rjt.video.model.VideoModel;
import com.rjt.video.service.VideoService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* @comment
* @author tanran
* @date 2019年6月14日
* @version 1.0
*/
@Service
public class WeiShiServiceImpl implements VideoService {
@Override
public VideoModel parseUrl(String url) {
// TODO Auto-generated method stub
VideoModel videoModel=new VideoModel();
try {
Matcher matcher = Pattern.compile("\\w{17}").matcher(url);
if(matcher.find()) {
String feedId=matcher.group(0);
url="https://h5.qzone.qq.com/webapp/json/weishi/WSH5GetPlayPage?feedid="+feedId;
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Response response = okHttpClient.newCall(request).execute();
String result=response.body().string();
System.out.println(result);
videoModel.setName(JsonUtil.getJsonValue(result, "data.feeds[0].material_desc"));
videoModel.setPlayAddr(JsonUtil.getJsonValue(result, "data.feeds[0].video_url"));
videoModel.setCover(JsonUtil.getJsonValue(result, "data.feeds[0].images[0].url"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return videoModel;
}
public static void main(String[] args) {
System.out.println(new WeiShiServiceImpl().parseUrl("https://h5.weishi.qq.com/weishi/feed/70hxLzG4U1HlJhtUj/wsfeed?_proxy=1&_wv=1&wxplay=1&id=70hxLzG4U1HlJhtUj&spid=h5&reqseq=311149196&cover=http%3A%2F%2Fpic640.weishi.qq.com%2Fab4595845793449cb17a6df26bb6cover.jpg&bgSize=cover&image=4595845793449cb17a6df26bb6cover.&chid=100000001&pkg=3670&attach=cp_reserves3_4001&qua=v1_ht5_qz_3.0.0_001_idc_new&xflag=2130706433a1685170850b1560498191"));
}
}
ZuiYouServiceImpl
package com.rjt.video.service.impl;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.stereotype.Service;
import com.rjt.common.util.JsonUtil;
import com.rjt.video.model.VideoModel;
import com.rjt.video.service.VideoService;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* @comment
* @author tanran
* @date 2019年6月14日
* @version 1.0
*/
@Service
public class ZuiYouServiceImpl implements VideoService {
@Override
public VideoModel parseUrl(String url) {
// TODO Auto-generated method stub
VideoModel videoModel=new VideoModel();
/*HttpRequest request = HttpRequest.get(url);
String res = request.body();
System.out.println(res);*/
try {
Matcher matcher = Pattern.compile("\\d{9}").matcher(url);
if(matcher.find()) {
String pid=matcher.group(0);
url="https://share.izuiyou.com/api/post/detail";
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), "{\"pid\":"+pid+"}");
Request request = new Request.Builder().url(url).post(body).build();
Response response = okHttpClient.newCall(request).execute();
String result=response.body().string();
System.out.println(result);
String id=JsonUtil.getJsonValue(result, "data.post.imgs[0].id");
videoModel.setName(JsonUtil.getJsonValue(result, "data.post.content"));
videoModel.setCover(JsonUtil.getJsonValue(result, "data.post.videos."+id+".cover_urls[0]"));
videoModel.setPlayAddr(JsonUtil.getJsonValue(result, "data.post.videos."+id+".url"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return videoModel;
}
public static void main(String[] args) {
System.out.println(new ZuiYouServiceImpl().parseUrl("https://h5.izuiyou.com/detail/122595613?zy_to=applink&to=applink"));
}
}
VideoService.java
package com.rjt.video.service;
import com.rjt.video.model.VideoModel;
/**
* @comment
* @author tanran
* @date 2019年7月24日
* @version 1.0
* vx:1661163785
*/
public interface VideoService {
public VideoModel parseUrl(String url);
}
package com.rjt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
在resources下的application.yml文件中我指定了程序运行的服务器端口80
yml配置文件的好处在于让配置更加富有层次感,在实际开发过程中通常都选择yml配置文件。
注意:
1、yml配置文件的:后面必须跟一个空格,否则这个配置就会报错
2、配置yml文件时,需要额外的注意属性的缩进,如果多缩进或者少缩进会改变配置信息的含义
接下来就是pom.xml 配置文件
4.0.0
rjt
jar
dyjx
org.springframework.boot
spring-boot-starter-parent
1.5.6.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
org.springframework.boot
spring-boot-starter-test
test
com.bladejava
blade-kit
1.2.9-alpha
com.alibaba
fastjson
1.2.47
org.jsoup
jsoup
1.11.3
com.squareup.okhttp3
okhttp
3.14.2
${project.artifactId}
org.springframework.boot
spring-boot-maven-plugin
到这里项目代码已经完成 接下来运行 主程序类MainApplication
这里解释下:使用@SpringBootApplication
来标注一个主程序类,表明这是一个SpringBoot应用,
Run一下 成功了
可以看到启动项目的端口为80
本地实现功能:
我这里用html+css已经写好了网页
接下来我们输入视频地址来实现解析
还可以部署到云服务器,目前还在调试 ,祝我成功吧 。。。。