diff --git a/package.json b/package.json index 181486f..787371d 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,9 @@ { "name": "easy-yopmail", - "version": "4.0.24", + "version": "4.1.0", "description": "Easy-YOPmail (get mail address, inbox and read mail from YOPmail with nodeJS)", - "main": "dist/index.js", + "main": "scr/index.js", "scripts": { - "build": "babel src --out-dir dist", "release": "standard-version", "test": "mocha src/test.spec.js --timeout 300000" }, @@ -33,20 +32,12 @@ }, "homepage": "https://jasp402.gitbook.io/easy-yopmail/", "devDependencies": { - "@babel/cli": "^7.21.5", - "@babel/core": "^7.21.8", - "@babel/plugin-proposal-class-properties": "^7.18.6", - "@babel/plugin-proposal-object-rest-spread": "^7.20.7", - "@babel/plugin-proposal-optional-chaining": "^7.21.0", - "@babel/preset-env": "^7.23.9", - "babel-preset-minify": "^0.5.2", - "babel-plugin-minify-mangle-names": "^0.5.1", "mocha": "10.1.0", "standard-version": "^9.5.0", "terser": "^5.17.1" }, "dependencies": { "axios": "^1.6.7", - "cheerio": "^1.0.0-rc.10" + "cheerio": "^1.0.0-rc.12" } } diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 0000000..2c75a67 --- /dev/null +++ b/src/constants.js @@ -0,0 +1 @@ +const a0_0x2b9437=a0_0x255f;(function(_0x4c3ad7,_0x2ea704){const _0xe663f3=a0_0x255f,_0x34e3d2=_0x4c3ad7();while(!![]){try{const _0x4a882f=-parseInt(_0xe663f3(0x163))/0x1+parseInt(_0xe663f3(0x193))/0x2+parseInt(_0xe663f3(0x180))/0x3+parseInt(_0xe663f3(0x14e))/0x4+-parseInt(_0xe663f3(0x19e))/0x5+parseInt(_0xe663f3(0x167))/0x6+-parseInt(_0xe663f3(0x155))/0x7;if(_0x4a882f===_0x2ea704)break;else _0x34e3d2['push'](_0x34e3d2['shift']());}catch(_0x44c3aa){_0x34e3d2['push'](_0x34e3d2['shift']());}}}(a0_0x4e20,0xf07f2));const path=require(a0_0x2b9437(0x183)),fs=require('fs');function timeStampInSeconds(){const _0x14c774=a0_0x2b9437;return Math[_0x14c774(0x191)](Date[_0x14c774(0x172)]()/0x3e8);}function obtenerYTime(_0xb5bed4){const _0x377c49=a0_0x2b9437,_0x34ceb4=_0xb5bed4*0x3e8,_0x569ab6=new Date(_0x34ceb4),_0x1d6db7=_0x569ab6[_0x377c49(0x158)](),_0x41a41e=_0x569ab6[_0x377c49(0x15f)]();return _0x377c49(0x154)+_0x1d6db7+':'+_0x41a41e+';';}function a0_0x4e20(){const _0xac37a6=['application/x-www-form-urlencoded','parameters\x20\x27_search\x27\x20keys\x20is\x20invalid,\x20try\x20again\x20with\x20(subject\x20ot\x20content)','&p=1&d=all&ctrl=','9027655hmAgQm','none','div.fl\x20>\x20div.ellipsis.nw.b.f18','Mozilla/5.0\x20(Windows\x20NT\x2010.0;\x20Win64;\x20x64)\x20AppleWebKit/537.36\x20(KHTML,\x20like\x20Gecko)\x20Chrome/112.0.0.0\x20Safari/537.36','Error\x20getting\x20version.\x20Error\x20loading\x20page','6394896uXAIHL','from',';\x20ywm=','Windows','resolve','.yopmail.com','ytime=','14812126hgXYOb','&p=','utf8','getHours','&r_c=&id=',';\x20__gads=ID=9a0b3cce9ff31bac:T=','keep-alive','input#yp','deleteInbox():\x20Error\x20delete\x20Inbox\x20page','tests','getMinutes','join','writeMessage():\x20Error\x20write\x20message',';\x20compte=','1904321tNNRcj','getInbox():\x20Error\x20getting\x20Inbox\x20page','&v=','yopmail.com','10646256StMksJ','parameters\x20\x27mail\x27\x20is\x20required','https://yopmail.com/ver/','/email-generator','&id=','navigate','subject','div.fl\x20>\x20div.md.text.zoom.nw.f18\x20>\x20span.ellipsis.b','\x22Not_A\x20Brand\x22;v=\x2299\x22,\x20\x22Chromium\x22;v=\x22121\x22,\x20\x22Google\x20Chrome\x22;v=\x22121\x22','/webmail.js','text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7','now','cors','deleteMessage():\x20Error\x20delete\x20message','archives',';\x20_ga=GA1.2.490358059.1683208319;\x20_gid=GA1.2.1148489241.1683208319;\x20__gads=ID=8e03875306c449c6-22790dab88df0074:T=1683208320:RT=1683208320:S=ALNI_MasidzVb7xQcb0qS7Hrb-gTpCYFkQ;\x20__gpi=UID=0000057b04df1c7f:T=1683208320:RT=','Error\x20getting\x20total\x20number\x20of\x20mails','mailContent.json','readMessage():\x20Error\x20read\x20message','iframe','&id=m','freeze','https://yopmail.com/','&r_c=&id=&ad=0','No\x20se\x20encontró\x20la\x20función','5520414QDXSHD','same-origin','document','path','empty','&ctrl=&yp=','getMail():\x20Error\x20getting\x20YOPmail\x20page','div.fl\x20>\x20div.md.text.zoom.nw.f18\x20>\x20span.ellipsis:last-child','/write?b=',':RT=','Error\x20getting\x20cookies','test@yopmail.com',';\x20_ga=GA1.2.503151223.1683393694;\x20_gid=GA1.2.1659462416.1683393694;\x20__gads=ID=1aef015d5c605919-2289ab29f27f0062:T=1683393694:RT=1683393694:S=ALNI_MYhXB3g-Ape8Bl2op45lGwvANkLFg;\x20__gpi=UID=000009f291461986:T=1683393694:RT=','gzip,\x20deflate,\x20br','&yj=','/wm','timestamp','floor','https://yopmail.com','3195412rHlvWJ','\x22Windows\x22','#mail','\x20_gat=1;',':S=ALNI_MZM6T_Gt5i9mVk7fSPYTS_lNnT05A;\x20compte=','es-ES,es;q=0.9','&p=1&d=','/inbox?login='];a0_0x4e20=function(){return _0xac37a6;};return a0_0x4e20();}function a0_0x255f(_0x46659a,_0x27dfd0){const _0x4e2057=a0_0x4e20();return a0_0x255f=function(_0x255fe6,_0x49dceb){_0x255fe6=_0x255fe6-0x14e;let _0x4349f2=_0x4e2057[_0x255fe6];return _0x4349f2;},a0_0x255f(_0x46659a,_0x27dfd0);}let location='es',pathLocation=path[a0_0x2b9437(0x160)](__dirname,'../.cache/location');fs['existsSync'](pathLocation)&&(location=fs['readFileSync'](pathLocation,a0_0x2b9437(0x157)));module['exports']=Object[a0_0x2b9437(0x17c)]({'BASE_URL':'https://yopmail.com/','GENERATOR_URL':a0_0x2b9437(0x17d)+location+a0_0x2b9437(0x16a),'READ_MAIL_URL':(_0x277d7d,_0x21bdf6)=>a0_0x2b9437(0x17d)+location+'/mail?b='+_0x277d7d+a0_0x2b9437(0x17b)+_0x21bdf6,'WRITE_MAIL_URL':_0x4f6842=>a0_0x2b9437(0x17d)+location+a0_0x2b9437(0x188)+_0x4f6842+a0_0x2b9437(0x16b),'DELETE_INBOX_URL':(_0x51b5bb,_0x2d35aa,_0x59c5ed,_0xcb0387,_0x9998a3)=>a0_0x2b9437(0x17d)+location+a0_0x2b9437(0x19a)+_0x51b5bb+a0_0x2b9437(0x19d)+_0x2d35aa+'&yp='+_0x59c5ed+a0_0x2b9437(0x18e)+_0xcb0387+a0_0x2b9437(0x165)+_0x9998a3+a0_0x2b9437(0x159),'DELETE_MESSAGE_URL':(_0x308fd1,_0x57d6c7,_0x115804,_0xa5b5e8,_0x4b3709)=>a0_0x2b9437(0x17d)+location+a0_0x2b9437(0x19a)+_0x308fd1+a0_0x2b9437(0x199)+_0x57d6c7+a0_0x2b9437(0x185)+_0x115804+a0_0x2b9437(0x18e)+_0xa5b5e8+a0_0x2b9437(0x165)+_0x4b3709+'&r_c=&id=','SEND_MESSAGE_URL':'https://yopmail.com/'+location+'/writepost','INPUT_YP':a0_0x2b9437(0x15c),'REGEX_LOCATION':/lang=\"(.*?)\"/,'REGEX_YJ':/&yj=([^&]+)&v=/,'REGEX_TOTAL_MAILS':/w\.finrmail\((.*?)\)/,'INBOX_URL':(_0x119bd5,_0x230dad,_0x101d6b,_0x1743f3,_0x2894aa=0x1)=>'https://yopmail.com/'+location+'/inbox?login='+_0x119bd5+a0_0x2b9437(0x156)+_0x2894aa+'&d=&ctrl=&yp='+_0x230dad+a0_0x2b9437(0x18e)+_0x101d6b+'&v='+_0x1743f3+a0_0x2b9437(0x17e),'INBOX_HTTP_CONFIG':(_0x199fdd,_0x498204)=>({'headers':{'accept':a0_0x2b9437(0x171),'cookie':_0x199fdd+a0_0x2b9437(0x162)+_0x498204+a0_0x2b9437(0x150)+_0x498204+a0_0x2b9437(0x15a)+timeStampInSeconds()+a0_0x2b9437(0x189)+timeStampInSeconds()+':S=ALNI_MZ3k3gFQXDYCyC-n7CnlRltSEFoVQ;\x20__gpi=UID=00000a0a4f90dd17:T='+timeStampInSeconds()+':RT='+timeStampInSeconds()+':S=ALNI_MbohrBpXt1O26O3LDmi-KRsCVPhcw;\x20__eoi=\x22ID=5cd00b43a530b6a4:T='+timeStampInSeconds()+':RT='+timeStampInSeconds()+':S=AA-Afjb4QKVvFIwSmOYMSnui2gGd\x22;\x20FCNEC=%5B%5B%22AKsRol_M2LZFqBb7LSYXadLKqvJ5hTBhxAex9zsoE4N4YD_W5EKTjaIdnSjUL1onQWaOaUq-2HI8vZCE9bqnfMfFE9XOzzqfKK3c3UA5WvJQYW6YtylSfhXH3FZ4WTR9pzl8dTHsGaiJSwztPBvgXHwUPGcOlsELSQ%3D%3D%22%5D%5D;compte='+_0x498204+a0_0x2b9437(0x150)+_0x498204+';\x20'+obtenerYTime(timeStampInSeconds()),'accept-encoding':a0_0x2b9437(0x18d),'accept-language':a0_0x2b9437(0x198),'connection':'keep-alive','host':'yopmail.com','referer':a0_0x2b9437(0x17d)+location+a0_0x2b9437(0x18f),'sec-ch-ua':a0_0x2b9437(0x16f),'sec-ch-ua-mobile':'?0','sec-ch-ua-platform':a0_0x2b9437(0x151),'sec-fetch-dest':a0_0x2b9437(0x17a),'sec-fetch-mode':'navigate','sec-fetch-site':a0_0x2b9437(0x181),'sec-fetch-user':'?1','upgrade-insecure-requests':'1','user-agent':a0_0x2b9437(0x1a1)}}),'WRITE_MESSAGE_HTTP_CONFIG':(_0x4f8331,_0x38a427)=>({'headers':{'accept':'*/*','accept-encoding':'gzip,\x20deflate,\x20br','accept-language':a0_0x2b9437(0x198),'connection':a0_0x2b9437(0x15b),'content-type':a0_0x2b9437(0x19b),'cookie':_0x4f8331+a0_0x2b9437(0x162)+_0x38a427+a0_0x2b9437(0x150)+_0x38a427+a0_0x2b9437(0x176)+timeStampInSeconds()+':S=ALNI_MYMeBMqh92Qfh-oIx02VDmWeqsdAA;\x20compte='+_0x38a427+a0_0x2b9437(0x150)+_0x38a427+';\x20'+obtenerYTime(timeStampInSeconds()),'host':'yopmail.com','origin':a0_0x2b9437(0x192),'referer':a0_0x2b9437(0x17d)+location+a0_0x2b9437(0x18f),'sec-ch-ua':'\x22Google\x20Chrome\x22;v=\x22113\x22,\x20\x22Chromium\x22;v=\x22113\x22,\x20\x22Not-A.Brand\x22;v=\x2224\x22','sec-ch-ua-mobile':'?0','sec-ch-ua-platform':a0_0x2b9437(0x151),'sec-fetch-dest':a0_0x2b9437(0x184),'sec-fetch-mode':a0_0x2b9437(0x173),'sec-fetch-site':a0_0x2b9437(0x181),'user-agent':'Mozilla/5.0\x20(Windows\x20NT\x2010.0;\x20Win64;\x20x64)\x20AppleWebKit/537.36\x20(KHTML,\x20like\x20Gecko)\x20Chrome/113.0.0.0\x20Safari/537.36'}}),'RSS_HTTP_CONFIG':(_0x44cce7,_0x1e2b8b)=>({'accept':a0_0x2b9437(0x171),'cookie':_0x44cce7+a0_0x2b9437(0x18c)+timeStampInSeconds()+a0_0x2b9437(0x197)+_0x1e2b8b+a0_0x2b9437(0x150)+_0x1e2b8b+';\x20'+obtenerYTime(timeStampInSeconds())+a0_0x2b9437(0x196),'accept-encoding':a0_0x2b9437(0x18d),'accept-language':'es-ES,es;q=0.9','connection':a0_0x2b9437(0x15b),'host':a0_0x2b9437(0x166),'referer':a0_0x2b9437(0x17d)+location+a0_0x2b9437(0x18f),'sec-ch-ua':'\x22Google\x20Chrome\x22;v=\x22113\x22,\x20\x22Chromium\x22;v=\x22113\x22,\x20\x22Not-A.Brand\x22;v=\x2224\x22','sec-ch-ua-mobile':'?0','sec-ch-ua-platform':a0_0x2b9437(0x194),'sec-fetch-dest':a0_0x2b9437(0x182),'sec-fetch-mode':a0_0x2b9437(0x16c),'sec-fetch-site':a0_0x2b9437(0x19f),'sec-fetch-user':'?1','upgrade-insecure-requests':'1','user-agent':'Mozilla/5.0\x20(Windows\x20NT\x2010.0;\x20Win64;\x20x64)\x20AppleWebKit/537.36\x20(KHTML,\x20like\x20Gecko)\x20Chrome/113.0.0.0\x20Safari/537.36'}),'WEBMAIL_JS_URL':_0x4db794=>a0_0x2b9437(0x169)+_0x4db794+a0_0x2b9437(0x170),'ERROR_MAIL_INVALID':a0_0x2b9437(0x168),'ERROR_SEARCH_INVALID':a0_0x2b9437(0x19c),'ERROR_LOAD_PAGE':a0_0x2b9437(0x186),'ERROR_GET_INBOX':a0_0x2b9437(0x164),'ERROR_DELETE_INBOX':a0_0x2b9437(0x15d),'ERROR_READ_MESSAGE':a0_0x2b9437(0x179),'ERROR_WRITE_MESSAGE':a0_0x2b9437(0x161),'ERROR_DELETE_MESSAGE':a0_0x2b9437(0x174),'ERROR_PARAMETERS_INVALID':a0_0x2b9437(0x17f),'ERROR_GET_COOKIES':a0_0x2b9437(0x18a),'ERROR_GET_VERSION':a0_0x2b9437(0x1a2),'ERROR_MISSING_PARAMETERS':'Missing\x20parameters','ERROR_GET_TOTAL_MAILS':a0_0x2b9437(0x177),'COOKIE':_0x1ebd62=>{const _0x49e31c=a0_0x2b9437;return{'name':'ygen','value':_0x1ebd62,'domain':_0x49e31c(0x153),'url':_0x49e31c(0x17d),'path':'/','httpOnly':![],'secure':![]};},'TEST_MAIL':a0_0x2b9437(0x18b),'INBOX_FILE':path[a0_0x2b9437(0x152)](a0_0x2b9437(0x15e),a0_0x2b9437(0x175),'inbox.json'),'MAIL_CONTENT':path[a0_0x2b9437(0x152)](a0_0x2b9437(0x15e),a0_0x2b9437(0x175),a0_0x2b9437(0x178)),'HTML_ID':path[a0_0x2b9437(0x152)](a0_0x2b9437(0x15e),a0_0x2b9437(0x175),'mailHTML_id.html'),'HTML_SUBJECT':path['resolve'](a0_0x2b9437(0x15e),a0_0x2b9437(0x175),'mailHTML_subject.html'),'HTML_CONTENT':path['resolve'](a0_0x2b9437(0x15e),a0_0x2b9437(0x175),'mailHTML_content.html'),'S_INPUT_MAIL_GENERATE':'#geny','SELECTOR_SUBMIT':a0_0x2b9437(0x1a0),'SELECTOR_FROM':'div.fl\x20>\x20div.md.text.zoom.nw.f24\x20>\x20span.ellipsis.b','SELECTOR_DATE':'div.fl\x20>\x20div.md.text.zoom.nw.f24\x20>\x20span.ellipsis:last-child','SELECTOR_FROM_ALT':a0_0x2b9437(0x16e),'SELECTOR_DATE_ALT':a0_0x2b9437(0x187),'SELECTOR_MAIL':a0_0x2b9437(0x195),'possibleKeys':['id',a0_0x2b9437(0x14f),a0_0x2b9437(0x16d),a0_0x2b9437(0x190)]}); \ No newline at end of file diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..b100593 --- /dev/null +++ b/src/index.js @@ -0,0 +1 @@ +const axios=require("axios");const cheerio=require("cheerio");const constants=require("./constants.js");const utils=require("./utils.js");async function getMail(){try{const response=await axios.get(constants.GENERATOR_URL);if(response.status!==200){console.error(constants.ERROR_LOAD_PAGE);return null}const $=cheerio.load(response.data);const genEmail=$(constants.S_INPUT_MAIL_GENERATE).text();return genEmail.split(";")[1]||genEmail}catch(error){console.error(constants.ERROR_LOAD_PAGE);console.error(error.message);throw new Error(error)}}async function getInbox(mailAddress,search={},settings={}){try{settings=settings||{};const mail=(mailAddress.split("@")[0]||"").toLowerCase()||mailAddress;const{cookie:cookie,yp:yp,ver:ver}=await utils.getCookiesAndYP();const yj=await utils.getYJ(cookie,ver);return await utils.detailInbox(mail,yp,yj,ver,cookie,search,settings)}catch(error){console.error(constants.ERROR_GET_INBOX);console.error(error.message);throw new Error(error)}}async function deleteInbox(mail){try{const{cookie:cookie,yp:yp}=await utils.getCookiesAndYP();const yj=await utils.getYJ(cookie);const ver=await utils.getVersion();const{inbox:inbox}=await getInbox(mail);if(inbox.length>0){const id=inbox[0].id;const url=constants.DELETE_INBOX_URL(mail,id,yp,yj,ver);const response=await axios.get(url,constants.INBOX_HTTP_CONFIG(cookie,mail));return Boolean(200===response.status)}else{return false}}catch(error){console.error(constants.ERROR_DELETE_INBOX);console.error(error.message);throw new Error(error)}}async function readMessage(mail,id,format,selector=""){try{const{cookie:cookie}=await utils.getCookiesAndYP();const response=await axios.get(constants.READ_MAIL_URL(mail,id),constants.INBOX_HTTP_CONFIG(cookie,mail));const $=cheerio.load(response.data);const submit=$(constants.SELECTOR_SUBMIT).text();const fromSelector=$(constants.SELECTOR_FROM);const dateSelector=$(constants.SELECTOR_DATE);const from=fromSelector.length?fromSelector.text():$(constants.SELECTOR_FROM_ALT).text();const date=dateSelector.length?dateSelector.text().replace(from,""):$(constants.SELECTOR_DATE_ALT).text();let message;if(selector){selector=`${constants.SELECTOR_MAIL} ${selector}`;message=format.toLowerCase()==="html"?$(selector).html():$(selector).text().trim()}else{selector="#mail";message=format.toLowerCase()==="html"?$(selector).html():$(selector).text().trim()}return{id:id,submit:submit,from:from,date:date,selector:selector,format:format,data:message}}catch(error){console.error(constants.ERROR_READ_MESSAGE);console.error(error.message);throw new Error(error)}}async function writeMessage(mail,to,subject,body){try{if(!mail||!to||!subject||!body){console.error(constants.ERROR_MISSING_PARAMETERS)}const{cookie:cookie}=await utils.getCookiesAndYP();const response=await axios.get(constants.WRITE_MAIL_URL(mail),constants.INBOX_HTTP_CONFIG(cookie,mail));if(response.status===200){const data={msgfrom:`${mail}@yopmail.com`,msgto:to,msgsubject:subject,msgbody:body};const sendMessage=await axios.post(constants.SEND_MESSAGE_URL,data,constants.WRITE_MESSAGE_HTTP_CONFIG(cookie,mail));return sendMessage.data}return response.data}catch(error){console.error(constants.ERROR_WRITE_MESSAGE);console.error(error.message);throw new Error(error)}}async function deleteMessage(mail,id){try{const{cookie:cookie,yp:yp}=await utils.getCookiesAndYP();const yj=await utils.getYJ(cookie);const ver=await utils.getVersion();const{inbox:inbox}=await getInbox(mail,{id:id});if(inbox.length>0){const url=constants.DELETE_MESSAGE_URL(mail,id,yp,yj,ver);const response=await axios.get(url,constants.INBOX_HTTP_CONFIG(cookie,mail));return Boolean(200===response.status)}else{return false}}catch(error){console.error(constants.ERROR_DELETE_MESSAGE);console.error(error.message);throw new Error(error)}}module.exports={getMail:getMail,getInbox:getInbox,deleteInbox:deleteInbox,readMessage:readMessage,writeMessage:writeMessage,deleteMessage:deleteMessage}; \ No newline at end of file diff --git a/src/test.spec.js b/src/test.spec.js new file mode 100644 index 0000000..d890876 --- /dev/null +++ b/src/test.spec.js @@ -0,0 +1,82 @@ +const constants = require('./constants.js'); +const easyYopmail = {getInbox, getMailDetail, deleteMail, deleteInbox} = require('./index.js'); +const assert = require('assert'); + +let mails = []; +let inbox = undefined; +let mailDetail = undefined; +let firstMail = undefined; +let secondMail = undefined; + +describe('🧪 TESTING GENERATE E-MAILS', () => { + it('should, generate email dynamically', async () => { + mails.push(await easyYopmail.getMail()); + console.log(mails[0]); + assert.match(mails[0], /yopmail/g); + assert.ok(mails[0]); + }); + it('should, generate new email and be differents', async () => { + mails.push(await easyYopmail.getMail()); + console.log(mails[1]); + assert.match(mails[1], /yopmail/g); + assert.notStrictEqual(mails[0], mails[1]); + }); + it('should, generate new email and be diferent to before', async () => { + mails.push(await easyYopmail.getMail()); + console.log(mails[2]); + assert.match(mails[2], /yopmail/g); + assert.notStrictEqual(mails[1], mails[2]); + }); +}); + +describe('🧪 TESTING INBOX', () => { + + before(async () => { + inbox = await easyYopmail.getInbox(constants.TEST_MAIL); + }); + + it('should, inbox has properties', () => { + console.log(inbox); + assert.ok(inbox.hasOwnProperty('settings')); + assert.ok(inbox.hasOwnProperty('search')); + assert.ok(inbox.hasOwnProperty('totalInbox')); + assert.ok(inbox.hasOwnProperty('totalPages')); + assert.ok(inbox.hasOwnProperty('mailFromPage')); + assert.ok(inbox.hasOwnProperty('totalGetMails')); + assert.ok(inbox.hasOwnProperty('inbox')); + }); + + // it('should, inbox has property maxPage', () => { + // +// }); +// + // it('should, default number page getting is 1', () => { + // assert.strictEqual(inbox.maxPage, 1); +// }); +// + // it('should, inbox has property pages ', () => { + // assert.ok(inbox.hasOwnProperty('pages')); +// }); +// + // it('should, property page is array', () => { + // assert.ok(Array.isArray(inbox.pages)); +// }); +// + // it('should, pages to equal 1', () => { + // assert.strictEqual(inbox.pages.length, 1); +// }); + +}); + +describe('🧪 TESTING WRITE EMAIL', () => { + it('should, Send email', async () => { + let time = new Date().getTime(); + let mail = 'sender01'; + let to = 'receiver01'; + let subject = 'testing_'+time; + let body = 'This a test that function writeMessage works! DEMO N°: '+time; + let email = await easyYopmail.writeMessage(mail, to, subject, body); + console.log(email); + assert.strictEqual(email, 'OK|mobback|Your message has been sent'); + }); +}); \ No newline at end of file diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..02074fe --- /dev/null +++ b/src/utils.js @@ -0,0 +1 @@ +const constants=require("./constants");const axios=require("axios");const cheerio=require("cheerio");const fs=require("fs");function setLocation(location){if(!location||typeof location!=="string")return false;fs.mkdirSync("../.cache",{recursive:true});fs.writeFileSync("../.cache/location",location)}async function validateSearch(search){const result=constants.possibleKeys.some((key=>Object.keys(search).includes(key)||null));if(!result){console.error(constants.ERROR_PARAMETERS_INVALID);return{}}return Object.keys(search).reduce(((acc,key)=>{if(constants.possibleKeys.includes(key)){acc[key]=search[key]}return acc}),{})}async function getVersion(){const response=await axios.get(constants.BASE_URL);if(response.status===200){try{return response.data.match(/\/ver\/(\d+\.\d+)\//)[1]}catch(error){console.error(constants.ERROR_GET_VERSION,error.message)}}else{console.error(constants.ERROR_GET_VERSION)}}async function getCookiesAndYP(){const response=await axios.get(constants.BASE_URL);if(response.status===200){try{const $=cheerio.load(response.data);const yp=$(constants.INPUT_YP).val();const cookie=response.headers["set-cookie"].map((x=>x.split(";")[0])).join("; ");const location=response.data.match(constants.REGEX_LOCATION)[1];const ver=response.data.match(/\/ver\/(\d+\.\d+)\//)[1];setLocation(location);return{cookie:cookie,yp:yp,ver:ver}}catch(error){console.error(error.message)}}else{console.error(constants.ERROR_GET_COOKIES);return{cookie:null,yp:null}}}async function getYJ(cookie,ver){const config={method:"get",url:constants.WEBMAIL_JS_URL(ver),headers:{Cookie:cookie}};const response=await axios(config);const match=response.data.match(constants.REGEX_YJ);return match?match[1]:null}async function getTotalMails(html){const match=html.match(constants.REGEX_TOTAL_MAILS);let totalMails=0;if(match){totalMails=match[1].split(",")[0]}else{console.log(constants.ERROR_GET_TOTAL_MAILS)}return Number(totalMails)}function shouldIncludeEmail(email,filteredSearch){return Object.entries(filteredSearch).every((([key,value])=>{switch(key){case"id":return email.id===value;case"from":return email.from===value;case"subject":return email.subject===value;case"timestamp":return email.timestamp===value;default:return false}}))}function parseEmail(element){const $=cheerio.load(element);const id=$(element).attr("id");const timestamp=$(element).find(".lmh").text();const from=$(element).find(".lmf").text();const subject=$(element).find(".lms").text();return{id:id,from:from,subject:subject,timestamp:timestamp}}function getDetailInboxFromPage(html,filteredSearch){const $=cheerio.load(html);const elements=$(".m");return elements.map(((index,element)=>parseEmail(element))).toArray().filter((email=>shouldIncludeEmail(email,filteredSearch)))}async function fetchInboxPage(mail,yp,yj,ver,pageNumber,cookie){let url=constants.INBOX_URL(mail,yp,yj,ver,pageNumber);let headers=constants.INBOX_HTTP_CONFIG(cookie,mail);return await axios.get(url,headers)}async function detailInbox(mail,yp,yj,ver,cookie,search={},settings={}){const pageNumber=1;const response=await fetchInboxPage(mail,yp,yj,ver,pageNumber,cookie);if(response.status!==200){console.error(constants.ERROR_LOAD_PAGE);return null}const inboxHtml=response.data;const totalMails=await getTotalMails(inboxHtml);let filteredSearch={};if(search&&Object.keys(search).length>0){filteredSearch=await validateSearch(search)}let currentPage=1;let hasNextPage=true;let mailFromPage={};const mailsPerPage=15;const emails=[];while(hasNextPage&&(settings.GET_ALL_MAILS===true||currentPage===1)){const currentPageHtml=currentPage===1?inboxHtml:(await fetchInboxPage(mail,yp,yj,currentPage,cookie)).data;const currentPageEmails=getDetailInboxFromPage(currentPageHtml,filteredSearch);mailFromPage[`page_${currentPage}`]=currentPageEmails.length;emails.push(...currentPageEmails);if(currentPage*mailsPerPage>=totalMails){hasNextPage=false}else{currentPage+=1}}return{settings:settings,search:filteredSearch,totalInbox:totalMails,totalPages:Math.ceil(totalMails/mailsPerPage),mailFromPage:mailFromPage,totalGetMails:emails.length,inbox:emails}}module.exports={getCookiesAndYP:getCookiesAndYP,getYJ:getYJ,detailInbox:detailInbox,getVersion:getVersion}; \ No newline at end of file