Export List of Seeding Torrents

Export list of your currently seeding torrents from TorrentBD.

// ==UserScript==
// @name         Export List of Seeding Torrents
// @namespace    dunno what to put here :|
// @version      1.0
// @description  Export list of your currently seeding torrents from TorrentBD.
// @author       K4K4SH1
// @match        https://*.torrentbd.com/seedbonus-breakdown.php
// @match        https://*.torrentbd.net/seedbonus-breakdown.php
// @match        https://*.torrentbd.me/seedbonus-breakdown.php
// @match        https://*.torrentbd.com/activities.php
// @match        https://*.torrentbd.net/activities.php
// @match        https://*.torrentbd.me/activities.php
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QA/wD/AP+gvaeTAAAAB3RJTUUH5gwKDQI3JNcLhAAAERdJREFUeNrVm3uUXEWdxz9VdW+/5v3IUxISNgmJhGiUYDQs0TwgLsbIKmh2OXJWXd6irnJYkSCg7spxxVUTgSAg6p6VXTQR0EVAD8oKapT3K8ExZJYoec2ju6f7dt9bVfvHvX27Z9IzmQ6TaOqce27P9K269fv+fvV7fKsaGmsiugBSwG2AD9g/0zUEfBvojuakauY5boEaBUABAXAj8Im2tjbd3dWtDmu019CstfT29togCISU8kljzBpgTzQ/Hc3GHgkA7OzZs5M7d+7c3tTUdPw9W+4xc+fOlal0CinlURFeSkm5XOYtS9/Crt5dgRDCEUI8a4xZ1SgIjc5YAOhAp4B0d1c3s2bNEr7vI8TRUb+1FmstQgj8wKe1pdU5/a9P18aYhUqpB4EpkfAqEn7MiTUKgK29a60plUrDJna0LwR8Y+M31Joz12it9cmO4zQEwmFZQO1ftWZfsQIhxJG/CO9BEGCtZfPNm9Wqlat0EAQNgXB0Fu2RaJE4Ukp838cYw+abN6vVq1Y3BMKxC0DUrLWvCYRjHgARyaOUGgbCqlXjWw7HPAA28svW2mEg3HrzreMC4ZgGoKJ9Y0wMQiVH0Fqz+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NLODYs6LfAfosqZzWisXbnwf+b4h5i6ZSWmoHNUUtgrCCDqpEgWCYOxTJbXCN2IB4TaeDev5/YXwpRkn5AWyBwpkOjKc+p5FSBkOWir45PqL4cQF5Ac9lKN4wxnzUUpiLZQKZfKDXlgMVVJTAV6hzEnL5yCVpJDzhpHHldzf6BCMYbviQoy75h//9nhlh0UJVMLB+BqLYG8BnJh+ltiixfZ5w0pdMfL4ibWYgdLYz0TN5MoAsWXUjiGkAOtihKxQ0eMSutEWlcO2BBSG9uc7+nYdMJ0zO2WFqdUHhdGaDMsKga7z/bBngFEPaISCmxHfWwuOo9j/+30U+oaY9LpukonkEQHBAZzsq9kCsLWUL11653m3kuloEtXtrNGaJag9ejKhcwstoJQtgYZ169bR0dERO7eJBqAixRWA9bLeuV7WO9SGiQVUUyrTHrIxESl6yO2t8TaBsYaWTAsfvvjDXLPhGvL5fCz8RFpCLS1eau5u+agQ4koESajPV7e3tone7b3laz937XHvOvNdv5VCNlksmaYMqVQ63AidABCstaSSKbq7u4fF9MMdaywAKrQ4uT1ZByhEV93De888+rQ02hgE/Qk3YUulUhxuMqk0bsKdEM0IBMYY+vr64rB2qLx+PEDYEfxSbAFU3VXtTvxBkK9YtYKenh6Ar3zg/R9ovvHfbrTZbFZIKeO8eyJ/RDkRY9mIqnccJ84lKiFZxs9U72HiVf+yPT09ZsZxM1LAaffedy8vvPiCrRxUrqSnE/nDSGPMa750oFFKsXPnTvbu2wtQNNaU6mr4UAoh2kgRQnzJWvupyo+ntdGvbSPkCDaLRUnFgb4DemBgQAGbgMs4jPMRf2k/n2/0CoA7gUxFnv8HVkR6JHRK5MUAAAAldEVYdGRhdGU6Y3JlYXRlADIwMjItMTItMTBUMTM6MDI6MzgrMDA6MDAt6U2HAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDIyLTEyLTEwVDEzOjAyOjM4KzAwOjAwXLT1OwAAAABJRU5ErkJggg==
// @run-at       document-end
// @license		 MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict'

    const seedingList = document.querySelector('.simple-data-table tbody')

    // Classes
    class seedingListExporter {
        constructor (table, includeHeaders = true) {
            this.table = table
            this.rows = Array.from(table.querySelectorAll('tr'))

            if (!includeHeaders && this.rows[0].querySelectorAll('th').length) {
                this.rows.shift()
            }
        }

        convertToCSV() {
            const lines = []
            const numCols = this._findLongestRowLength()

            for (const row of this.rows) {
                let line = ''

                for (let i = 0; i < numCols; i++) {
                    if (row.children[i] !== undefined) {
                        line += seedingListExporter.parserCell(row.children[i])
                    }

                    line += (i !== (numCols - 1)) ? ',' : ''
                }

                lines.push(line)
            }
            
            return lines.join('\n')
        }

        _findLongestRowLength() { 
            return this.rows.reduce((l, row) => row.childElementCount > 1 ? row.childElementCount : l, 0)
        }

        static parserCell(tableCell) {
            let parsedValue = tableCell.textContent

            // Replace all the double quotes with dwo double quotes
            parsedValue = parsedValue.replace(/"/g, `""`)

            // If value contains comma, new-line or double-quotes, enclose in the doulbe quotes
            parsedValue = /[",\n]/.test(parsedValue) ? `"${parsedValue}"` : parsedValue
            
            parsedValue = parsedValue.replace(/\s+/g,' ').trim();
            // console.log(parsedValue)

            return parsedValue
        }
    }

    // Get day, month, year
    let date = new Date();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let year = date.getFullYear();

    let currentDate = `${day}-${month}-${year}`


    // Create a download button
    const exportCSVButton = document.createElement('a')
    const exportCSVButtonText = document.createTextNode('Export List as CSV')
    exportCSVButton.classList.add('btn', 'waves-effect', 'waves-light', 'center-align')
    exportCSVButton.appendChild(exportCSVButtonText)
    exportCSVButton.style.margin = '10px 0px'

    seedingList.parentElement.insertBefore(exportCSVButton, seedingList)

    exportCSVButton.addEventListener('click', () => {
        const exporter = new seedingListExporter(seedingList)
        const csvOutput = exporter.convertToCSV()
        const csvBlob = new Blob([csvOutput], {type: 'text/csv'})
        const blobURL = URL.createObjectURL(csvBlob)
        
        exportCSVButton.href = blobURL
        exportCSVButton.download = `Seeding-List-${currentDate}`

        setTimeout(() => {
            URL.revokeObjectURL(blobURL)
        }, 500)
    })
})();