Created
February 1, 2023 18:08
-
-
Save tbtstw/f1f2f060e21fc88bbe77f6177b9b750e to your computer and use it in GitHub Desktop.
Spotify Takedown Report - Google App Script
This file contains 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
function processTakedownMessages() | |
{ | |
var threads = GmailApp.search("from:(spotify.com) subject:\"takedown notification\""); | |
var messages=[]; | |
threads.forEach(function(thread) | |
{ | |
thread.getMessages().forEach(function(message) | |
{ | |
messages.push(message); | |
}); | |
}); | |
let takedowns = getPlaylistIdsFromMessages(messages); | |
// Write to json. | |
saveAsJSON(takedowns); | |
} | |
function saveAsJSON(obj) { | |
var blob,file,fileSets; | |
/** | |
* Updates a file in the user's Google Drive | |
*/ | |
fileSets = { | |
title: 'SpotifyTakedownReport.json', | |
mimeType: 'application/json' | |
}; | |
blob = Utilities.newBlob(JSON.stringify(obj), "application/vnd.google-apps.script+json"); | |
// Use this the very first time. Then comment it out. | |
file = Drive.Files.insert(fileSets, blob); | |
// After your file is created, get the id from the log, update below, and comment out these two lines. | |
//let fileId = '<insert your file id here after it is created>'; | |
//file = Drive.Files.update(fileSets, fileId, blob); | |
Logger.log('ID: %s, File size (bytes): %s, type: %s', file.id, file.fileSize, file.mimeType); | |
} | |
const regex = /:playlist:([\w]{22})\*/; | |
function getPlaylistIdsFromMessages(messages) | |
{ | |
var playlists = {}; | |
messages.forEach(function(message) | |
{ | |
var dateObj = message.getDate(); | |
var text = message.getPlainBody(); | |
// Format date. | |
var year = dateObj.toLocaleString("default", { year: "numeric" }); | |
var month = dateObj.toLocaleString("default", { month: "2-digit" }); | |
var day = dateObj.toLocaleString("default", { day: "2-digit" }); | |
var date = year + "-" + month + "-" + day; | |
// Look for the spotify id. | |
var matches = regex.exec(text); | |
if (!matches || matches < 2) | |
{ | |
console.log("No match found in text: " + text); | |
return; | |
} | |
var playlistId = matches[1]; | |
var dayTakedowns = playlists[date]; | |
if (!dayTakedowns) | |
{ | |
dayTakedowns = {}; | |
} | |
var takedowns = dayTakedowns[playlistId]; | |
var count = 1; | |
if (takedowns) | |
{ | |
count = takedowns + 1; | |
} | |
dayTakedowns[playlistId] = count; | |
playlists[date] = dayTakedowns; | |
}); | |
console.log(playlists); | |
return playlists; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment