从Discogs添加豆瓣条目

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

// ==UserScript==
// @name         从Discogs添加豆瓣条目
// @license MIT
// @namespace    http://tampermonkey.net/
// @version      1.7
// @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
// ==/UserScript==

(function () {
  'use strict';
const doubanURL = 'https://music.douban.com/new_subject';
const discogsURL = window.location.href;

async function getRequest(url = '') {
  const response = await fetch(url);
  return response.json();
}

function newSubject() {
  const masterURL = 'https://www.discogs.com/master/';
  const releaseURL = 'https://www.discogs.com/release/';
  const apiURL = 'https://api.discogs.com/';
  let id;
  switch (true) {
    case discogsURL.startsWith(masterURL):
      id = discogsURL.substring(31, discogsURL.indexOf('-'));
      getRequest(apiURL.concat('masters/', id)).then(data => {
        const releaseID = data.main_release;
        addItem(releaseID);
      });
      break;
    case discogsURL.startsWith(releaseURL):
      id = discogsURL.substring(32, discogsURL.indexOf('-'));
      addItem(id);
      break;
  }
}

function addItem(releaseID) {
  getRequest(`https://api.discogs.com/releases/${releaseID}?token=tgRatMaOmFfXjBwHNBlZDQtXrOAELZwpywEOCEbb`)
    .then(data => {
      const info = {
        artists: [...data.artists],
        genres: [...data.genres],
        numArtists: data.artists.length,
        label: data.labels[0].name,
        title: data.title,
        format: data.formats[0].name,
        styles: data.styles ? [...data.styles] : 'Unknown',
        release: data.released ? data.released : 'Unknown',
        note: data.notes ? data.notes : 'None',
        country: data.country ? data.country : 'Unknown',
        link: discogsURL,
        tracklist: '',
        type: (() => {
          const descriptions = data.formats[0].descriptions;
          if (!descriptions) return '';
          switch (true) {
            case descriptions.includes('Album'):
            case descriptions.includes('LP'):
            case descriptions.includes('Stereo'):
            case descriptions.includes('12"'):
            case descriptions.includes('33 ⅓ RPM'):
              return '专辑';
            case descriptions.includes('Single'):
            case descriptions.includes('7"'):
            case descriptions.includes('45 RPM'):
              return '单曲';
            case descriptions.includes('Compilation'):
              return '选集';
            case descriptions.includes('EP'):
            case descriptions.includes('Mini-Album'):
              return 'EP';
            default:
              return '';
          }
        })(),
      };

      for (const track of data.tracklist) {
        if (track.type_ == 'heading') {
          info.tracklist += track.title + '\n';
        }
        if (track.type_ == 'track') {
          info.tracklist += `${track.position}. ${track.title} ${track.duration}\n`;
        }
      }

      console.log(info);
      if (data.images) {
        GM_download(data.images[0].uri, `${info.title}.jpg`);
      }
      const doubanWindow = window.open(doubanURL, JSON.stringify(info));
    });
}

GM_registerMenuCommand('添加条目', newSubject, '');


}
)();