/java
package tw.oktw;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.lang.System.out;
public class Main {
public static void main(String[] args) {
Path oszDir = Paths.get(args[0]);
try {
Files.newDirectoryStream(oszDir).forEach(path -> {
if (path.getFileName().toString().matches("\\d+\\.osz")) {
String oszFileName = path.getFileName().toString();
int beatmapId = Integer.parseInt(oszFileName.replaceAll("\\D.*", ""));
String url = "
https://osu.ppy.sh/api/get_beatmaps?k=f4479181523b2c437d09775b546fdab8cae58c4e&s=" + beatmapId;
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new URL(url).openConnection().getInputStream()))) {
JSONObject json = new JSONArray(bufferedReader.readLine()).getJSONObject(0);
String newFileName = String.format("%d %s - %s.osz", beatmapId, json.getString("artist"), json.getString("title")).replaceAll("[\\*\\\\/:\"|?]", "").replace("<", "[").replace(">", "]");
if (path.toFile().renameTo(Paths.get(oszDir.toString(), newFileName).toFile())) {
out.printf("rename %s to %s%n", oszFileName, newFileName);
} else {
out.printf("rename %s fail!%n", oszFileName);
}
} catch (JSONException | IOException e) {
e.printStackTrace();
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}