UNPKG

cky-image-public

Version:

public image to public host

152 lines (126 loc) 4.28 kB
const fs = require('fs'); const _ = require('lodash'); const async = require('async'); const request = require('request').defaults({ headers: { Origin: 'https://postimages.org', Accept: 'application/json', 'Cache-Control': 'no-cache', 'X-Requested-With': 'XMLHttpRequest', 'Referer': 'https://postimages.org/', DNT: 1, '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' } }) const cheerio = require('cheerio'); let helper = require('../helper'); function rand_string (e) { for (var t = "", i = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", n = 0; n < e; n++) t += i.charAt(Math.floor(Math.random() * i.length)); return t; } let _parse = ($, script, type) => { // token= // var text = script.innerText // var sub = text.substr(text.indexOf('token')) // sub = sub.substr(0, sub.indexOf('")')) // sub = sub.substr(sub.lastIndexOf('"')) if (!script) return ''; let text = $(script).html(); if (!text || text.length == 0) return ''; let sub = text.substr(text.indexOf(type)); sub = sub.substr(0, sub.indexOf('")')); sub = sub.substr(sub.lastIndexOf('"') + 1); return sub; } class Postimg { constructor (options = {}) { } upload (imgPath, callback) { async.waterfall([ (next) => { request({ url: 'https://postimages.org', method: 'GET' }, (err, response, body) => { if (err || !body) return next('EGETHOMEPAGE', err); let $ = cheerio.load(body); let scripts = $('script'); let data = null; _.forEach(scripts, (s) => { let innerText = $(s).html(); if (innerText.indexOf('token') > -1) { data = s; return false; } }) if (!data) return next('EPARSETOKEN'); let now = new Date(); let upload_session = rand_string(32); let session_upload = now.getTime(); let ui = `[24,1440,900,"true","","","${now.toLocaleString()}"]`; let token = _parse($, data, 'token'); let upload_referer = _parse($, data, 'upload_referer'); let options = { url: 'https://postimages.org/json/rr', method: 'POST', formData: { file: { value: fs.createReadStream(imgPath), options: { filename: imgPath, contentType: 'image/jpeg' } }, session_upload, ui, gallery: '', token, upload_session, numfiles: 1, upload_referer } } return next(null, options); }); }, (options, next) => { request(options, (err, response, body) => { if (err || (response && response.statusCode != 200)) return next('EUPLOADPOSTIMG', err); body = helper.safeParse(body); if (!body) return next('EPARSEBODY', body); /* { "status": "error", "error": "Forbidden" } */ /*{"status":"OK","url":"https://postimg.cc/MvyNB6C3/4afce76e"}*/ if (body.status && body.status == 'error') return callback('EUPLOAD', body); return next(null, body); }); }, // get direct link (result, next) => { request({ url: result.url, method: 'GET' }, (err, response, body) => { let $ = cheerio.load(body); let link = $('#code_html').attr('value'); let linkDirect = $('#code_direct').attr('value'); let linkRemove = $('#code_remove').attr('value'); let thumbnail = $('#code_web_thumb').attr('value'); let $$ = cheerio.load(thumbnail); let linkThumbnail = $$('img').attr('src'); result['link'] = link; result['linkDirect'] = linkDirect; result['linkRemove'] = linkRemove; result['linkThumbnail'] = linkThumbnail; return next(null, result); }); } ], callback); } } module.exports = Postimg;