1. Encapsulate a function to identify the type to be parsed
// Get typeget_type(){ if((/http[s]?:\/\/v\.douyin\.com\/[^ ]+/) != null){ ("Recognize the [dy] link") return "dy" } else if((/http[s]?:\/\/v\.\/[^ ]+/) != null){ ("Recognize the [ks] link") return "ks" } else if((/http[s]?:\/\/xhslink\.com\/[^ ]+/) != null){ ("Recognize the [xhs] link") return "xhs" } else{ ("The link type is not recognized, please enter the correct link") return null } }
2. Write the data shared by this instance in the initialization method
// Initialization methodconstructor() { = "Z1QljZOZiT4NTG" // token // Request address array object this.req_urls = { dy: "/v1/parse_short_video/dy", ks: "/v1/parse_short_video/ks", xhs: "/v1/parse_short_video/xhs", } = '' // Address to be parsed = '' // Used to store the identified type}
3. Encapsulate a "universal analysis" method
// Universal Analysisparse_video(){ axios({ url: this.req_urls[], method: 'POST', headers: { 'Content-Type': "application/x-www-form-urlencoded" }, responseType: 'json', data: { token: , url: } }) .then(resp => { // Check whether the parsing is successful if( != 200 && != "OK"){ ("Resolution failed") } else{ // Get parsed data const data = (data) var type = // Type: 1 video 2 picture collection var title = // Title var cover_url = data.cover_url // Cover address var video_url = data.video_url // No watermark video address var imgs = // No watermark image array } }) }
Don't say much nonsense, just add the complete code👇
const axios = require('axios') class Parse{ // Initialization method constructor() { = "Z1QljZOZiT4NTG" // token // Request address array object this.req_urls = { dy: "/v1/parse_short_video/dy", ks: "/v1/parse_short_video/ks", xhs: "/v1/parse_short_video/xhs", } = '' // Address to be parsed = '' // Used to store the identified type } // Universal Analysis parse_video(){ axios({ url: this.req_urls[], method: 'POST', headers: { 'Content-Type': "application/x-www-form-urlencoded" }, responseType: 'json', data: { token: , url: } }) .then(resp => { // Check whether the parsing is successful if( != 200 && != "OK"){ ("Resolution failed") } else{ // Get parsed data const data = (data) var type = // Type: 1 video 2 picture collection var title = // Title var cover_url = data.cover_url // Cover address var video_url = data.video_url // No watermark video address var imgs = // No watermark image array } }) } // Get type get_type(){ if((/http[s]?:\/\/v\.douyin\.com\/[^ ]+/) != null){ ("Recognize the [dy] link") return "dy" } else if((/http[s]?:\/\/v\.\/[^ ]+/) != null){ ("Recognize the [ks] link") return "ks" } else if((/http[s]?:\/\/xhslink\.com\/[^ ]+/) != null){ ("Recognize the [xhs] link") return "xhs" } else{ ("The link type is not recognized, please enter the correct link") return null } } // Use regular rules to distinguish which platform the link to be parsed is from [dy, ks, xhs] run(url){ // 1. Save the url to the instance variable [Easy to later use] = url // 1. Obtain type = this.get_type(); if(!){ return } // 2. Call universal analysis this.parse_video() } } if(__filename === ) { // new a Parse object const p = new Parse() // Call the run method ("/hoDBW9H") ("/C75B2q") ("/fKihbj") }
Supplement: In addition to using the axios network to request third-party platform interaction, third-party libraries can also be used to implement the dewatermarking function. For example, using the jimp library, the example code is as follows:
const Jimp = require('jimp'); // Read the original image('').then(image => { // Read the watermark image ('').then(watermark => { // Get the width and height of the original image and watermark image const width = ; const height = ; const wmWidth = ; const wmHeight = ; // Calculate the width and height scaling ratio of the watermark const scale = width / wmWidth; // Zoom watermark image (scale); // Draw the watermark on the original image (watermark, 0, 0, { mode: Jimp.BLEND_SOURCE_OVER, opacitySource: 1, opacityDest: 1 }); // Save the processed picture (''); }); });