Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lots of tuning #3

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
root = true

[*.java]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = tab
indent_brace_style = K&R
tab_width = 4
charset = utf-8
trim_trailing_whitespace = false
curly_bracket_next_line = false
spaces_around_operators = true
spaces_around_brackets = outside
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.class
*.geany
*.jar

58 changes: 42 additions & 16 deletions ID3Reader.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@


import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by Bozhidar Ganev on 13.12.17.
Expand All @@ -29,8 +30,8 @@ public class ID3Reader {
* @throws InterruptedException if the exiftool process is interrupted
*/
static String[][] getFrames(String[] files, String... frames) throws IOException, InterruptedException {
//Create the command
ArrayList<String> options = new ArrayList<>();
//create the command
options.add("exiftool");
//Make it force print if a frame does not exist
options.add("-f");
Expand All @@ -44,31 +45,56 @@ static String[][] getFrames(String[] files, String... frames) throws IOException
Process p = pc.start();
p.waitFor();


//Read the output
BufferedReader processOutput = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder result = new StringBuilder("");
String line = null;

//Read the output
int l = 0;
while ((line = processOutput.readLine()) != null) {
result.append(line).append(System.lineSeparator());
}

//Create a way to fast lookup filenames back to our file indices
HashMap<String, Integer> filesLookup = new HashMap<>();
for (var i = 0; i < files.length; i++) {
filesLookup.put(files[i], i);
}

//Parse the information in the frames
String[][] file_frames = new String[files.length][frames.length];
String[][] fileFrames = new String[files.length][frames.length];
Scanner scanner = new Scanner(result.toString());
for (int i = 0; i < files.length; i++) {
if (files.length > 1) scanner.nextLine();
//System.out.println(i + " out of " + files.length);
for (int j = 0; j < frames.length; j++) {
String frame = scanner.nextLine();
//If the frame has ":" (typically when the frame exists), get only the meaningful part
if(frame.contains(":")) frame = frame.substring(frame.indexOf(':') + 1).trim();
file_frames[i][j] = frame;
Pattern patternFilename = Pattern.compile("^=+ (.*)$");
Pattern patternColonedResult = Pattern.compile("^.*\\s+: (.*)$");
String currentFilename = null;
int currentFrameIndex = 0;
Matcher patternFilenameMatcher = null;
Matcher patternColonedResultMatcher = null;
String nextLine;
int i;
while (scanner.hasNext()) {
nextLine = scanner.nextLine();

patternFilenameMatcher = patternFilename.matcher(nextLine);
patternColonedResultMatcher = patternColonedResult.matcher(nextLine);

if (patternFilenameMatcher.find()) {
currentFilename = patternFilenameMatcher.group(1);
currentFrameIndex = 0;
} else if ((currentFilename != null) && (currentFrameIndex < frames.length)) {
i = filesLookup.get(currentFilename);

if (patternColonedResultMatcher.find()) {
//If the frame has ":" (typically when the frame exists), get only the meaningful part
fileFrames[i][currentFrameIndex] = patternColonedResultMatcher.group(1);
currentFrameIndex++;
} else {
fileFrames[i][currentFrameIndex] = nextLine;
currentFrameIndex++;
}
}
}
return file_frames;

return fileFrames;
}

/**
Expand Down
66 changes: 66 additions & 0 deletions POPMDecoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by Bozhidar Ganev on 12.12.17.
*/
public class POPMDecoder {
/**
* Extracts the rating from the popularimeter frame.
* The format of the exiftool output is:
* Popularimeter : <Email> Rating=0 Count=0
*
* @param popm is the exiftool output for the POPM frame.
* @return the extracted rating.
*/
public static int extractRating(String popm) {
if (popm == null) return 0;

String pattern = "Rating=(\\d+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(popm);
if (!m.find()) return 0;
int rating = Integer.parseInt(m.group(1));

/*
If mapping from a 5 star rating system, we would have (rounded down)...

1 => 1/5*255 = 51
2 => 2/5*255 = 102
3 => 3/5*255 = 153
4 => 4/5*255 = 204
5 => 5/5*255 = 255

Gap is about 50
Half gap is 25, and should be our tolerance
*/

if (rating == 0) {
return 0;
}
if (rating <= 51 + 25) {
return 1;
}
if (rating <= 102 + 25) {
return 2;
}
if (rating <= 153 + 25) {
return 3;
}
if (rating <= 204 + 25) {
return 4;
}
return 5;
}

public static int extractPlayCount(String popm) {
if (popm == null) return 0;

String pattern = "Count=(\\d+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(popm);
if (!m.find()) return 0;
return Integer.parseInt(m.group(1));
}
}
Loading