一、封装一个函数来识别要解析的类型
1 // 获取类型 2get_type(){ 3 if(this.url.match(/http[s]?:\/\/v\.douyin\.com\/[^ ]+/) != null){ 4 console.log("识别到【dy】链接") 5 return "dy" 6 } 7 else if(this.url.match(/http[s]?:\/\/v\.kuaishou.com\/[^ ]+/) != null){ 8 console.log("识别到【ks】链接") 9 return "ks" 10 } 11 else if(this.url.match(/http[s]?:\/\/xhslink\.com\/[^ ]+/) != null){ 12 console.log("识别到【xhs】链接") 13 return "xhs" 14 } 15 else{ 16 console.log("未识别到链接类型,请输入正确的链接") 17 return null 18 } 19}
二、在初始化方法中写入本实例共用的数据
1 // 初始化方法 2constructor() { 3 this.token = "Z1QljZOZiT4NTG" // token 4 5 // 请求地址数组对象 6 this.req_urls = { 7 dy: "http://api.txapi.cn/v1/parse_short_video/dy", 8 ks: "http://api.txapi.cn/v1/parse_short_video/ks", 9 xhs: "http://api.txapi.cn/v1/parse_short_video/xhs", 10 } 11 12 this.url = '' // 要解析的地址 13 this.type = '' // 用来存储识别到的类型 14}
三、封装一个“万能解析”的方法
1 // 万能解析 2parse_video(){ 3 axios({ 4 url: this.req_urls[this.type], 5 method: 'POST', 6 headers: { 7 'Content-Type': "application/x-www-form-urlencoded" 8 }, 9 responseType: 'json', 10 data: { 11 token: this.token, 12 url: this.url 13 } 14 }) 15 .then(resp => { 16 // 校验是否解析成功 17 if(resp.data.code != 200 && resp.data.msg != "OK"){ 18 console.log("解析失败") 19 } 20 else{ 21 // 获取到解析后的数据 22 const data = resp.data.data 23 console.log(data) 24 25 var type = data.type // 类型:1视频 2图片集 26 var title = data.title // 标题 27 var cover_url = data.cover_url // 封面地址 28 var video_url = data.video_url // 无水印视频地址 29 var imgs = data.imgs // 无水印图片数组 30 } 31 }) 32} 33废话不多说 直接上完整代码👇 34const axios = require('axios') 35 36 37class Parse{ 38 // 初始化方法 39 constructor() { 40 this.token = "Z1QljZOZiT4NTG" // token 41 42 // 请求地址数组对象 43 this.req_urls = { 44 dy: "http://api.txapi.cn/v1/parse_short_video/dy", 45 ks: "http://api.txapi.cn/v1/parse_short_video/ks", 46 xhs: "http://api.txapi.cn/v1/parse_short_video/xhs", 47 } 48 49 this.url = '' // 要解析的地址 50 this.type = '' // 用来存储识别到的类型 51 } 52 53 // 万能解析 54 parse_video(){ 55 axios({ 56 url: this.req_urls[this.type], 57 method: 'POST', 58 headers: { 59 'Content-Type': "application/x-www-form-urlencoded" 60 }, 61 responseType: 'json', 62 data: { 63 token: this.token, 64 url: this.url 65 } 66 }) 67 .then(resp => { 68 // 校验是否解析成功 69 if(resp.data.code != 200 && resp.data.msg != "OK"){ 70 console.log("解析失败") 71 } 72 else{ 73 // 获取到解析后的数据 74 const data = resp.data.data 75 console.log(data) 76 77 var type = data.type // 类型:1视频 2图片集 78 var title = data.title // 标题 79 var cover_url = data.cover_url // 封面地址 80 var video_url = data.video_url // 无水印视频地址 81 var imgs = data.imgs // 无水印图片数组 82 } 83 }) 84 } 85 86 // 获取类型 87 get_type(){ 88 if(this.url.match(/http[s]?:\/\/v\.douyin\.com\/[^ ]+/) != null){ 89 console.log("识别到【dy】链接") 90 return "dy" 91 } 92 else if(this.url.match(/http[s]?:\/\/v\.kuaishou.com\/[^ ]+/) != null){ 93 console.log("识别到【ks】链接") 94 return "ks" 95 } 96 else if(this.url.match(/http[s]?:\/\/xhslink\.com\/[^ ]+/) != null){ 97 console.log("识别到【xhs】链接") 98 return "xhs" 99 } 100 else{ 101 console.log("未识别到链接类型,请输入正确的链接") 102 return null 103 } 104 } 105 106 // 使用正则区分要解析的链接是哪个平台的【dy、ks、xhs】 107 run(url){ 108 // 1、把url保存给实例变量【方便后期使用】 109 this.url = url 110 111 // 1、获取类型 112 this.type = this.get_type(); 113 if(!this.type){ 114 return 115 } 116 117 // 2、调用万能解析 118 this.parse_video() 119 } 120} 121 122 123if(__filename === process.mainModule.filename) { 124 // new一个Parse对象 125 const p = new Parse() 126 127 // 调用run方法 128 p.run("https://v.douyin.com/hoDBW9H") 129 p.run("https://v.kuaishou.com/C75B2q") 130 p.run("http://xhslink.com/fKihbj") 131}