jasp402 / Easy-YOPmail

📮 YOPmail emails manager from an NPM NodeJS library
https://jasp402.github.io/Easy-YOPmail/starter-topic.html
18 stars 3 forks source link

Error getting cookies when reading emails #22

Closed iam-alex-p closed 2 weeks ago

iam-alex-p commented 1 month ago

Hello,

I am trying to read emails from YOPMail address using following code:

const easyYopmail = require('easy-yopmail');

const AUTOMATION_EMAIL = 'my.yop.email@yopmail.com';

easyYopmail.getInbox(AUTOMATION_EMAIL).then(emails => {
    emails.inbox.forEach(msg => {
        easyYopmail.readMessage(AUTOMATION_EMAIL, msg['id']).then(objMsg => {
            console.log(objMsg);
        });
    });
});

When I run this code, I am experiencing following error:

PS D:\Development\Projects\Sandbox\YOPMailUtility> node app.js
D:\Development\Projects\Sandbox\YOPMailUtility\node_modules\easy-yopmail\src\core.js:1
"use strict";function _defineProperty(a,b,c){return b=_toPropertyKey(b),b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function _toPropertyKey(a){var b=_toPrimitive(a,"string");return"symbol"==typeof b?b:b+""}function _toPrimitive(a,b){if("object"!=typeof a||!a)return a;var c=a[Symbol.toPrimitive];if(void 0!==c){var d=c.call(a,b||"default");if("object"!=typeof d)return d;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===b?String:Number)(a)}const fs=require("fs"),zlib=require("zlib"),https=require("https"),cheerio=require("cheerio"),constants=require("./constants");class Core{constructor(){_defineProperty(this,"validateSelectAttrEQ",((a,b,c,d,e)=>(b&&0===a(b).length?e.push("Warning - The selector <".concat(b,"> is not found in the html.")):d>a(b).length&&e.push("Warning - The eq(".concat(d,") is greater than the number of elements found in the selector ").concat(b,".")),e))),_defineProperty(this,"getContentBySelectorForTxt",((a,b,c,d,e)=>{let f=[];return e=this.validateSelectAttrEQ(a,b,c,d,e),b||(f+=a(this.constants.S_MAIL).text().replace(/\s+|\n/gm," ").trim()),b&&!d&&a(b).each(((b,c)=>{f.push(a(c).text().trim())})),b&&d&&(f+=a(b).eq(parseInt(d,10)).text().replace(/\s+|\n/gm," ").trim(),!f&&e.push("Warning - No text found with selector <".concat(b,"> and .eq(").concat(d,")"))),{info:e,content:f}})),_defineProperty(this,"getContentBySelectorForHTML",((a,b,c,d,e)=>{let f;if(e=this.validateSelectAttrEQ(a,b,c,d,e),!b)return f=a.html(this.constants.S_MAIL),{info:e,content:f};if(f=(()=>c?void 0!==d&&null!==d?a(b).eq(parseInt(d,10)).attr(c):a(b).map(((b,d)=>a(d).attr(c))).toArray():void 0!==d&&null!==d?a(b).eq(parseInt(d,10)).html():a(b).map(((b,c)=>a.html(c))).toArray())(),!f||Array.isArray(f)&&!f.length){let a="Element not found";a+=" with selector ".concat(b),a+=c?" and attribute ".concat(c):"",a+=void 0===d?"":" and eq ".concat(d),e.push(a)}return{info:e,content:f}})),this.constants=constants,this.cwd=process.cwd()}async 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;b||this.handleError({customMessage:this.constants.ERROR_URL_NOT_FOUND});const e=(a,b)=>{const c=Buffer.concat(b).toString();return{statusCode:a.statusCode,headers:a.headers,body:c}},f=(a,b,c)=>{const d=a.headers["content-encoding"];let f=a;"gzip"===d?f=a.pipe(zlib.createGunzip()):"deflate"===d&&(f=a.pipe(zlib.createInflate()));const g=[];f.on("data",(a=>g.push(a))),f.on("end",(()=>b(e(a,g)))),f.on("error",(a=>c(a)))},g=((a,b,c)=>{const d=new URL(a);return{hostname:d.hostname,port:443,path:"".concat(d.pathname).concat(d.search),method:b.toUpperCase(),...c}})(b,a,c),h=(a,b)=>new Promise(((c,d)=>{const e=https.request(a,(a=>{f(a,c,d)}));e.on("error",(a=>d(a))),a.body&&e.write(a.body),"POST"===a.method&&b&&e.write(b),e.end()}));try{return await h(g,d)}catch(a){this.handleError({customMessage:"Request Error",error:a})}}async getHttpSettings(){try{const a=await this.request("GET",this.constants.URL_BASE);200!==a.statusCode&&this.handleError({customMessage:this.constants.ERROR_GET_COOKIES});const b=this.loadHTMl(a.body),c=b(this.constants.S_INPUT_YP).val(),d=a.headers["set-cookie"].map((a=>a.split(";")[0])).join("; "),e=a.body.match(this.constants.REGEX_LOCATION)[1],f=a.body.match(this.constants.REGEX_VER)[1],g=await this.request("GET",this.constants.URL_WEBMAIL_JS(f),{headers:{Cookie:d}});200!==g.statusCode&&this.handleError({customMessage:this.constants.ERROR_GET_YJ});const h=g.body.match(this.constants.REGEX_YJ),i=h?h[1]:null,j=this.timeStampInSeconds(),k=this.timeStampInSeconds(),l=this.obtenerYTime(j);return{location:e,version:f,cookie:d,yp:c,yj:i,T:j,RT:k,ytime:l}}catch(a){return this.handleError({customMessage:this.constants.ERROR_GET_COOKIES,error:a}),null}}async detailInbox(a,b,c,d){const{location:e,version:f,cookie:g,yp:h,yj:i,T:j,RT:k,ytime:l}=d,m=15;let n=1,o=!0,p={},q=0,r=[],s={};const t=async(a,b,c,d,e,f,g)=>{let h=constants.URL_INBOX(a,b,c,d,f,g),i=constants.HEADERS_INBOX(e,a,g,j,k,l);const m=await this.request("GET",h,i);return 200!==m.statusCode&&this.handleError({customMessage:constants.ERROR_LOAD_PAGE}),m};b&&0<Object.keys(b).length&&(s=this.validateSearch(b));const u=await t(a,h,i,f,g,1,e),v=u.body,w=this.getTotalMails(v);for(;o;){const b=1===n?v:(await t(a,h,i,f,g,n,e)).body,d=this.getDetailInboxFromPage(b,s,n);p["page_".concat(n)]=d.length,q=n,r.push(...d),n*m>=w||n>=c.LIMIT?o=!1:n+=1}return{mail:"".concat(a,"@yopmail.com"),settings:c,filteredSearch:s,pageCount:Math.ceil(w/m),totalEmails:w,emailsPerPageCount:p,exploredPageCount:q,fetchedEmailCount:r.length,inbox:r}}getDetailInboxFromPage(a,b,c){const d=this.loadHTMl(a),e=d(".m");return e.map(((a,b)=>this.parseEmail(b,c))).toArray().filter((a=>this.shouldIncludeEmail(a,b)))}loadHTMl(a){return cheerio.load(a)}getTotalMails(a){const b=a.match(constants.REGEX_TOTAL_MAILS);let c=0;return b&&(c=b[1].split(",")[0]),+c}saveEmailHTML(a,b,c){let d="".concat(this.cwd,"/").concat(a);fs.existsSync(d)||fs.mkdirSync(d,{recursive:!0}),fs.writeFileSync("".concat(d,"/emailPage_").concat(c,".html"),b,(function(a){if(a)return new Error(a)}))}validateSearch(a){const b=["id","from","subject","timestamp"],c=b.some((b=>Object.keys(a).includes(b)||null));return c||this.handleError({customMessage:this.constants.ERROR_PARAMETERS_INVALID,error:new Error}),Object.keys(a).reduce(((c,d)=>(b.includes(d)&&(c[d]=a[d]),c)),{})}parseEmail(a,b){const c=cheerio.load(a),d=c(a).attr("id"),e=c(a).find(".lmh").text(),f=c(a).find(".lmf").text(),g=c(a).find(".lms").text();return{id:d,from:f,subject:g,timestamp:e,page:b}}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}))}obtenerYTime(a){const b=new Date(1e3*a),c=b.getHours(),d=b.getMinutes();return"".concat(c,":").concat(d)}timeStampInSeconds(){return Math.floor(Date.now()/1e3)}simplifyEmail(a){return-1<a.indexOf("@")?(a.split("@")[0]||"").toLowerCase():a.toLowerCase()}validateReadMessageOpt(a){const{format:e="txt",selector:b,attribute:c,pathToSave:d}={...{format:"txt",selector:null,attribute:null,pathToSave:null},...a};a={format:e.toLowerCase(),selector:b,attribute:c,pathToSave:d,eq:null};let f=[];const g=(a,b,c)=>{["html","txt"].includes(b.toLowerCase())||(c.push("Warning - the format ".concat(b," is incorrect, the default format is TXT")),a.format="txt")},h=(a,b,c)=>{const d=null===b||void 0===b?void 0:b.match(/^(.+)\.eq\((\d+)\)$/);if(d){const[,b,c]=d;a.eq=c,a.selector=b}else if(null!==b&&void 0!==b&&b.includes(".eq(")){const[d]=b.split(".eq(");a.selector=d,a.eq=null,c.push('Warning - the ".eq()" value is invalid, the default eq is null')}},i=(a,b,c,d)=>{c&&!b?(d.push("Warning - the attribute requires a selector, the default attribute is null"),a.attribute=null):c&&"string"!=typeof c&&(d.push("Warning - the attribute ".concat(c," is incorrect, the default attribute is empty")),a.attribute=null)},j=(a,b,c)=>{b&&"string"!=typeof b&&(c.push("Warning - the pathToSave ".concat(b," is incorrect, the default pathToSave is empty")),a.pathToSave=null)};return g(a,e,f),((a,b,c)=>{b&&"string"!=typeof b?(c.push("Warning - the selector ".concat(b," is incorrect, the default selector is null")),a.selector=null):h(a,b,c)})(a,b,f),i(a,b,c,f),j(a,d,f),{opt:a,info:f}}handleError(){let{customMessage:b=this.constants.ERROR_LOAD_PAGE,error:a}=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};throw null!==a&&void 0!==a&&a.message&&(b+="\n - ".concat(a.message)),new Error(b)}}module.exports=Core;

Error: Error getting cookies
 - Error getting cookies
    at EasyYopmail.handleError (D:\Development\Projects\Sandbox\YOPMailUtility\node_modules\easy-yopmail\src\core.js:1:7631)
    at EasyYopmail.getHttpSettings (D:\Development\Projects\Sandbox\YOPMailUtility\node_modules\easy-yopmail\src\core.js:1:3787)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async EasyYopmail.getInbox (D:\Development\Projects\Sandbox\YOPMailUtility\node_modules\easy-yopmail\index.js:1:711)

Node.js v22.5.1

I am running easy-yopmail v5.1.0. Could it be a configuration issue? Any advice will be appreciated!

Sincerely, Aleksei

jasp402 commented 1 month ago

Hi, I've tried to replicate the error. The truth is that I don't see the cookie error, but I did see that the code you provided could be improved so that when the inbox is empty it stops the process and doesn't try to make an unnecessary loop. I did a test by leaving an email and your code worked correctly.

const easyYopmail = require('easy-yopmail');

const AUTOMATION_EMAIL = 'my.yop.email@yopmail.com';

easyYopmail.getInbox(AUTOMATION_EMAIL).then(emails => {
    if (emails.inbox.length) {
        console.log('You have messages in your inbox');
        emails.inbox.forEach(msg => {
            easyYopmail.readMessage(AUTOMATION_EMAIL, msg['id']).then(objMsg => {
                console.log(objMsg);
            });
        });
    } else {
        console.log('You have no messages in your inbox');
    }
});

image

Please validate what I'm telling you. And if you can, let me know so I can close this Issue.

iam-alex-p commented 1 month ago

Hello @jasp402,

Thank you for the quick response and for improving my code!

Unfortunately, I am still experiencing the same error getting Cookies. Can you please tell me what version of Node.js you are using to run the code?

jasp402 commented 1 month ago

image

It is possible that it is a re-Captcha problem. Can you go directly to the page and validate if it asks you for a captcha to see the email? If not, I can't think of anything else that could be affecting your query.

iam-alex-p commented 1 month ago

@jasp402, it does not ask me for a Captcha right away, when I am getting access to my email. However, it asks for Captcha from time to time, when I am navigating through inboxes. So, yeah... Cookies issue could be related to the Captcha...

jasp402 commented 1 month ago

In this case I don't see how they can be related. The cookie is not directly related to the re-captcha, it simply gets an empty message, but does not generate an error. I have tested the code you have provided me, I see it working correctly. Even using an online platform to test code online such as https://codesandbox.io/

image

iam-alex-p commented 1 month ago

@jasp402 thank you for looking into that! I forgot to mention that at the very beginning when I have just tried to read inboxes from that YOPMail account with your library, it worked for me. But than at some point, I started to experience that Cookie error without changing the code. So, really tricky issue...