cky-image-public
Version:
public image to public host
50 lines (44 loc) • 1.36 kB
JavaScript
const fs = require('fs');
const request = require('request').defaults({
headers: {
accept: '*/*',
'accept-language': 'en-US,en;q=0.9,vi;q=0.8,fr-FR;q=0.7,fr;q=0.6,la;q=0.5',
'content-type': 'multipart/form-data',
dnt: 1,
origin: 'https://www.pinterest.com',
referer: 'https://www.pinterest.com/',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'
}
})
let helper = require('../helper');
class Pinterest {
constructor (options = {}) {
let { token } = options;
this.token = token;
}
upload (imgPath, callback) {
if (!this.token) return callback('ENOTOKEN', 'Invalid Token');
request({
url: 'https://www.pinterest.com/upload-image/',
method: 'POST',
headers: {
cookie: `csrftoken=${this.token};`,
'x-csrftoken': this.token
},
formData: {
img: {
value: fs.createReadStream(imgPath),
options: {
filename: imgPath,
contentType: 'image/jpeg'
}
}
}
}, (err, response, body) => {
if (err || (response && response.statusCode != 200)) return callback('EUPLOADPINTEREST', err);
body = helper.safeParse(body);
return callback(null, body);
});
}
}
module.exports = Pinterest;