Greasy Fork

从Discogs添加豆瓣条目

添加豆瓣条目,自动填写信息,使用https://api.discogs.com

目前为 2023-01-08 提交的版本。查看 最新版本

// ==UserScript==
// @name         从Discogs添加豆瓣条目
// @license MIT
// @namespace    http://tampermonkey.net/
// @version      1.0
// @license      MIT
// @run-at document-end
// @description  添加豆瓣条目,自动填写信息,使用https://api.discogs.com
// @author       越洋飞机
// @match        *://www.discogs.com/release*
// @match        *://www.discogs.com/master*
// @match        *://music.douban.com/new_subject*
// @icon         https://s.discogs.com/badaa268132c5df6360d067acd267fbebd755915/images/discogs-white.png?5
// @grant        GM_xmlhttpRequest
// @grant        GM_download
// @grant        GM_openInTab
// @grant        GM_addElement
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function () {
    'use strict';
    var doubanURL = "https://music.douban.com/new_subject";
    var discogsURL = document.URL;
    function ParseReleaseID() {
        let masterURL = "https://www.discogs.com/master/"
        let releaseURL = "https://www.discogs.com/release/"
        let apiURL = "https://api.discogs.com/"
        if (discogsURL.startsWith(masterURL)) {
            let pos = discogsURL.indexOf("-")
            let masterID = discogsURL.substring(31, pos)
            GetRequest(apiURL.concat("masters/",masterID),"master_json");
            while (true){
                if(localStorage.getItem("master_json")!=null){
                    let releaseID = JSON.parse(localStorage.getItem("master_json")).main_release;
                    return releaseID;
                }
            }
        } else if (discogsURL.startsWith(releaseURL)) {
            let pos = discogsURL.indexOf("-")
            let releaseID = discogsURL.substring(32,pos)
            return releaseID;
        }
    }
    function GetRequest(url,storageName) {
        var httpRequest = new XMLHttpRequest();
        var json;
        httpRequest.open('GET', url, true);
        httpRequest.send();
        httpRequest.onreadystatechange = function () {
            if (httpRequest.readyState == 4 && httpRequest.status == 200) {
                json = JSON.parse(httpRequest.responseText);
                localStorage.setItem(storageName,httpRequest.responseText);
            }
        };
    }
    function addItem() {
        console.log(ParseReleaseID());
        let releaseID = ParseReleaseID();
        var httpRequest = new XMLHttpRequest();
        httpRequest.open('GET', 'https://api.discogs.com/releases/'+releaseID+'?token=tgRatMaOmFfXjBwHNBlZDQtXrOAELZwpywEOCEbb', true);
        httpRequest.send();
        httpRequest.onreadystatechange = function () {
            if (httpRequest.readyState == 4 && httpRequest.status == 200) {
                var json = JSON.parse(httpRequest.responseText);
                console.log(json);
                var info = new Object();
                info.artists = [...json.artists];
                info.numArtists = info.artists.length;
                info.label = json.labels[0].name;
                info.title = json.title;
                info.format = json.formats[0].name;
                info.genres = [...json.genres];
                info.styles = [...json.styles];
                info.release = json.released;
                info.note = json.notes;
                info.country = json.country;
                info.link = discogsURL;
                info.tracklist = ''
                for (let index = 0; index < json.tracklist.length; index++) {
                    if(json.tracklist[index].type_=='heading'){
                        info.tracklist+=json.tracklist[index].title+'\n';
                    }
                    if(json.tracklist[index].type_=='track'){
                        info.tracklist+=json.tracklist[index].position+'. '+json.tracklist[index].title+' '+json.tracklist[index].duration+'\n';
                    }
                }
                if(json.formats[0].descriptions.indexOf("Album")>-1){
                    info.type = "专辑"
                }
                else if(json.formats[0].descriptions.indexOf("Single")>-1){
                    info.type = "单曲";
                }
                else if(json.formats[0].descriptions.indexOf("Compilation")>-1){
                    info.type = "选集";
                }
                else if(json.formats[0].descriptions.indexOf("EP")>-1){
                    info.type = "EP";
                }
                console.log(info);
                GM_download(json.images[0].uri,info.title+'.jpg');
                let doubanWindow = window.open(doubanURL,JSON.stringify(info));
            }
        };
    }
    GM_registerMenuCommand("添加条目", addItem, '');

}
)();