DEV Community

Discussion on: Pulling your DEV Organization Stats into a Spreadsheet

Collapse
 
andrewbrown profile image
Andrew Brown 🇨🇦 • Edited

If you want this as json instead of csv

function articles(){
    const results = []
    const articles = document.querySelectorAll('.single-article')
    let data;
    for(let i = 0; i < articles.length; i++){
        data = article(articles[i])
        if (data) {
            results.push(data)
        }
    }
    return results
}

function article(article){
    // if there is no time selector that means this article 
    // is not published and will have no useful stats
    if (article.querySelector('time')){
        const name = article.querySelector('h2').innerText
        const tags = article.querySelectorAll('.tag')
        const author = article.querySelector('option[selected=selected]').innerText
        const date = article.querySelector('time').innerText
        const page_view_count = article.querySelector('.page-views-count').innerText
        const reactions_count = article.querySelector('.reactions-count').innerText
        const comments_count = article.querySelector('.comments-count').innerText
        const tags_string = []  
        for(let t = 0; t < tags.length; t++){
            tags_string.push(tags[t].innerText)
        }   
        return {
            name: name,
            tags: tags_string.join(' '),
            author: author,
            date: date,
            page_views_count: page_view_count,
            reactions_count: reactions_count,
            comments_count: comments_count
        }
    } else {
        return false        
    }
}

function save_data(){
  const results = JSON.stringify(articles())
  var blob = new Blob([results], {type: 'text/json'})
  let e    = document.createEvent('MouseEvents')
  let a    = document.createElement('a')
  const date = new Date().getTime()
  const epoch = Math.round(date / 1000)

  a.download = `export-${epoch}.json`
  a.href = window.URL.createObjectURL(blob)
  a.dataset.downloadurl =  ['text/json', a.download, a.href].join(':')
  e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
  a.dispatchEvent(e)
}

save_data()
Enter fullscreen mode Exit fullscreen mode