// ==UserScript== // @name 从Discogs添加豆瓣条目 // @license MIT // @namespace http://tampermonkey.net/ // @version 1.0000000002 // @license MIT // @run-at document-end // @description // @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 // @description 添加豆瓣条目,自动填写信息,使用https://api.discogs.com // @downloadURL none // ==/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.genres = [...json.genres]; info.numArtists = info.artists.length; info.label = json.labels[0].name; info.title = json.title; info.format = json.formats[0].name; if(json.styles) { info.styles = [...json.styles]; } else { info.styles = "Unknown"; } if(json.released) { info.release = json.released; } else { info.release = "Unknown"; } if(json.notes) { info.note = json.notes; } else { info.note = "None"; } if(json.country) { info.country = json.country; } else { info.country = "Unknown"; } 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"; } else if(json.formats[0].descriptions.indexOf("LP")>-1||json.formats[0].descriptions.indexOf("Stereo")>-1||json.formats[0].descriptions.indexOf("12\"")>-1||json.formats[0].descriptions.indexOf("33 ⅓ RPM")>-1){ info.type = "专辑"; } else if(json.formats[0].descriptions.indexOf("Mini-Album")>-1){// whatever info.type = "EP"; } else if(json.formats[0].descriptions.indexOf("7\"")>-1||json.formats[0].descriptions.indexOf("45 RPM")>-1){ info.type = "单曲"; } console.log(info); if(json.images) { GM_download(json.images[0].uri,info.title+'.jpg'); } let doubanWindow = window.open(doubanURL,JSON.stringify(info)); } }; } GM_registerMenuCommand("添加条目", addItem, ''); } )();