2023-01-11 01:16:25 +00:00
import { config } from "dotenv" ;
import fetch from 'node-fetch' ;
config ( ) ;
2023-01-17 16:54:44 +00:00
let lidarrUrl = process . env . LIDARR _URL || ` http://localhost:8686 ` ;
2023-01-11 01:16:25 +00:00
if ( lidarrUrl . endsWith ( ` / ` ) )
lidarrUrl = lidarrUrl . slice ( 0 , - 1 ) ;
const apiKey = process . env . LIDARR _API _KEY ;
2023-01-11 19:16:45 +00:00
const unmonitor = process . env . UNMONITOR || ` false ` ;
2023-01-25 22:04:34 +00:00
const printUnmonitored = process . env . OUTPUT _UNMONITORED || ` false ` ;
2023-01-26 14:38:23 +00:00
const doDelete = process . env . DELETE || ` false ` ;
2023-01-11 01:16:25 +00:00
2023-01-11 18:27:48 +00:00
try
{
let apiCheck = await fetch ( ` ${ lidarrUrl } /api?apikey= ${ apiKey } ` ) ;
apiCheck = await apiCheck . json ( ) ;
if ( ! Object . prototype . hasOwnProperty . call ( apiCheck , ` current ` ) )
2023-02-27 01:43:06 +00:00
throw new Error ( ` Lidarr API is not available. ` ) ;
2023-01-11 18:27:48 +00:00
}
catch
{
2023-02-27 01:43:06 +00:00
console . log ( ` Lidarr API is not available. \n Do you have the right URL & API Key? \n You also need to be on the latest develop version of Lidarr. ` ) ;
2023-01-11 18:27:48 +00:00
process . exit ( 1 ) ;
}
2023-01-11 01:16:25 +00:00
let artists = await fetch ( ` ${ lidarrUrl } /api/v1/artist?apikey= ${ apiKey } ` ) ;
artists = await artists . json ( ) ;
2023-01-26 14:02:41 +00:00
const artistIdsObject = artists . map ( ( artist ) => artist . id ) ; // Used to be artist.artistMetadataId but it wasn't returning the correct results?
2023-01-11 01:16:25 +00:00
for ( const artistId of artistIdsObject )
{
let albums = await fetch ( ` ${ lidarrUrl } /api/v1/album?artistId= ${ artistId } &includeAllArtistAlbums=true&apikey= ${ apiKey } ` ) ;
albums = await albums . json ( ) ;
let singles = albums . filter ( ( album ) => album . albumType === ` Single ` ) ;
2023-01-25 22:04:34 +00:00
singles = printUnmonitored === ` true ` ? singles . filter ( ( album ) => album . monitored === false ) : singles . filter ( ( album ) => album . monitored === true ) ;
2023-01-11 01:16:25 +00:00
let everythingElse = albums . filter ( ( album ) => album . albumType !== ` Single ` ) ;
everythingElse = everythingElse . filter ( ( album ) => album . monitored === true ) ;
2023-01-11 18:27:48 +00:00
const singleNamesLower = [ ] ;
2023-01-11 01:16:25 +00:00
const singleNames = [ ] ;
const singleIds = [ ] ;
for ( const single of singles )
{
2023-01-11 18:27:48 +00:00
singleNamesLower . push ( single . title . toLowerCase ( ) ) ;
2023-01-11 01:16:25 +00:00
singleNames . push ( single . title ) ;
singleIds . push ( single . id ) ;
}
2023-01-26 12:22:17 +00:00
for ( const album of everythingElse )
2023-01-11 01:16:25 +00:00
{
2023-01-26 14:02:41 +00:00
if ( album . releaseDate > new Date ( ) . toISOString ( ) )
// eslint-disable-next-line no-continue
continue ; // Allow duplicate singles to be monitored if the album they are on is not released yet.
2023-01-26 12:22:17 +00:00
let trackList = await fetch ( ` ${ lidarrUrl } /api/v1/track?artistId= ${ artistId } &albumId= ${ album . id } &apikey= ${ apiKey } ` ) ;
trackList = await trackList . json ( ) ;
if ( trackList . some ( ( track ) => singleNamesLower . includes ( track . title . toLowerCase ( ) ) ) )
2023-01-11 01:16:25 +00:00
{
2023-01-26 12:22:17 +00:00
const trackName = trackList . find ( ( track ) => singleNamesLower . includes ( track . title . toLowerCase ( ) ) ) . title ;
const singleId = singleIds [ singleNamesLower . indexOf ( trackName . toLowerCase ( ) ) ] ;
2023-01-11 01:16:25 +00:00
const artistName = artists . find ( ( artist ) => artist . id === artistId ) . artistName ;
2023-01-26 12:22:17 +00:00
if ( printUnmonitored === ` true ` )
{
const single = singles . find ( ( album ) => album . id === singleId ) ;
if ( single . statistics . trackFileCount > 0 )
2023-01-26 14:38:23 +00:00
{
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 } ` ) ;
}
2023-01-26 12:22:17 +00:00
}
else if ( printUnmonitored === ` false ` && unmonitor === ` true ` )
2023-01-11 19:16:45 +00:00
{
2023-01-26 12:22:17 +00:00
await fetch ( ` ${ lidarrUrl } /api/v1/album/monitor?apikey= ${ apiKey } ` , {
method : ` PUT ` ,
headers : {
"Content-Type" : ` application/json `
} ,
body : JSON . stringify ( {
albumIds : [ singleId ] ,
monitored : false
2023-01-26 14:38:23 +00:00
} ) ,
2023-01-26 12:22:17 +00:00
} ) ;
2023-01-26 14:38:23 +00:00
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 } ` ) ;
2023-01-11 19:16:45 +00:00
}
2023-01-26 12:22:17 +00:00
else if ( printUnmonitored === ` false ` && unmonitor === ` false ` )
console . log ( ` " ${ singleNames [ singleIds . indexOf ( singleId ) ] } " is also found on ${ album . title } by ${ artistName } ` ) ;
2023-01-11 01:16:25 +00:00
}
}
}