UNPKG

@ashiteam/ashi-smule

Version:

AES encription/decription library for my use

554 lines 22.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ashi_smule_interfaces_1 = require("./ashi-smule-interfaces"); var PerformanceType; (function (PerformanceType) { PerformanceType["MyJoin"] = "MYJOIN"; PerformanceType["JoinedMe"] = "JOINEDME"; PerformanceType["Post"] = "POST"; PerformanceType["Group"] = "GROUP"; PerformanceType["Solo"] = "SOLO"; PerformanceType["Unknown"] = "UNKNOWN"; })(PerformanceType = exports.PerformanceType || (exports.PerformanceType = {})); class AshiSmuleStats { constructor(userSongData) { this.userSongData = userSongData; this.myJoins = []; this.othersJoinMe = []; this.updateJoins(); } updateJoins() { this.myJoins.splice(0); const partners = []; const joiners = []; const chanel_songs = this.userSongData.chanel_songs ? this.userSongData.chanel_songs : []; const user = this.user; chanel_songs.forEach(x => { if (x.ensemble_type && x.ensemble_type === ashi_smule_interfaces_1.EnsembleType.Duet) { // console.log(x.ensemble_type); const otherUser = this.getOtherSinger(x, false, user); if (otherUser.account_id !== 0 && partners.findIndex(y => { if (y.account_id === otherUser.account_id) { return true; } else { return false; } }) < 0) { if (x.other) { partners.push(otherUser); } } if (x.child_count && x.child_count > 0 && x.joins.length > 0) { x.joins.forEach(k => { const joiner = this.getOtherSinger(k, false, user); if (joiner.account_id !== 0) { const joinerIndex = joiners.findIndex(r => r.account_id && r.account_id === joiner.account_id ? true : false); if (joinerIndex < 0) { joiners.push(joiner); } } }); } } }); partners.forEach(x => { const userCollab = new AshiCollabData(); userCollab.otherUser = x; const account_id = x.account_id ? x.account_id : 0; chanel_songs.forEach(y => { if (y.ensemble_type && y.ensemble_type === ashi_smule_interfaces_1.EnsembleType.Duet) { const other = this.getOtherSinger(y, false, user); if (account_id === other.account_id) { userCollab.performances.push(y); } } }); this.myJoins.push(userCollab); }); const postings = this.postings; joiners.forEach(x => { const account_id = x.account_id ? x.account_id : 0; const otherJoin = new AshiCollabData(); otherJoin.otherUser = x; postings.forEach(k => { k.joins.forEach(y => { const other = this.getOtherSinger(y, true, user); if (account_id === other.account_id) { otherJoin.performances.push(y); } }); }); this.othersJoinMe.push(otherJoin); }); this.sortJoins(this.myJoins, 'count', 'desc'); this.sortJoins(this.othersJoinMe, 'count', 'desc'); // this.sortJoins(this.othersJoinMe, 'name', 'asc'); } getPerformanceType(perf) { if (perf.ensemble_type) { if (perf.ensemble_type === ashi_smule_interfaces_1.EnsembleType.Solo) { return PerformanceType.Solo; } else if (perf.ensemble_type === ashi_smule_interfaces_1.EnsembleType.Group) { return PerformanceType.Group; } else if (perf.ensemble_type === ashi_smule_interfaces_1.EnsembleType.Duet) { if (perf.child_count && perf.child_count > 0) { return PerformanceType.Post; } else { const user = this.user; if (perf.owner && perf.owner.account_id === user.account_id) { return PerformanceType.JoinedMe; } else if (perf.owner && perf.owner.account_id !== user.account_id) { return PerformanceType.MyJoin; } } } } return PerformanceType.Unknown; } getOtherSinger(perf, okToReturnMe, user) { if (!user) { user = this.user; } const otherUser = { account_id: 0, handle: 'Unknown' }; if (perf.other && perf.other.id && perf.other.id !== user.account_id) { otherUser.account_id = perf.other.id; otherUser.handle = perf.other.label; } if (otherUser.account_id === 0) { if (perf.owner && perf.owner.account_id && (okToReturnMe || perf.owner.account_id !== user.account_id)) { otherUser.account_id = perf.owner.account_id; otherUser.handle = perf.owner.handle; } } return otherUser; } order(a, b, directionMult = 1) { const aLower = a.toLowerCase(); const bLowere = b.toLowerCase(); if (aLower < bLowere) { return -1 * directionMult; } else if (aLower > bLowere) { return 1 * directionMult; } else { return 0; } } sortJoins(joins, orderBy, direction) { const directionMult = direction === 'asc' ? 1 : direction === 'desc' ? -1 : 0; joins.sort((a, b) => { if (orderBy === 'name') { const aHandle = a.otherUser.handle ? a.otherUser.handle : ''; const bHandle = b.otherUser.handle ? b.otherUser.handle : ''; return this.order(aHandle, bHandle, directionMult); } else if (orderBy === 'count') { return (a.performances.length - b.performances.length) * directionMult; } return 0; }); } get user() { const emptyUser = {}; return this.userSongData && this.userSongData.user ? this.userSongData.user : emptyUser; } get chanelSongs() { if (this.userSongData && this.userSongData.chanel_songs) { return this.userSongData.chanel_songs; } else { return []; } } get favSongs() { if (this.userSongData && this.userSongData.fav_songs) { return this.userSongData.fav_songs; } else { return []; } } get postings() { if (this.userSongData && this.userSongData.chanel_songs) { return this.userSongData.chanel_songs.filter(x => { return x.child_count && x.child_count > 0; }); } else { return []; } } get postingsByDate() { const oldDate = new Date(2015, 1, 1, 0, 0, 0, 0); return this.postings.sort((a, b) => { const aCreatedAt = a.created_at ? a.created_at.valueOf() : oldDate.valueOf(); const bCreatedAt = b.created_at ? b.created_at.valueOf() : oldDate.valueOf(); if (aCreatedAt > bCreatedAt) { return 1; } else if (aCreatedAt < bCreatedAt) { return -1; } else { return 0; } }); } get postingsByTitle() { return this.postings.sort((a, b) => { const aTitle = a.title ? a.title : ''; const bTitle = b.title ? b.title : ''; return this.order(aTitle, bTitle); }); } get joins() { if (this.userSongData && this.userSongData.chanel_songs) { return this.userSongData.chanel_songs.filter(x => { return (x.ensemble_type && x.ensemble_type === ashi_smule_interfaces_1.EnsembleType.Duet && (!x.child_count || x.child_count === 0)); }); } else { return []; } } getPerformancesOfEnsambleType(ensembleType) { if (this.userSongData && this.userSongData.chanel_songs) { return this.userSongData.chanel_songs.filter(x => { return x.ensemble_type && x.ensemble_type === ensembleType; }); } else { return []; } } getPerformancesOfType(type) { const data = []; const chanelSongs = this.chanelSongs; chanelSongs.forEach(x => { const xType = this.getPerformanceType(x); if (xType === type) { data.push(x); } else if (xType === PerformanceType.Post && (type === PerformanceType.JoinedMe || type === PerformanceType.MyJoin)) { x.joins.forEach(y => { const yType = this.getPerformanceType(y); if (yType == type) { data.push(y); } }); } }); return data; } get solos() { return this.getPerformancesOfEnsambleType(ashi_smule_interfaces_1.EnsembleType.Solo); } get groupSongs() { return this.getPerformancesOfEnsambleType(ashi_smule_interfaces_1.EnsembleType.Group); } get myJoinSongs() { return this.getPerformancesOfType(PerformanceType.MyJoin); } get joinedMeSongs() { return this.getPerformancesOfType(PerformanceType.JoinedMe); } get allDuets() { const duets = this.myJoinSongs; duets.push(...this.joinedMeSongs); return duets; } get myJoinCount() { return this.getPerformanceCount(this.myJoins); } get othersJoinMeCount() { return this.getPerformanceCount(this.othersJoinMe); } get myPostingsCount() { if (this.userSongData.chanel_songs) { return this.userSongData.chanel_songs.reduce((n, x) => { if (x.child_count && x.child_count > 0) { return n + 1; } return n; }, 0); } return 0; } get myTotalPerformances() { if (this.userSongData.chanel_songs) { return this.userSongData.chanel_songs.length; } return 0; } getPerformanceCount(collabs) { return collabs.reduce((n, x) => { return n + x.performances.length; }, 0); } getEnsambleTypeCount(ensambleType) { if (this.userSongData.chanel_songs) { return this.userSongData.chanel_songs.reduce((n, x) => { if (x.ensemble_type && x.ensemble_type === ensambleType) { return n + 1; } return n; }, 0); } return 0; } get mySoloCount() { return this.getEnsambleTypeCount(ashi_smule_interfaces_1.EnsembleType.Solo); } get myGroupSongCount() { return this.getEnsambleTypeCount(ashi_smule_interfaces_1.EnsembleType.Group); } get biggestJoin() { const perf = this.postings.reduce((a, b) => { return a.joins.length > b.joins.length ? a : b; }); return perf; } get smallestJoin() { const perf = this.postings.reduce((a, b) => { return a.joins.length < b.joins.length ? a : b; }); return perf; } get uniqueSongTitleArray() { const data = []; const chanelSongs = this.chanelSongs; chanelSongs.forEach(x => { const titleLower = (x.title ? x.title.toLowerCase() : '').trim(); const index = data.findIndex(y => y.toLowerCase().trim() === titleLower); if (index < 0 && x.title) { data.push(x.title.trim()); } }); return data; } get uniqueSingersArray() { const user = this.user; const allDuets = this.allDuets; let singers = []; allDuets.forEach(x => { const otherSinger = this.getOtherSinger(x, true, user); const index = singers.findIndex(k => k.account_id === otherSinger.account_id); if (index < 0 && otherSinger) { singers.push(otherSinger); } }); return singers.sort((a, b) => { const aHandle = a.handle ? a.handle : ''; const bHandle = b.handle ? b.handle : ''; return this.order(aHandle, bHandle); }); } getPerfString(x, chanelSongs, user) { if (!chanelSongs) { chanelSongs = this.chanelSongs; } if (!user) { user = this.user; } const xLower = x.toLowerCase().trim(); const perfs = chanelSongs.filter(k => k.title && k.title.toLowerCase().trim() === xLower); let data = `${x} : ${perfs.length}\r\n`; perfs.forEach(y => { const type = this.getPerformanceType(y); if (type === PerformanceType.Solo) { data += ` SOLO\r\n`; } else if (type === PerformanceType.Group) { data += ` GROUP (${y.other_performers ? y.other_performers.length : 0})\r\n`; if (y.other_performers) { y.other_performers.forEach(k => { data += ` ${k.handle}\r\n`; }); } } else if (type === PerformanceType.Post) { data += ` POST - Joins (${y.joins.length})\r\n`; y.joins.forEach(k => { const otherSinger = this.getOtherSinger(k, true, user).handle; data += ` ${otherSinger}\r\n`; }); } else { const otherSinger = this.getOtherSinger(y, false, user).handle; data += ` ${otherSinger}\r\n`; } }); return data; } getString() { let data = ''; const user = this.user; const favSongs = this.favSongs; const chanelSongs = this.chanelSongs; data += '\r\n'; data += 'User Info\r\n'; data += ` Handle: ${user.handle}\r\n`; data += ` Name: ${user.first_name} ${user.last_name}\r\n`; data += ` Location: ${user.location}\r\n`; data += '\r\n'; data += 'Following Info\r\n'; data += ` Following: ${user.followees}\r\n`; data += ` Followers: ${user.followers}\r\n`; const followees = this.getNumber(user.followees); const followers = this.getNumber(user.followers); if (followees > 0) { data += ` Ratio: 1 - ${followers / followees}\r\n`; } else { data += ` Ratio: 1 - infinity\r\n`; } const myPostingsCount = this.myPostingsCount; const myJoinCount = this.myJoinCount; const othersJoinMeCount = this.othersJoinMeCount; const mySoloCount = this.mySoloCount; const myGroupSongCount = this.myGroupSongCount; const biggestJoin = this.biggestJoin; const smallestJoin = this.smallestJoin; data += '\r\n'; data += 'Songs Info\r\n'; data += ` ${user.handle} sang ${myJoinCount} songs with ${this.myJoins.length} people\r\n`; data += ` ${this.othersJoinMe.length} people joind ${user.handle} and created ${othersJoinMeCount} collaborations\r\n`; data += ` Total Postings = ${myPostingsCount} : ${this.postings.length}\r\n`; data += ` Average Joins Per Post = ${othersJoinMeCount / myPostingsCount}\r\n`; data += ` Joins\r\n Min = ${smallestJoin.joins.length} - (${smallestJoin.title}),\r\n Max = ${biggestJoin.joins.length} - (${biggestJoin.title})\r\n`; data += ` Total Solo = ${mySoloCount} : ${this.solos.length}\r\n`; data += ` Total Group Songs = ${myGroupSongCount} : ${this.groupSongs.length}\r\n`; data += ` Total Joins = ${myJoinCount} : ${this.myJoinSongs.length}\r\n`; data += ` Total Joins With ${user.handle} = ${this.joinedMeSongs.length}\r\n`; data += ` Total Recordings = ${myPostingsCount + myJoinCount + myGroupSongCount + mySoloCount}\r\n`; if (this.myTotalPerformances != myPostingsCount + myJoinCount + myGroupSongCount + mySoloCount) { data += ` Problem Total Recordings Alt = ${this.myTotalPerformances}\r\n`; } // data += ` Total Recordings (Smule) = ${user.num_performances}\r\n`; const nameWidth = 20; data += '\r\n'; data += `${user.handle} Joining Others - Song Count ${myJoinCount} - People Count ${this.myJoins.length}\r\n`; data += ` ${'Name'.padEnd(nameWidth, ' ')} Users\r\n`; data += ` ${'----'.padEnd(nameWidth, ' ')} -----\r\n`; this.myJoins.forEach(x => { const otherUserHandle = x.otherUser && x.otherUser.handle ? x.otherUser.handle : 'Unknown'; data += ` ${otherUserHandle.padEnd(nameWidth)}`; data += ` ${x.performances.length}\r\n`; }); data += '\r\n'; data += `Others Joining ${user.handle} - Song Count ${othersJoinMeCount} : ${this.joinedMeSongs.length} - People Count ${this.othersJoinMe.length}\r\n`; data += ` ${'Name'.padEnd(nameWidth, ' ')} Users\r\n`; data += ` ${'----'.padEnd(nameWidth, ' ')} -----\r\n`; this.othersJoinMe.forEach(x => { const otherUserHandle = x.otherUser && x.otherUser.handle ? x.otherUser.handle : 'Unknown'; data += ` ${otherUserHandle.padEnd(nameWidth)}`; data += ` ${x.performances.length}\r\n`; }); // data += `Last Song = ${ // this.chanelSongs[this.chanelSongs.length - 1].title // }\r\n`; const uniqueSongIdArray = this.uniqueSongTitleArray.sort((a, b) => { return this.order(a, b); }); data += '\r\n'; data += `Total Unique Tracks = ${uniqueSongIdArray.length}\r\n`; uniqueSongIdArray.forEach(x => { data += '\r\n'; data += this.getPerfString(x, chanelSongs, user); }); data += '\r\n'; const solos = this.solos; data += `Solos by ${user.handle} (${solos.length} Songs)\r\n`; solos.forEach(x => { data += ` ${x.title}\r\n`; }); data += '\r\n'; const posts = this.postings; data += `Songs posted by ${user.handle} (${posts.length} Songs)\r\n`; posts.forEach(x => { const title = x.title ? x.title : ''; data += ` ${title.padEnd(50)} (${x.joins.length} Joins)\r\n`; }); const uniqueSingersArray = this.uniqueSingersArray; data += '\r\n'; data += `${user.handle} has duets with ${uniqueSingersArray.length} singers\r\n`; const iJoinedTitle = `${user.handle} Joined`.padStart(nameWidth + 7); data += ` ${'Name'.padEnd(nameWidth)} ${iJoinedTitle} ${'Other Joined'.padStart(14)} ${'Total'.padStart(10)} ${'Ratio'.padStart(12)}\r\n`; uniqueSingersArray.forEach(x => { const handle = x.handle ? x.handle : ''; const joins = this.myJoins.filter(k => k.otherUser.account_id === x.account_id); const joinCount = joins.length > 0 ? joins[0].performances.length : 0; const joinCountStr = `${joinCount}`.padStart(nameWidth + 7); const otherJoins = this.othersJoinMe.filter(k => k.otherUser.account_id === x.account_id); const otherJoinCount = otherJoins.length > 0 ? otherJoins[0].performances.length : 0; const otherJoinCountStr = `${otherJoinCount}`.padStart(14); const totalStr = `${joinCount + otherJoinCount}`.padStart(10); const ratio = otherJoinCount / joinCount; const ratioStr = ratio.toFixed(2).padStart(12); data += ` ${handle.padEnd(nameWidth)} ${joinCountStr} ${otherJoinCountStr} ${totalStr} ${ratioStr}\r\n`; }); // uniqueSongIdArray.forEach(x => { // const xLower = x.toLowerCase().trim(); // chanelSongs // .filter(k => k.title && k.title.toLowerCase().trim() === xLower) // .filter(y => this.getPerformanceType(y) === PerformanceType.Solo) // .forEach(m => { // data += this.getPerfString(x, chanelSongs, user); // }); // }); return data; } getNumber(num) { if (num) { if (num.length > 0 && num[num.length - 1].toLowerCase() == 'k') { const float = Number.parseFloat(num.substr(0, num.length - 1)); return float * 1000; } else { return Number.parseInt(num); } } else { return 0; } } } exports.AshiSmuleStats = AshiSmuleStats; class AshiCollabData { constructor() { this.otherUser = {}; this.performances = []; } } exports.AshiCollabData = AshiCollabData; //# sourceMappingURL=ashi-smule-stats.js.map