Advertisement
Guest User

Untitled

a guest
Aug 15th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. sleepRandom = () => new Promise(r => setTimeout(r, Math.random() * 3000));
  2. function starURLs(name, page, myName) {
  3.   function formatDate(d) {
  4.     return d.getFullYear() + ('00' + (d.getMonth() + 1)).slice(-2) + ('00' + d.getDate()).slice(-2);
  5.   }
  6.  
  7.   return fetch(`http://b.hatena.ne.jp/api/users/${name}/bookmarks?page=${page}`)
  8.           .then(r => r.json())
  9.           .then(
  10.             a => a.item
  11.                   .bookmarks
  12.                   .map(i => `http://b.hatena.ne.jp/${i.user.name}/${formatDate(new Date(i.created))}#bookmark-${i.location_id}`)
  13.                   .map(url => `uri=${encodeURIComponent(url)}`).join('&')
  14.           )
  15.           .then(q => fetch(`http://s.hatena.ne.jp/entries.json?${q}`))
  16.           .then(r => r.json())
  17.           .then(
  18.             v => v.entries.filter(e => e.stars
  19.                                         .some(s => s.name == myName))
  20.                                         .map(e => e.uri)
  21.           )
  22. }
  23.  
  24. function listStarURLs(name, maxPage) {
  25.   fetch('http://b.hatena.ne.jp/api/my/profile')
  26.     .then(r => r.json())
  27.     .then(
  28.       r => [...new Array(maxPage).keys()].reduce(
  29.                                             (acc, x) => acc.then(l => {
  30.                                               console.log("Now " + l);
  31.                                               return sleepRandom().then(() => starURLs(name, x + 1, r.name))
  32.                                                                   .then(m => l.concat(m))
  33.                                             }),
  34.                                             Promise.resolve([])
  35.                                          )
  36.     )
  37.     .then(JSON.stringify)
  38.     .then(console.log)
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement