Advertisement
Guest User

I FUCK DOGS

a guest
Apr 29th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2.  
  3. const Promise = require('es6-promise').Promise;
  4. const PixivAppApi = require('pixiv-app-api')
  5. /* PixivAppApi.prototype.
  6. constructor(username?: string, password?: string): PixivAppApi
  7. login(username?: string, password?: string): Promise
  8. userDetail(id: ID, params?: Object): Promise
  9.     params = { restrict='public', offset=0 }
  10. userFollowing(id: ID, params?: Object): Promise
  11. */
  12.  
  13. // =============================================================================
  14.  
  15. function initPixiv( authInfo ) {
  16.     const pixiv = new PixivAppApi()
  17.     if (authInfo) {
  18.         return pixiv.login(authInfo.username, authInfo.password).then(() => {
  19.             return { pixiv }
  20.         })
  21.     } else {
  22.         return Promise.resolve({ pixiv })
  23.     }
  24. }
  25.  
  26. function getFollowingUsers( {pixiv, userID} ) {
  27.     return pixiv.userDetail(userID).then((res) => {
  28.         const pagesCount = Math.ceil(res.profile.totalFollowUsers / 30)
  29.         const pagesOffsets = Array(pagesCount).fill(0).map((x,i) => i*30)
  30.         return Promise.all(
  31.             pagesOffsets.map( (x) => pixiv.userFollowing(userID, {offset: x}) )
  32.         ).then( responses => {
  33.             const nestedUsers = responses.map((page) =>
  34.                 page.userPreviews.map((prev) => { return {
  35.                     id: prev.user.id,
  36.                     name: prev.user.id,
  37.                     account: prev.user.account,
  38.                     avatarURL: prev.user.profileImageUrls.medium
  39.                 } })
  40.             )
  41.             const flattedUsers = [].concat.apply([], nestedUsers)
  42.             return { pixiv, following: flattedUsers }
  43.         })
  44.     })
  45. }
  46.  
  47. // =============================================================================
  48.  
  49. const MY_AUTH_INFO = undefined // можно не логинится
  50. const SOME_ID = 393516
  51.  
  52. initPixiv( MY_AUTH_INFO )
  53.     .then(res => getFollowingUsers({ pixiv: res.pixiv, userID: SOME_ID }))
  54.     .then(res => console.log(res.following))
  55.     .catch(err => console.log(err))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement