cky-image-public
Version:
public image to public host
154 lines (131 loc) • 3.76 kB
JavaScript
// 546c25a59c58ad7
const request = require('request').defaults({
jar: true,
headers: {
'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',
},
followRedirect: false,
rejectUnauthorized: false,
strictSSL: false,
});
const fs = require('fs');
let helper = require('../helper');
const URL_IMGUR = 'https://imgur.com';
const URL_API_IMGUR = 'https://api.imgur.com';
class Imgur {
constructor (options = {}) {
this.client_id = options.client_id;
this.user_name = options.user_name;
this.access_token = options.access_token;
this.cookie_str = options.cookie_str;
this.cookie = request.jar();
if (this.access_token) {
let cookie = request.cookie(`accesstoken=${this.access_token}`);
this.cookie.setCookie(cookie, URL_IMGUR);
this.cookie.setCookie(cookie, URL_API_IMGUR);
}
}
homepage (callback) {
request({
url: 'https://imgur.com',
method: 'GET',
headers: {
authority: 'imgur.com',
'upgrade-insecure-requests': 1,
'dnt': 1,
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
referer: 'https://imgur.com/',
'accept-encoding': 'gzip, deflate, br',
'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'
},
jar: this.cookie,
}, (err, response, body) => {
return callback(err, body);
});
}
me (callback) {
this.homepage(() => {
console.log('cookie=', this.cookie);
request({
url: `https://api.imgur.com/3/account/me?IMGURPLATFORM=web&client_id=${this.client_id}`,
method: 'GET',
headers: {
},
jar: this.cookie
}, (err, response, body) => {
return callback(err, body);
});
})
}
about (callback) {
console.log('cookie=', this.cookie);
this.homepage(() => {
console.log('\n\ncookie=', this.cookie);
request({
url: `https://api.imgur.com/account/v1/accounts/${this.user_name}?client_id=${this.client_id}`,
method: 'GET',
jar: this.cookie
}, (err, response, body) => {
return callback(err, body);
});
});
}
upload (imgPath, callback) {
let formData = {
Filedata: {
value: fs.createReadStream(imgPath),
options: {
filename: imgPath,
contentType: 'image/jpeg'
}
},
resize: -1,
new_album_id: 0,
}
let options = {
url: `https://imgur.com/upload`,
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
'DNT': 1,
'Origin': `https://${this.user_name ? this.user_name + '.' : ''}imgur.com`,
'Referer': `https://${this.user_name ? this.user_name + '.' : ''}imgur.com`,
},
jar: this.cookie,
formData
};
request(options, (err, response, body) => {
return callback(err, helper.safeParse(body));
});
}
}
module.exports = Imgur;
/*
{
"data": {
"hashes": [
"3WsklMF"
],
"hash": "3WsklMF",
"deletehash": "tbHjVPvrYWgmSc2",
"ticket": false,
"album": false,
"edit": false,
"gallery": null,
"poll": false,
"animated": false,
"height": 540,
"width": 720,
"ext": ".jpg",
"msid": "4b8ab60485487cea4e733e46d420aed2"
},
"success": true,
"status": 200
}
image link: https://imgur.com/3WsklMF
direct link: https://i.imgur.com/3WsklMF.jpg
markdown link: [Imgur](https://i.imgur.com/3WsklMF.jpg)
html: <a href="https://imgur.com/3WsklMF"><img src="https://i.imgur.com/3WsklMF.jpg" title="source: imgur.com" /></a>
bbcode: [img]https://i.imgur.com/3WsklMF.jpg[/img]
linked bbcode: [url=https://imgur.com/3WsklMF][img]http://i.imgur.com/3WsklMF.jpg[/img][/url]
*/