UNPKG

hanime-crave

Version:
83 lines (71 loc) 2.69 kB
const HentaiEpisode = require('../structures/episode.js'); const trending = require('./trending.js'); const sorts = [ 'year', '6_month', '3_month', 'month', 'week', 'day' ]; const defaults = { page: 0, sort: 'month' }; function addMissingArgs(args) { let keys = Object.keys(defaults); for(let i = 0; i < keys.length; i++) if(!args.hasOwnProperty(keys[i])) args[keys[i]] = defaults[keys[i]]; return args } module.exports = (args = defaults) => { return new Promise((res, rej) => { if(typeof args !== 'object') { if(!isNaN(args)) args = addMissingArgs({page: args}); if(typeof args === 'string') if(sorts.includes(args)) args = addMissingArgs({sort: args}); } else args = addMissingArgs(args); if(typeof args !== 'object' || (args || []).length >= 0) return rej(new Error("Wrong arguments format.")); fetch( "https://members.hanime.tv/rapi/v7/browse-trending?time="+args.sort+"&page="+args.page, { "headers": { "x-signature-version": "web2" } } ) .then(resp => resp.json()) .then(json => { let episodes = []; for(let i = 0; i < json.hentai_videos.length; i++) episodes[i] = new HentaiEpisode(json.hentai_videos[i].name, json.hentai_videos[i].slug, json.hentai_videos[i].created_at, json.hentai_videos[i].created_at_unix, json.hentai_videos[i].released_at, json.hentai_videos[i].released_at_unix, json.hentai_videos[i].views, json.hentai_videos[i].interests, json.hentai_videos[i].poster_url, json.hentai_videos[i].cover_url, json.hentai_videos[i].brand, json.hentai_videos[i].is_censored, json.hentai_videos[i].likes, json.hentai_videos[i].dislikes, json.hentai_videos[i].downloads, json.hentai_videos[i].is_banned_in); res({ episodes: episodes, query: {page: json.page, sort: json.time}, next: function() { args.page += 1; return trending(args) } }) }) .catch(err => rej(err)) }) }