Publish a book to Availabooks
Getting Book Data .
Working Version:
Working Chatper:
const bookInfo={} const deploymentId="AKfycbxwbEHJvXcaxmfEEYAmvQH5Qh-GtQSl9FAuti28PWj26_IUBv306-UMWxQ4HTkDamO-tA" const loadingTimer={ seconds:0 } function gasUrl(){ return `https://script.google.com/macros/s/${deploymentId}/exec` } function init(){ console.log("at init") const urlParams = new URLSearchParams( window.location.search) bookInfo.source={ blogId:urlParams.get('blogid'), blogName:urlParams.get('blog'), year:urlParams.get('year'), month:urlParams.get('month'), postTitle:urlParams.get('title') } bookInfo.build={ token:urlParams.get('token') } loadingTimer.counter= window.setInterval(updateProgress, 1000) processBook() } function showChapters(){ const chapters = bookInfo.source.settings.versions[tag("versions").value].chapters tag("chapters").replaceChildren() for(const [chapterNumber, chapter] of Object.entries(chapters)){ const newOption = document.createElement("option"); newOption.value = chapter.postId; newOption.text = chapterNumber; tag("chapters").add(newOption); } } function updateProgress(){ console.log("it's been a second") loadingTimer.seconds++ tag("progress").textContent += " ." } function tag(id){ return document.getElementById(id) } async function processBook(){ const source = await getSource() bookInfo.source.posts = source.posts bookInfo.source.settings = jsyaml.load(bookInfo.source.posts[source.settingsIndex].content) // Configure settings for(const [versionName, version] of Object.entries(source.versions)){ // fill in chapters if(!bookInfo.source.settings.versions[versionName].chapters){ bookInfo.source.settings.versions[versionName].chapters={} for(let x=1;x<=bookInfo.source.settings.bookData.lastChapter;x++){ bookInfo.source.settings.versions[versionName].chapters[x.toString()]={} } } // integrate version data for(const [key,val] of Object.entries(version)){ bookInfo.source.settings.versions[versionName][key]=val } } const destBlogIds=[] // make Version Map bookInfo.source.settings.versionMap={} for(const [versionName, version] of Object.entries(bookInfo.source.settings.versions)){ if(!destBlogIds.includes(version.blogId)){ destBlogIds.push(version.blogId) } if(!bookInfo.source.settings.versionMap[version.year]){ bookInfo.source.settings.versionMap[version.year]={} } bookInfo.source.settings.versionMap[version.year][version.month]=version } // get Version post ids for(const destBlogId of destBlogIds){ const pubfeed = await getBlogData(`https://www.blogger.com/feeds/${destBlogId}/posts/default`) for(post of pubfeed.entry){ publishedArray = post.published.$t.split("-") yr=publishedArray[0] mo=parseInt(publishedArray[1]) if(bookInfo.source.settings.versionMap[yr] && bookInfo.source.settings.versionMap[yr][mo]){ // we have a version with that is published to this blog, year, and month const linkData = getDataFromLinks(post.link) if(!isNaN(linkData.chapter)){ const postId = post.id.$t.split(".post-")[1] bookInfo.source.settings.versionMap[yr][mo].chapters[linkData.chapter].postId=postId } } } } console.log("bi1",bookInfo) // update interface clearInterval(loadingTimer.counter) tag("progress").textContent += " Done." // add versions to select for(const versionName of Object.keys(source.versions)){ const newOption = document.createElement("option"); newOption.value = versionName; newOption.text = versionName; tag("versions").add(newOption); } showChapters() tag("tools").style.display="" tag("progress").style.visibility="hidden" } function getDataFromLinks(links){ const values={} console.log("links", links) for(const link of links){ if(link.rel==='alternate'){ data=link.href.split("/") values.year=parseInt(data[3]) values.month=parseInt(data[4]) values.blog=data[2].split(".")[0] values.chapter=data[5].split(".")[0] break } } return values } async function getSource(){ const url = `${gasUrl()}?mode=source&token=${bookInfo.build.token}&blogid=${bookInfo.source.blogId}&year=${bookInfo.source.year}&month=${bookInfo.source.month}&debug=gamma` console.log("urlxd", url) const response = await fetch(url) const data = await response.json() console.log("getSource->data",data) for(let x=0;x<data.posts.length;x++){ const post = data.posts[x] console.log("-->",post.title) if (post.title==="settings"){ data.settingsIndex = x break } } return data } async function getBlogData(url){ console.log("url", url) const response = await fetch(`${url}?alt=json&max-results=0`) const data = await response.json() postCount=data.feed.openSearch$totalResults.$t data.feed.entry=[] const pageSize=500 const requests=[] for(let x=0;x
response.json())) } await Promise.all(requests) for(let r=0;r
{ data.feed.entry.push(...JSON.parse(JSON.stringify(code.feed.entry))) }) } return data.feed }