-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRBToPOPM.java
126 lines (110 loc) · 5.35 KB
/
RBToPOPM.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
/**
* Created by bozhidar on 11.10.17.
*/
public class RBToPOPM {
public static HashMap<Integer, Integer> starsToNumber = new HashMap<>();
static {
starsToNumber.putIfAbsent(0, 0);
starsToNumber.putIfAbsent(1, 1);
starsToNumber.putIfAbsent(2, 64);
starsToNumber.putIfAbsent(3, 128);
starsToNumber.putIfAbsent(4, 196);
starsToNumber.putIfAbsent(5, 255);
}
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_RESET = "\u001B[0m";
private static int total = 0;
public static void main(String... args) throws ParserConfigurationException, IOException, SAXException, URISyntaxException, InterruptedException, ExecutionException {
args = Files.readAllLines(Paths.get(args[0])).get(0).split(",;");
HashMap<String, File> files = new HashMap<>();
//Read all the files that are passed to the program
for (String arg : args) {
files.put(arg, new File(arg));
}
String database = System.getProperty("user.home") + "/.local/share/rhythmbox/rhythmdb.xml";
int threads = Runtime.getRuntime().availableProcessors();
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(threads);
System.out.println("Running program on " + threads + " threads");
File db = new File(database);
if (db.canRead()) {
//Read the RB database
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(db);
doc.getDocumentElement().normalize();
NodeList songs = doc.getElementsByTagName("entry");
//For all songs
for (int i = 0; i < songs.getLength(); i++) {
Node song = songs.item(i);
Element songElement = (Element) song;
Node location = songElement.getElementsByTagName("location").item(0);
//If the location is not null
if (location != null) {
String val = location.getTextContent();
val = val.replace("file://", "");
val = URLDecoder.decode(val, "UTF-8");
//Get the filename and if it is one of the requested files
if (files.containsKey(val)) {
File toAddPOPM = files.get(val);
NodeList temp = songElement.getElementsByTagName("rating");
if (temp != null) {
//Increment the number of total processed files
total ++;
//Get the rating of RhythmBox
int RBRating;
Node stars = temp.item(0);
if (stars == null) {
RBRating = 0;
} else {
RBRating = Integer.parseInt(temp.item(0).getTextContent());
}
int WMPRating = starsToNumber.get(RBRating);
executor.execute(() -> {
try {
addNewRating(toAddPOPM, RBRating, WMPRating);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
});
}
}
}
}
executor.shutdown();
}
//System.out.println("\n" + ANSI_GREEN + "Processed " + total + " songs successfully!" + ANSI_RESET + "");
}
public static void addNewRating(File toAddPOPM, int RBRating, int WMPRating) throws IOException, InterruptedException {
System.out.println(ANSI_YELLOW + "[Deleting obsolete POPM]\t" + ANSI_RESET + toAddPOPM.getAbsolutePath());
ProcessBuilder pb = new ProcessBuilder("eyeD3", "--remove-frame=POPM", toAddPOPM.getAbsolutePath());
Process p = pb.start();
p.waitFor();
//Add the RB rating
System.out.println(ANSI_GREEN + "[Adding POPM] [Rating=" + RBRating + "]\t" + ANSI_RESET + toAddPOPM.getAbsolutePath());
pb = new ProcessBuilder("eyeD3", "--add-popularity=RhythmBox:" + RBRating + ":0", toAddPOPM.getAbsolutePath());
Process addRhythmBox = pb.start();
addRhythmBox.waitFor();
pb = new ProcessBuilder("eyeD3", "--add-popularity=Windows Media Player 9 Series:" + WMPRating + ":0", toAddPOPM.getAbsolutePath());
Process addWMP = pb.start();
addWMP.waitFor();
}
}