Skip to content

Commit

Permalink
Warn if library file not up to date.
Browse files Browse the repository at this point in the history
  • Loading branch information
krizleebear committed Aug 26, 2022
1 parent 7995989 commit afbaa0b
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/de/christianleberfinger/melodies2go/Melodies2Go.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -47,6 +48,7 @@ public static void main(String[] args)
long availableCapacityBytes = gigabytes * FileUtils.ONE_GB;

File itunesLibrary = findiTunesLibrary();
checkIfUpToDate(itunesLibrary);

Melodies2Go sync = new Melodies2Go();
List<ITrack> allTracks = sync.readiTunesLibrary(itunesLibrary);
Expand All @@ -67,7 +69,23 @@ public static void main(String[] args)

M3UWriter.writeRecentlyAdded(destPath, syncedTracks);
}


/**
* Apple Music doesn't automatically export the library as XML file.
* You have export manually via "File > Library > Export Library ...".
*
* This method detects suspiciously old library files and prints a warning.
*
* @param itunesLibrary
*/
protected static void checkIfUpToDate(File itunesLibrary) {
long fileAgeMillis = System.currentTimeMillis() - itunesLibrary.lastModified();
final long oneDayMillis = Duration.ofDays(1).toMillis();
if (fileAgeMillis > oneDayMillis) {
System.err.println(itunesLibrary.getAbsolutePath() + " seems not up to date. Maybe you need to export it manually in Apple Music?");
}
}

public static <T> List<T> limitTrackNumber(List<T> tracks, int maxCount) {

int toIndex = Math.min(maxCount, tracks.size());
Expand Down Expand Up @@ -162,7 +180,8 @@ public static File findiTunesLibrary() throws FileNotFoundException
while(files.hasNext())
{
File file = files.next();
if(file.getName().equals("iTunes Library.xml"))
if (file.getName().equals("iTunes Library.xml") // iTunes classic
|| file.getName().equals("Library.xml")) // Apple Music
{
return file;
}
Expand Down

0 comments on commit afbaa0b

Please sign in to comment.