UNPKG

@ashiteam/ashi-smule

Version:

AES encription/decription library for my use

3 lines (2 loc) 13.8 kB
/*! @ashiteam/ashi-smule 2020-01-29 */ "use strict";var __awaiter=this&&this.__awaiter||function(t,e,i,s){return new(i||(i=Promise))(function(r,n){function a(t){try{l(s.next(t))}catch(t){n(t)}}function o(t){try{l(s.throw(t))}catch(t){n(t)}}function l(t){t.done?r(t.value):new i(function(e){e(t.value)}).then(a,o)}l((s=s.apply(t,e||[])).next())})};Object.defineProperty(exports,"__esModule",{value:!0});const ashi_smule_interfaces_1=require("./models/ashi-smule-interfaces");class ProxyData{constructor(t,e,i){this.host=t,this.port=e,this.country=i}}exports.ProxyData=ProxyData;class AshiSmulUniversalFetchData{constructor(){this.ky=require("ky-universal")}getData(t,e){return __awaiter(this,void 0,void 0,function*(){console.log("Fetching URL: "+t);const i=yield this.ky.get(t);return"html"==e?yield i.text():i.json()})}getHtmlBody(t){return __awaiter(this,void 0,void 0,function*(){return this.getData(t,"html")})}getJson(t){return __awaiter(this,void 0,void 0,function*(){return this.getData(t,"json")})}addAvoidCurrentProxy(){}}exports.AshiSmulUniversalFetchData=AshiSmulUniversalFetchData;class AshiSmule{constructor(t){this.smuleBaseUrl="https://www.smule.com/",this.chanelUrlBase="s/profile/performance/USER_NAME/sing",this.favUrlBase="USER_NAME/favorites/json",this.offsetParam="?offset=",this.sizeParam="&size=",this.httpData=t||new AshiSmulUniversalFetchData}log(t){const e=new Date(Date.now()),i=`${e.getFullYear()}/${e.getMonth()+1}/${e.getDate()} ${e.getHours()}:${e.getMinutes()}:${e.getSeconds()}`;console.log(`${i} - ${t}`)}getOffsetSizeParamUrlPart(t,e){return e||(e=25),this.offsetParam+t+this.sizeParam+e}fixUserNameInUrl(t,e){return t.replace(/USER_NAME/g,e)}getUrl(t,e,i,s){const r=this.smuleBaseUrl+t+this.getOffsetSizeParamUrlPart(i,s);return this.fixUserNameInUrl(r,e)}getData(t,e,i,s=3){return __awaiter(this,void 0,void 0,function*(){try{const r=this.getUrl(t,e,i);return yield this.httpData.getJson(r)}catch(r){return this.log("ERROR in getData"),0!=s?this.getData(t,e,i,s-1):null}})}getAllData(t,e,i,s){return __awaiter(this,void 0,void 0,function*(){const r=[];try{let n=yield this.getData(t,e,0);for(;n&&n.list&&(i?r.push(...yield this.expandCollaborations(n.list)):(this.log(`Got ${n.list.length} perormances`),r.push(...n.list)),!s||!s(n))&&n.next_offset&&n.next_offset>0;){const i=n.next_offset;(n=yield this.getData(t,e,i))||(n=yield this.getData(t,e,i))||(n=yield this.getData(t,e,i))}}catch(t){this.log("ERROR - getAllData"),this.log(t)}return r})}isSoloForOthers(t){return t.message&&t.message.toLowerCase().indexOf("solo solo")>=0}getCollabsFetchUrl(t,e){return this.smuleBaseUrl+`p/${t}/list_duets?offset=${e}&size=25`}getCollabsForPerf(t,e=0){return __awaiter(this,void 0,void 0,function*(){const i=[];try{if(t&&!this.isSoloForOthers(t)){const e=t.performance_key?t.performance_key:"";if(e.length>0){this.log(`\r\nLooking for joins for ${t.title}`);let s=0;for(;s>=0;){const r=this.getCollabsFetchUrl(e,s),n=yield this.httpData.getJson(r);n.list&&(this.log(`Got ${n.list.length} Joins for ${t.title}`),n.list.forEach(t=>{this.logSongInfo(t)}),n.list.forEach(t=>{this.updateMediaUrl(t)}),i.push(...n.list)),s=n.next_offset?n.next_offset:-1}}}}catch(i){this.log("ERROR in getCollabsForPerf"),e<3&&this.getCollabsForPerf(t,++e)}return i})}fixUser(t){return __awaiter(this,void 0,void 0,function*(){t&&t.handle&&!t.account_id&&(t.account_id=yield this.getUserId(t.handle))})}fixFilter(t){return __awaiter(this,void 0,void 0,function*(){const e=[];e.push(this.fixUser(t.owner)),e.push(this.fixUser(t.other)),t.singers&&t.singers.forEach(t=>{e.push(this.fixUser(t))}),t.title&&(t.title=t.title.toLowerCase()),t.artist&&(t.artist=t.artist.toLowerCase()),yield Promise.all(e)})}isOnlyOneMatch(t){return!(!t||!t.performance_key||""===t.performance_key)}canUseSong(t,e){if(e){if(e.performance_key&&""!==e.performance_key)return e.performance_key===t.performance_key;const i=t.owner&&t.owner.account_id?t.owner.account_id:0,s=t.other&&t.other.id?t.other.id:0;if(0!==i&&e.owner&&e.owner.account_id&&i!==e.owner.account_id)return!1;if(0!==s&&e.other&&e.other.account_id&&s!==e.other.account_id)return!1;if(e.singers){if(0!==i&&e.singers.findIndex(t=>t.account_id===i)<0)return!1;if(0!==s&&e.singers.findIndex(t=>t.account_id===s)<0)return!1}if(e.type&&e.type!==t.type)return!1;if(e.title&&""!==e.title){const i=!!e.titleExactMatch&&e.titleExactMatch.valueOf();if(i&&e.title!==t.title)return!1;if(!i&&t.title&&!t.title.valueOf().toLowerCase().includes(e.title))return!1}if(e.artist&&""!==e.artist){const i=!!e.artistExactMatch&&e.artistExactMatch.valueOf();if(i&&e.artist!==t.artist)return!1;if(!i&&t.artist&&!t.artist.valueOf().toLowerCase().includes(e.artist))return!1}if(e.ensemble_type&&e.ensemble_type!==t.ensemble_type)return!1}return!0}filterList(t,e){const i=[];if(t){const s=this.isOnlyOneMatch(e);let r=!0;t.forEach(t=>{r&&this.canUseSong(t,e)&&(this.log("Adding song "+t.title),i.push(t),s&&(r=!1))})}return i}getDataFiltered(t,e,i,s,r){return __awaiter(this,void 0,void 0,function*(){const n=[];try{yield this.fixFilter(i),this.log(i);const a=this.isOnlyOneMatch(i);yield this.getAllData(t,e,s,t=>(n.push(...this.filterList(t.list,i)),!!r&&(r()||a&&n.length>0)))}catch(t){this.log("ERROR in getDataFiltered"),this.log(t)}return n})}getWebUrls(t,e,i){return __awaiter(this,void 0,void 0,function*(){const s=[],r=yield this.getData(t,e,i);return r&&r.list&&r.list.forEach(t=>{t.web_url&&s.push(this.smuleBaseUrl+t.web_url.substr(1))}),{url_list:s,next_offset:r?r.next_offset:-1}})}getUser(t,e=0){return __awaiter(this,void 0,void 0,function*(){try{const i=require("cheerio"),s=yield this.httpData.getHtmlBody(this.smuleBaseUrl+t),r=i.load(s)("script");if(r.length>0){const t=r.toArray()[0].firstChild,e="window.DataStore = ".length;if(t.data){let i=t.data.trim().substr(e);const s=i.indexOf('"recommended_recordings"');i=(i=i.substr(0,s).trim()).substr(0,i.length-1);const r=(i+="}").indexOf("Profile: ");return i=i.substr(r+"Profile: ".length),JSON.parse(i).user}}}catch(i){if(this.log(`Error finding user with handle '${t}'`),e<3)return this.getUser(t,++e)}return null})}getUsers(t){return __awaiter(this,void 0,void 0,function*(){const e=[],i=[];t.forEach(t=>{i.push(this.getUser(t))}),yield Promise.all(i);for(let s=0;s<t.length;++s)e.push(yield i[s]);return e})}getUserId(t){return __awaiter(this,void 0,void 0,function*(){const e=yield this.getUser(t);return e&&e.account_id?e.account_id:0})}getUserIds(t){return __awaiter(this,void 0,void 0,function*(){const e=[];return(yield this.getUsers(t)).forEach(t=>{t&&t.handle&&t.account_id&&e.push({handle:t.handle,account_id:t.account_id})}),e})}getChanelData(t,e){return __awaiter(this,void 0,void 0,function*(){return this.getData(this.chanelUrlBase,t,e)})}getFavData(t,e){return __awaiter(this,void 0,void 0,function*(){return this.getData(this.favUrlBase,t,e)})}getAllChanelData(t,e,i){return __awaiter(this,void 0,void 0,function*(){return this.getAllData(this.chanelUrlBase,t,e,i)})}getAllFavData(t,e,i){return __awaiter(this,void 0,void 0,function*(){return this.getAllData(this.favUrlBase,t,e,i)})}getChanelWebUrls(t,e){return __awaiter(this,void 0,void 0,function*(){return this.getWebUrls(this.chanelUrlBase,t,e)})}getFavWebUrls(t,e){return __awaiter(this,void 0,void 0,function*(){return this.getWebUrls(this.favUrlBase,t,e)})}getChanelDataFiltered(t,e,i,s){return __awaiter(this,void 0,void 0,function*(){return this.getDataFiltered(this.chanelUrlBase,t,e,i,s)})}getFavDataFiltered(t,e,i,s){return __awaiter(this,void 0,void 0,function*(){return this.getDataFiltered(this.favUrlBase,t,e,i,s)})}getPerformanceData(t){const e=require("cheerio").load(t)("script");if(e.length>0){const t=e.toArray()[0].firstChild;if(t.data){let e=t.data.trim();const i=e.indexOf('"performance":');if(i>=0){const t=(e=e.substr(i+'"performance":'.length)).indexOf('"upNextRecommendations":');return e=(e=e.substr(0,t).trim()).substr(0,e.length-1),JSON.parse(e)}}}return null}getSingerNames(t){const e=[];if(t){const i=t.owner&&t.owner.account_id?t.owner.account_id:0,s=t.duet&&t.duet.account_id?t.duet.account_id:0;t.owner&&t.owner.handle&&e.push(t.owner.handle),t.duet&&t.duet.handle&&e.push(t.duet.handle),t.other_performers&&t.other_performers.forEach(t=>{t.handle&&t.account_id&&t.account_id!==i&&t.account_id!==s&&e.push(t.handle)})}return e}getCollaborations(t){return __awaiter(this,void 0,void 0,function*(){try{const e=(yield this.httpData.getHtmlBody(t)).body,i=yield this.getPerformanceData(e);return this.getCollabsForPerf(i)}catch(t){return this.log("ERROR in getCollaborations"),this.log(t),[]}})}expandCollaborations(t){return __awaiter(this,void 0,void 0,function*(){const e=[];for(let i=0;i<t.length;++i){const s=t[i];if(s.child_count&&s.child_count>0){const t=yield this.getCollabsForPerf(s);s.joins=t}e.push(s)}return e})}mergeLists(t,e){e.forEach(e=>{const i=t.findIndex(t=>t.performance_key===e.performance_key);if(i<0)t.unshift(e);else if(e.child_count&&e.child_count>0){const s=t[i];this.mergeLists(s.joins,e.joins)}})}logSongInfo(t){const e=t.joins&&t.joins.length>0?"Join":t.ensemble_type,i=this.getSingerNames(t).join(" & ");this.log(` ${e} - ${t.title} By ${i} - ${t.performance_key}`)}updateSongs(t,e,i,s){return __awaiter(this,void 0,void 0,function*(){const r=[],n="fav"==s?t.fav_songs:t.chanel_songs,a="fav"==s?t.chanel_songs:null,o=new Date(i.valueOf()-6912e5);this.log("Last check date = "+i.toDateString()),this.log("Eight days ago = "+o.toDateString());let l=yield"fav"==s?this.getFavData(e,0):this.getChanelData(e,0);if(!l)return!1;for(;l&&l.list;){this.log(`Got ${l.list.length} songs`),l.list.forEach(t=>{this.logSongInfo(t)});for(let t=0;t<l.list.length;++t){const e=l.list[t],s=new Date(e.created_at?Date.parse(e.created_at.toString()):0),h=a?a.find(t=>t.performance_key==e.performance_key):null;if(s>i)e.child_count&&e.child_count>0&&(e.joins=h?h.joins:yield this.getCollabsForPerf(e)),process.stdout.write("."),this.updateMediaUrl(e),r.push(e);else if(e.child_count&&e.child_count>0&&s>o){e.joins||(e.joins=[]);let t=!1;(h?h.joins:yield this.getCollabsForPerf(e)).forEach(s=>{process.stdout.write(".");let r=s.created_at&&s.created_at>i;if(!r&&n){const t=n.findIndex(t=>t.performance_key===e.performance_key);t>=0&&(r=!n[t].joins||n[t].joins.findIndex(t=>t.performance_key===s.performance_key)<0)}r&&(process.stdout.write("."),e.joins.unshift(s),t=!0)}),t&&r.push(e)}else e.child_count&&e.child_count>0&&(this.log("Old Collab?????"),this.logSongInfo(e))}if(l.list.length>0){const t=l.list[l.list.length-1],e=new Date(t.created_at?Date.parse(t.created_at.toString()):0);if(e<i&&e<o){this.log(""),this.log("Last data found is too old, so stopping");break}}if(!(l.next_offset&&l.next_offset>0)){this.log(""),this.log("No more data, so stopping - "+e);break}{const t=l.next_offset?l.next_offset:0;(l=yield"fav"==s?this.getFavData(e,l.next_offset):this.getChanelData(e,l.next_offset))&&l.list||(l||(l=yield"fav"==s?this.getFavData(e,t):this.getChanelData(e,t)),l&&l.list||this.log("No more data, but odd"))}}return this.log(`Found ${r.length} new songs`),r.length>0&&("fav"==s?(t.fav_songs||(t.fav_songs=[]),this.mergeLists(t.fav_songs,r)):(t.chanel_songs||(t.chanel_songs=[]),this.mergeLists(t.chanel_songs,r))),!0})}loadUserSongData(t,e){return __awaiter(this,void 0,void 0,function*(){if(t.user&&t.user.handle&&(e=t.user.handle),!e)return;const i=t.last_updated?new Date(Date.parse(t.last_updated.toString())):new Date(1980,1,1,1,0,0,0);if(t.chanel_songs||(t.chanel_songs=[]),t.fav_songs||(t.fav_songs=[]),!t.user){const i=yield this.getUser(e);t.user=i||void 0}this.log("statring chanel for "+e);const s=yield this.updateSongs(t,e,i,"chanel");this.log("statring fav for "+e);const r=yield this.updateSongs(t,e,i,"fav");this.log("\r\nDone "+e),r&&s&&(t.last_updated=new Date(Date.now()))})}updateMediaUrl(t){return __awaiter(this,void 0,void 0,function*(){if(t.web_url)try{const e=this.smuleBaseUrl+t.web_url.substr(1),i=yield this.getDownloadUrl(e,3);i&&""!=i.trim()?(t.smule_media_url=i,t.need_refresh=!1):t.need_refresh=!0}catch(e){t.need_refresh=!0,this.log(`updateMediaUrl ERROR - ${e}`)}else this.log("ERROR - No web_url")})}getDownloadUrl(t,e){return __awaiter(this,void 0,void 0,function*(){try{this.smuleBaseUrl;const i=yield this.httpData.getHtmlBody(t);try{i.split('twitter:player:stream:content_type" content="')[1].split('">')[0].replace("/mp4","");return i.split('twitter:player:stream" content="')[1].split('">')[0].replace(/amp;/g,"")}catch(e){this.log(`ERROR getDownloadUrl - ${e}`),this.log(` ${t}`)}}catch(i){if(e>0)return this.getDownloadUrl(t,e-1)}return""})}sanitizeFileName(t){return require("sanitize-filename")(t,{replacement:"_"})}getMediaFilePath(t,e){let i="",s="";if(e.type&&(e.type==ashi_smule_interfaces_1.Type.Audio?(i="audio/",s="m4a"):e.type==ashi_smule_interfaces_1.Type.Video&&(i="video/",s="mp4")),i+=`${t}/`,e.title){const t=this.getSingerNames(e).join(" & ");i+=`${this.sanitizeFileName(e.title)} - ${this.sanitizeFileName(t)}.${s}`}else i="";return i}getAudioFilePath(t,e){const i=this.getMediaFilePath(t,e);if(e.type&&e.type==ashi_smule_interfaces_1.Type.Audio)return i;i.replace(".mp4",".m4a")}getVideoFilePath(t,e){const i=this.getMediaFilePath(t,e);if(e.type&&e.type==ashi_smule_interfaces_1.Type.Video)return i;i.replace(".m4a",".mp4")}addPerfs(t,e,i){e.forEach(e=>{this.isSoloForOthers(e)||(e.child_count&&e.child_count>0?e.joins&&this.addPerfs(t,e.joins,i):i?t.findIndex(t=>t.performance_key==e.performance_key)<0&&t.push(e):t.push(e))})}getAllPerformances(t){const e=[];return t&&(t.chanel_songs&&this.addPerfs(e,t.chanel_songs,!1),t.fav_songs&&this.addPerfs(e,t.fav_songs,!0)),e}getLinksNeededPerfs(t,e=!1){const i=[];return this.getAllPerformances(t).forEach(t=>{!e&&t.smule_media_url&&""!=t.smule_media_url.trim()||t.need_refresh||i.push(t)}),i}}exports.AshiSmule=AshiSmule;