Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- const Promise = require('es6-promise').Promise;
- const PixivAppApi = require('pixiv-app-api')
- /* PixivAppApi.prototype.
- constructor(username?: string, password?: string): PixivAppApi
- login(username?: string, password?: string): Promise
- userDetail(id: ID, params?: Object): Promise
- params = { restrict='public', offset=0 }
- userFollowing(id: ID, params?: Object): Promise
- */
- // =============================================================================
- function initPixiv( authInfo ) {
- const pixiv = new PixivAppApi()
- if (authInfo) {
- return pixiv.login(authInfo.username, authInfo.password).then(() => {
- return { pixiv }
- })
- } else {
- return Promise.resolve({ pixiv })
- }
- }
- function getFollowingUsers( {pixiv, userID} ) {
- return pixiv.userDetail(userID).then((res) => {
- const pagesCount = Math.ceil(res.profile.totalFollowUsers / 30)
- const pagesOffsets = Array(pagesCount).fill(0).map((x,i) => i*30)
- return Promise.all(
- pagesOffsets.map( (x) => pixiv.userFollowing(userID, {offset: x}) )
- ).then( responses => {
- const nestedUsers = responses.map((page) =>
- page.userPreviews.map((prev) => { return {
- id: prev.user.id,
- name: prev.user.id,
- account: prev.user.account,
- avatarURL: prev.user.profileImageUrls.medium
- } })
- )
- const flattedUsers = [].concat.apply([], nestedUsers)
- return { pixiv, following: flattedUsers }
- })
- })
- }
- // =============================================================================
- const MY_AUTH_INFO = undefined // можно не логинится
- const SOME_ID = 393516
- initPixiv( MY_AUTH_INFO )
- .then(res => getFollowingUsers({ pixiv: res.pixiv, userID: SOME_ID }))
- .then(res => console.log(res.following))
- .catch(err => console.log(err))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement