From 7a8f78746656f300caafe3817b3d55af40b4d4ba Mon Sep 17 00:00:00 2001 From: chase Date: Thu, 26 Jan 2023 08:38:23 -0600 Subject: [PATCH] Closes #12. (Untested) --- .env.example | 1 + README.md | 15 +++++++++------ src/app.js | 31 ++++++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index 3971488..8a2dceb 100644 --- a/.env.example +++ b/.env.example @@ -2,3 +2,4 @@ LIDARR_URL="https://lidarr.domain.tld" LIDARR_API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" UNMONITOR="false" OUTPUT_UNMONITORED="false" +DELETE="false" diff --git a/README.md b/README.md index 525251b..5d80ca4 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ docker run --rm \ -e LIDARR_URL=http://localhost:8686 \ -e LIDARR_API_KEY=your_api_key \ -e UNMONITOR=false \ + -e DELETE=false \ -e OUTPUT_UNMONITORED=false \ ghcr.io/chxseh/lidarr-true-singles:latest ``` @@ -27,14 +28,16 @@ docker run --rm \ | `LIDARR_URL` | The URL to your Lidarr instance | `http://localhost:8686` | | `LIDARR_API_KEY` | Your Lidarr API Key | | | `UNMONITOR` | Whether to unmonitor the tracks or not | `false` | +| `DELETE` | Whether to delete the tracks or not | `false` | | `OUTPUT_UNMONITORED` | Print unmonitored singles that are downloaded. Note that this overrides the `UNMONITOR` option. | `false` | -| Variable | Option | Effect | -| -------------------- | ------- | ------------------------------------------------------------------------------------------------------- | -| `MONITOR` | `false` | Will *just* tell you what singles it finds on albums | -| `MONITOR` | `true` | Will *just* unmonitor said singles and let you know that it unmonitored them | -| `OUTPUT_UNMONITORED` | `false` | Will do nothing (falls back to what you have `MONITOR` set to, if any) | -| `OUTPUT_UNMONITORED` | `true` | Will *just* tell you about **any** single that is unmonitored and downloaded, with a link to delete it. | +| Environment Variable | Outcome | +| ------------------------------------------ | -------------------------------------------------------- | +| `UNMONITOR=false` | See duplicate singles | +| `UNMONITOR=true` | Unmonitor duplicate singles | +| `UNMONITOR=true`
`DELETE=true` | Delete duplicate singles | +| `OUTPUT_UNMONITORED=true` | See duplicate unmonitored singles that are downloaded | +| `OUTPUT_UNMONITORED=true`
`DELETE=true` | Delete duplicate unmonitored singles that are downloaded | ## Development ```bash diff --git a/src/app.js b/src/app.js index 905affe..b23bd7b 100644 --- a/src/app.js +++ b/src/app.js @@ -10,6 +10,7 @@ if (lidarrUrl.endsWith(`/`)) const apiKey = process.env.LIDARR_API_KEY; const unmonitor = process.env.UNMONITOR || `false`; const printUnmonitored = process.env.OUTPUT_UNMONITORED || `false`; +const doDelete = process.env.DELETE || `false`; try { @@ -67,7 +68,20 @@ for (const artistId of artistIdsObject) { const single = singles.find((album) => album.id === singleId); if (single.statistics.trackFileCount > 0) - console.log(`"${ singleNames[singleIds.indexOf(singleId)] }" by ${ artistName } is downloaded but unmonitored, consider deleting it. ${ lidarrUrl }/album/${ single.foreignAlbumId }`); + { + if (doDelete === `true`) + { + await fetch(`${ lidarrUrl }/api/v1/album/${ singleId }?apikey=${ apiKey }`, { + method: `DELETE`, + headers: { + "Content-Type": `application/json` + }, + }); + console.log(`"${ singleNames[singleIds.indexOf(singleId)] }" by ${ artistName } deleted.`); + } + else + console.log(`"${ singleNames[singleIds.indexOf(singleId)] }" by ${ artistName } is downloaded but unmonitored, consider deleting it. ${ lidarrUrl }/album/${ single.foreignAlbumId }`); + } } else if (printUnmonitored === `false` && unmonitor === `true`) { @@ -79,9 +93,20 @@ for (const artistId of artistIdsObject) body: JSON.stringify({ albumIds: [singleId], monitored: false - }) + }), }); - console.log(`Unmonitored "${ singleNames[singleIds.indexOf(singleId)] }" by ${ artistName }`); + if (doDelete === `true`) + { + await fetch(`${ lidarrUrl }/api/v1/album/${ singleId }?apikey=${ apiKey }`, { + method: `DELETE`, + headers: { + "Content-Type": `application/json` + }, + }); + console.log(`"${ singleNames[singleIds.indexOf(singleId)] }" by ${ artistName } unmonitored & deleted.`); + } + else + console.log(`Unmonitored "${ singleNames[singleIds.indexOf(singleId)] }" by ${ artistName }`); } else if (printUnmonitored === `false` && unmonitor === `false`) console.log(`"${ singleNames[singleIds.indexOf(singleId)] }" is also found on ${ album.title } by ${ artistName }`);