Skip to content

Instantly share code, notes, and snippets.

@kthjm
Last active January 19, 2018 03:49
Show Gist options
  • Save kthjm/ea657a9e4d2be85fc77aa437ee5f71d5 to your computer and use it in GitHub Desktop.
Save kthjm/ea657a9e4d2be85fc77aa437ee5f71d5 to your computer and use it in GitHub Desktop.
const tumblrSamplingTags = (() => {
const appendScript = (src) =>
new Promise((resolve, reject) => {
const s = document.createElement('script')
s.src = src
s.onload = resolve
s.onerror = reject
document.head.appendChild(s)
})
const appendScripts = (srcs) => Promise.all(srcs.filter(src => src).map(src => appendScript(src)))
const asyncCreateTags = async (iterator, tags = new Set()) => {
const { value, done } = iterator.next()
const fetchtags = await value
fetchtags.forEach(tag => tags.add(tag))
return done
? [...tags.values()]
: asyncCreateTags(iterator, tags)
}
return (account) => {
const origin = account ? `https://${account}.tumblr.com` : location.origin
const path = `${origin}/api/read/json`
const timeout = 10000
const times = 4
const maxnum = 3 // max === 50
return appendScripts([
!window.jsonpSimple && 'https://cdn.jsdelivr.net/npm/jsonp-simple/dist/jsonp-simple.min.js',
!window.tiloop && 'https://cdn.jsdelivr.net/npm/tiloop/min.js'
])
.then(() => jsonpSimple(`${path}?num=0`, timeout))
.then(res => res['posts-total'])
.then(total => {
console.log(total)
const iterator = tiloop.default(
new tiloop.IndexesRandom({
length: total,
maxIncrement: Math.floor(total / times)
}),
(indexes) => {
console.log(indexes)
const start = indexes[0]
const num = indexes.length > maxnum ? maxnum : indexes.length
return jsonpSimple(`${path}?start=${start}&num=${num}`,timeout)
.then(res => [].concat(...res.posts.map(({ tags }) => tags).filter(tags => tags)))
}
)
return iterator
})
.then(iterator => asyncCreateTags(iterator))
.then(tags => tags.map(tag => ({ tag, url: `${origin}/tagged/${encodeURIComponent(tag)}` })))
.catch(err => console.error(err))
}
})()
const tumblrSamplingTags=(()=>{const a=d=>new Promise((e,f)=>{const g=document.createElement('script');g.src=d,g.onload=e,g.onerror=f,document.head.appendChild(g)}),b=d=>Promise.all(d.filter(e=>e).map(e=>a(e))),c=async(d,e=new Set)=>{const{value:f,done:g}=d.next(),h=await f;return h.forEach(i=>e.add(i)),g?[...e.values()]:c(d,e)};return d=>{const e=d?`https://${d}.tumblr.com`:location.origin,f=`${e}/api/read/json`,g=1e4,i=3;return b([!window.jsonpSimple&&'https://cdn.jsdelivr.net/npm/jsonp-simple/dist/jsonp-simple.min.js',!window.tiloop&&'https://cdn.jsdelivr.net/npm/tiloop/min.js']).then(()=>jsonpSimple(`${f}?num=0`,g)).then(j=>j['posts-total']).then(j=>{console.log(j);const k=tiloop.default(new tiloop.IndexesRandom({length:j,maxIncrement:Math.floor(j/4)}),l=>{console.log(l);const m=l[0],n=l.length>i?i:l.length;return jsonpSimple(`${f}?start=${m}&num=${n}`,g).then(o=>[].concat(...o.posts.map(({tags:p})=>p).filter(p=>p)))});return k}).then(j=>c(j)).then(j=>j.map(k=>({tag:k,url:`${e}/tagged/${encodeURIComponent(k)}`}))).catch(j=>console.error(j))}})();
@kthjm
Copy link
Author

kthjm commented Jan 19, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment