Groupees Backup

备份 Groupees 数据。

Tính đến 24-06-2022. Xem phiên bản mới nhất.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Groupees Backup
// @namespace    https://greatest.deepsurf.us/users/34380
// @version      20220624
// @description
// @match        https://groupees.com/purchases
// @grant        none
// @description 备份 Groupees 数据。
// ==/UserScript==

(function () {
    'use strict';

    // Your code here...

    var id, page, generator, all, text;
    var plats = ["Steam", "Itch.io", "Desura"];
    var kws = ["PC", "OS X", "Linux", "Android", "Mp3", "Flac"];
    var user_id = document.querySelector('.fayepub').getAttribute('data-user');

    function getNextPageBundle() {
        fetch('https://groupees.com/users/451393/more_entries?page=' + page + '&kind=bundles').then(res => res.json()).then(res => {
            if (res.length > 0) {
                generator = getBundleHTML(res);
                generator.next();
            } else {
                toExcel();
            }
        })
    }

    function* getBundleHTML(bundles) {
        for (var bundle of bundles) {
            var bun = JSON.parse(bundle);
            if (id) {
                if (id == bun.id) { id = false; } else { console.log(generator.done); }
            } else {
                yield fetch('https://groupees.com/orders/' + bun.id + '?user_id=' + user_id).then(res => res.text()).then(res => {
                    // var html = res.match(/(?<=html = )[\s\S]+(?=;[\s\S]+var groupeesRe)/);
                    var html = res.match(/(?<=html = \$\(')[\s\S]+(?='\);[\s\S]+var groupeesRe)/);
                    var div = document.createElement('DIV');
                    html = html[0].replace(/\\n/g, "").replace(/\\'/g, "'").replace(/\\"/g, '"').replace(/\\\//g, "/");
                    div.innerHTML = html;
                    var [json, note] = getMainData(div);
                    all.push([bun, json, note]);
                    document.querySelector('#output-textarea').value += '已加载包 id ' + bun.id + ' ' + bun.bundle_name + '\n';

                    setTimeout(() => {
                        if (generator.next().done == true) {
                            page++;
                            getNextPageBundle();
                        }
                    }, 3000);
                });
            }
        }
        if (id && bundles.length) { page++; getNextPageBundle(); }
    }

    function getMainData(div) {
        var jsons = [];
        var items = div.querySelectorAll('.product');
        var note = div.querySelector('.announcements');
        note = note ? note.innerText.replace(/\n/g, ' ') : '';
        for (var item of items) {
            var obj = {};
            obj['name'] = item.querySelector('.product-name').innerText;

            var action = item.querySelector('.product-meta');
            if (action) {
                if (action.querySelector('.icon-givenaway-arrow')) { obj['Steam'] = { "key": "", "status": "Givenaway" }; }
                else { obj['Steam'] = { "key": "", "status": "Reveal this product" }; }
            } else {
                var keys_rows = item.querySelectorAll('.col-sm-6.key');
                for (var row of keys_rows) {
                    var type = row.querySelector('strong').innerText;
                    var key = row.querySelector('.form-control.code');
                    // if (!key)  {document.querySelector('.key-meta').innerText;}
                    var value = key.value;
                    var status = key.disabled;
                    if (row.querySelector('.key-meta .green')) { status = 'Givenaway'; }
                    if (obj[type]) { note += type + ' ' + obj['name'] + ': ' + value + ' ' + status + '. '; }
                    if (!plats.includes(type)) { note += type + ' ' + obj['name'] + ': ' + value + ' ' + status + '. '; }
                    obj[type] = { "key": value, "status": status };
                }

                var link_row = item.querySelectorAll('.dropdown-header');
                for (var row of link_row) {
                    var type = row.innerText;
                    var link = row.nextElementSibling.children[0].href;
                    if (obj[type]) { note += type + ' ' + obj['name'] + ': ' + link + ' . '; }
                    if (!kws.includes(type)) { note += type + ' ' + obj['name'] + ': ' + link + ' . '; }
                    obj[type] = link;
                }
            }
            jsons.push(obj);
        }
        return [jsons, note];
    }

    function toExcel() {
        text = '包名	价格	日期	名字	Steam	状态	Itch	状态	Desura	状态	PC	OS X	Linux	Android	Mp3	Flac\n';
        for (var one of all) {
            var text_bun = one[0].bundle_name + '	' + one[0].total_amount + '	' + one[0].completed_at + '	';
            for (var item of one[1]) {
                var text_item = item.name + '	';
                for (var plat of plats) {
                    if (item[plat]) {
                        text_item += item[plat].key + '	' + item[plat].status + '	';
                    } else {
                        text_item += '	' + '	';
                    }
                }

                for (var kw of kws) {
                    text_item += (item[kw] || '') + '	';
                }
                text += text_bun + text_item + '\n';
            }
            if (one[2] != '') { text += one[2] + '\n'; }
        }
        document.querySelector('#output-textarea').value = text;
    }

    document.querySelector('.pre-nav').insertAdjacentHTML('afterend', `
        <div>
            <input id="bun-id" type="text" placeholder="留空或输入 id 继续上次加载" style="width:200px;">
            <button id="run-script"type="button">运行备份脚本</button>
            <button id="excel-data" type="button">Excel 数据</button>
            <textarea id="output-textarea" style="display:block; wide:100%;"></textarea>
        </div>
    `);

    document.querySelector('#run-script').addEventListener('click', () => {
        page = 1, all = [];
        id = document.querySelector('#bun-id').value;
        id = id == "" ? false : parseInt(id, 10);
        getNextPageBundle();
        document.querySelector('#output-textarea').value = '加载中\n';
    });

    document.querySelector('#excel-data').addEventListener('click', () => {
        toExcel();
    });
})();