easy-yopmail
Version:
Easy-YOPMail (get mail address, inbox and read mail from YOPMail with nodeJS)
1 lines • 3.76 kB
JavaScript
;const constants=require("./constants"),cheerio=require("cheerio"),fs=require("fs"),https=require("https"),zlib=require("zlib");function request(a,b){let c=2<arguments.length&&arguments[2]!==void 0?arguments[2]:{},d=3<arguments.length&&arguments[3]!==void 0?arguments[3]:null;const e=new URL(b),f={hostname:e.hostname,port:443,path:"".concat(e.pathname).concat(e.search),method:a.toUpperCase(),...c};return new Promise(((b,c)=>{const e=https.request(f,(a=>{let c=[];const d=a.headers["content-encoding"];let e=a;"gzip"===d?e=a.pipe(zlib.createGunzip()):"deflate"===d&&(e=a.pipe(zlib.createInflate())),e.on("data",(a=>{c.push(a)})),e.on("end",(()=>{const d=Buffer.concat(c).toString();b({statusCode:a.statusCode,headers:a.headers,body:d})}))}));e.on("error",(a=>{console.log("Error:",a.message),c(a)})),f.body&&e.write(f.body),"POST"===a&&d&&e.write(d),e.end()}))}async function getCookiesAndYP(){const a=await request("GET",constants.BASE_URL);if(200===a.statusCode)try{const b=cheerio.load(a.body),c=b(constants.INPUT_YP).val(),d=a.headers["set-cookie"].map((a=>a.split(";")[0])).join("; "),e=a.body.match(constants.REGEX_LOCATION)[1],f=a.body.match(/\/ver\/(\d+\.\d+)\//)[1];return setLocation(e),{cookie:d,yp:c,ver:f}}catch(a){console.error(a.message)}else return console.error(constants.ERROR_GET_COOKIES),{cookie:null,yp:null}}function setLocation(a){return!!(a&&"string"==typeof a)&&void(fs.mkdirSync("../.cache",{recursive:!0}),fs.writeFileSync("../.cache/location",a))}function simplifyEmail(a){return-1<a.indexOf("@")?(a.split("@")[0]||"").toLowerCase():a.toLowerCase()}async function validateSearch(a){const b=constants.possibleKeys.some((b=>Object.keys(a).includes(b)||null));return b?Object.keys(a).reduce(((b,c)=>(constants.possibleKeys.includes(c)&&(b[c]=a[c]),b)),{}):(console.error(constants.ERROR_PARAMETERS_INVALID),{})}async function getYJ(a,b){const c=await request("GET",constants.WEBMAIL_JS_URL(b),{headers:{Cookie:a}});if(200===c.statusCode){const a=c.body.match(constants.REGEX_YJ);return a?a[1]:null}console.error(constants.ERROR_GET_YJ)}async function getTotalMails(a){const b=a.match(constants.REGEX_TOTAL_MAILS);let c=0;return b?c=b[1].split(",")[0]:console.log(constants.ERROR_GET_TOTAL_MAILS),+c}function shouldIncludeEmail(a,b){return Object.entries(b).every((b=>{let[c,d]=b;return"id"===c?a.id===d:"from"===c?a.from===d:"subject"===c?a.subject===d:!("timestamp"!==c)&&a.timestamp===d}))}function parseEmail(a){const b=cheerio.load(a),c=b(a).attr("id"),d=b(a).find(".lmh").text(),e=b(a).find(".lmf").text(),f=b(a).find(".lms").text();return{id:c,from:e,subject:f,timestamp:d}}function getDetailInboxFromPage(a,b){const c=cheerio.load(a),d=c(".m");return d.map(((a,b)=>parseEmail(b))).toArray().filter((a=>shouldIncludeEmail(a,b)))}async function fetchInboxPage(a,b,c,d,e,f){let g=constants.INBOX_URL(a,b,c,d,e),h=constants.INBOX_HTTP_CONFIG(f,a);return await request("GET",g,h)}async function detailInbox(a,b,c,d,e){let f=5<arguments.length&&arguments[5]!==void 0?arguments[5]:{},g=6<arguments.length&&arguments[6]!==void 0?arguments[6]:{};const h=await fetchInboxPage(a,b,c,d,1,e);if(200!==h.statusCode)return console.error(constants.ERROR_LOAD_PAGE),null;const i=h.body,j=await getTotalMails(i);let k={};f&&0<Object.keys(f).length&&(k=await validateSearch(f));let l=1,m=!0,n={};const o=[];for(;m&&(!0===g.GET_ALL_MAILS||1===l);){const d=1===l?i:(await fetchInboxPage(a,b,c,l,e)).data,f=getDetailInboxFromPage(d,k);n["page_".concat(l)]=f.length,o.push(...f),l*15>=j?m=!1:l+=1}return{settings:g,search:k,totalInbox:j,totalPages:Math.ceil(j/15),mailFromPage:n,totalGetMails:o.length,inbox:o}}module.exports={getCookiesAndYP:getCookiesAndYP,getYJ:getYJ,detailInbox:detailInbox,simplifyEmail:simplifyEmail,request:request};