Last active
May 26, 2025 08:05
-
-
Save hansogj/5b2b5faafe18583b240d50799e1d219f to your computer and use it in GitHub Desktop.
Edit all listed items in your discogs collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const g = window || global; | |
const find = (selector, base = document) => Array.from(base.querySelectorAll(selector)); | |
const timer = (ms) => new Promise((res) => setTimeout(res, ms)); | |
const GRADES = { | |
M: 'Mint (M)', | |
NM: 'Near Mint (NM or M-)', | |
VG_P: 'Very Good Plus (VG+)', | |
VG: 'Very Good (VG)', | |
G_P: 'Good Plus (G+)', | |
G: 'Good (G)', | |
F: 'Fair (F)', | |
P: 'Poor (P)', | |
}; | |
const PLACEMENT = { | |
O: 'Oppe', | |
N: 'Nede', | |
B: 'Boden', | |
}; | |
const ORIGINS = { | |
HoG: 'HoG', | |
IGM: 'IGM', | |
OG: 'OG', | |
Gj: 'Gjerdrum', | |
Maus: 'Maus', | |
Skiaker: 'Skiaker', | |
}; | |
const NOTE_TYPES = { | |
MEDIA_CONDITION: 'mediaCondition', | |
SLEVE_CONDITION: 'sleveCondition', | |
ORIGIN: 'origin', | |
PLACEMENT: 'placement', | |
}; | |
const discogsNoteTypeIds = { | |
[NOTE_TYPES.MEDIA_CONDITION]: 1, | |
[NOTE_TYPES.SLEVE_CONDITION]: 2, | |
[NOTE_TYPES.ORIGIN]: 4, | |
[NOTE_TYPES.PLACEMENT]: 5, | |
}; | |
const grapQurl = 'https://www.discogs.com/service/catalog/api/graphql'; | |
const headers = { | |
accept: '*/*', | |
'accept-language': 'en-NO,en;q=0.9,nb-NO;q=0.8,nb;q=0.7,en-US;q=0.6,no;q=0.5', | |
'apollographql-client-name': 'release-page-client', | |
'content-type': 'application/json', | |
priority: 'u=1, i', | |
'sec-ch-ua': '"Google Chrome";v="135", "Not-A.Brand";v="8", "Chromium";v="135"', | |
'sec-ch-ua-mobile': '?0', | |
'sec-ch-ua-platform': '"Windows"', | |
'sec-fetch-dest': 'empty', | |
'sec-fetch-mode': 'cors', | |
'sec-fetch-site': 'same-origin', | |
cookie: document.cookie, | |
Referer: 'https://www.discogs.com/user/hansogj/collection?folder=1', | |
'Referrer-Policy': 'strict-origin-when-cross-origin', | |
}; | |
const crateBody = ({ discogsItemId, discogsNoteTypeId, noteText }) => ({ | |
operationName: 'EditCollectionItemNote', | |
variables: { | |
input: { discogsItemId, discogsNoteTypeId, noteText }, | |
}, | |
extensions: { | |
persistedQuery: { version: 1, sha256Hash: '759194518a1e8634735edc1b68d5c511b467fd1901249a7ac7d2d8387f7899db' }, | |
}, | |
}); | |
const print = async (body) => { | |
await timer(1500).then(() => console.dir({ headers, body, method: 'POST' })); | |
}; | |
const postGrapql = async (body) => | |
fetch(grapQurl, { | |
headers, | |
body, | |
method: 'POST', | |
}); | |
const currentValue = (item, noteType) => { | |
const selectElemOrder = { | |
[NOTE_TYPES.MEDIA_CONDITION]: 0, | |
[NOTE_TYPES.SLEVE_CONDITION]: 1, | |
[NOTE_TYPES.ORIGIN]: 2, | |
[NOTE_TYPES.PLACEMENT]: 3, | |
}; | |
const selectIndex = selectElemOrder[noteType]; | |
return find('select', item)[selectIndex]; | |
}; | |
const force = (item) => true; | |
const ifEmpty = (item, noteType) => !currentValue(item, noteType).value; | |
const equals = (comparator) => (item, noteType) => currentValue(item, noteType).value === comparator; | |
const editField = async (noteType, noteText, strategy = ifEmpty) => | |
find('[data-id]') | |
// .filter((e,i) => i ===0) | |
.filter((item) => strategy(item, noteType)) | |
.map((e, i) => parseInt(e.getAttribute('data-id'))) | |
.map((discogsItemId) => crateBody({ discogsItemId, noteText, discogsNoteTypeId: discogsNoteTypeIds[noteType] })) | |
.map(JSON.stringify) | |
.map(async (body) => await postGrapql(body).then((response) => console.log(`Updated item with values:`, response))); | |
const getItems = () => Array.from(document.querySelectorAll('[data-id]')); | |
const customNotes = (val) => `[data-field=custom-notes-${val}]`; | |
const rating = (n) => `[data-field=rating] > div:nth-of-type(${n})`; | |
g.discogs = g.discogs || {}; | |
g.discogs.setOrigin = async (val, force) => await editField(NOTE_TYPES.ORIGIN, val, force); | |
g.discogs.setPlacement = async (val, force) => await editField(NOTE_TYPES.PLACEMENT, val, force); | |
g.discogs.setMediaCondition = async (val, force) => await editField(NOTE_TYPES.MEDIA_CONDITION, val, force); | |
g.discogs.setSleeveCondition = async (val, force) => await editField(NOTE_TYPES.SLEVE_CONDITION, val, force); | |
g.discogs.setMediaAndSleeveCondition = async (val, force) => | |
[NOTE_TYPES.SLEVE_CONDITION, NOTE_TYPES.MEDIA_CONDITION].map(async (n) => await editField(n, val, force)); | |
g.discogs.defaults = async () => await Promise.all([ | |
discogs.setMediaAndSleeveCondition(GRADES.NM), | |
discogs.setOrigin(ORIGINS.HoG), | |
discogs.setPlacement(PLACEMENT.N) | |
]); | |
/* | |
USAGE | |
* discogs.setPlacement(PLACEMENT.N, equals(PLACEMENT.B)) / set placement NEDE if items current placement is Boden | |
* discogs.setPlacement(PLACEMENT.N) // set placenement NEDE if current placement is empty | |
* discogs.setPlacement(PLACEMENT.N, force) // set placenement NEDE !!! | |
*/ | |
console.log('discogs loaded'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Put in you snippets with