Skip to content

Commit 2aed559

Browse files
committed
Accept Steam profile URLs as player IDs
1 parent 5b35397 commit 2aed559

File tree

4 files changed

+103
-38
lines changed

4 files changed

+103
-38
lines changed

src/com/joelchristophel/sourceradio/FileUtilities.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import java.io.File;
55
import java.io.FileReader;
66
import java.io.IOException;
7+
import java.io.InputStreamReader;
8+
import java.net.HttpURLConnection;
9+
import java.net.URL;
710
import java.nio.file.Files;
811
import java.nio.file.Paths;
912
import java.nio.file.StandardOpenOption;
@@ -171,10 +174,38 @@ static boolean removeLine(String path, String line, boolean commentAware) throws
171174
return hasLine;
172175
}
173176

177+
static void writeFile(String path, String text) throws IOException {
178+
File file = new File(path);
179+
file.delete();
180+
Files.write(Paths.get(path), text.getBytes(), StandardOpenOption.CREATE);
181+
FileUtilities.trimFile(path);
182+
}
183+
184+
static void writeFile(String path, String[] lines) throws IOException {
185+
String text = "";
186+
for (int i = 0; i < lines.length; i++) {
187+
text += lines[i] + (i == lines.length - 1 ? "" : System.lineSeparator());
188+
}
189+
writeFile(path, text);
190+
}
191+
174192
static String normalizeDirectoryPath(String path) {
175193
if (path != null && !path.endsWith(File.separator)) {
176194
path += File.separator;
177195
}
178196
return path;
179197
}
198+
199+
static String getHtml(String url) throws Exception {
200+
StringBuilder result = new StringBuilder();
201+
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
202+
conn.setRequestMethod("GET");
203+
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
204+
String line;
205+
while ((line = reader.readLine()) != null) {
206+
result.append(line);
207+
}
208+
reader.close();
209+
return result.toString();
210+
}
180211
}

src/com/joelchristophel/sourceradio/Player.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
57

68
class Player {
79

@@ -10,7 +12,7 @@ class Player {
1012
private static List<Player> players = new ArrayList<Player>();
1113

1214
private Player(String steamId, String username) {
13-
steamId3 = toSteamId3(steamId);
15+
steamId3 = asSteamId3(steamId);
1416
this.username = username == null ? null : username.trim();
1517
}
1618

@@ -36,7 +38,7 @@ static Player getPlayerFromUsername(String username, boolean caseSensitive) {
3638
}
3739

3840
static Player getPlayerFromSteamId(String steamId) {
39-
String steamId3 = toSteamId3(steamId);
41+
String steamId3 = asSteamId3(steamId);
4042
Player matchingPlayer = null;
4143
for (Player player : players) {
4244
if (player.getSteamId3() != null && player.getSteamId3().equals(steamId3)) {
@@ -52,7 +54,7 @@ static Player getPlayerFromSteamId(String steamId) {
5254
}
5355

5456
static boolean addPlayer(String steamId, String username) {
55-
String steamId3 = toSteamId3(steamId);
57+
String steamId3 = asSteamId3(steamId);
5658
username = username.trim();
5759
int playersBefore = players.size();
5860
if (steamId3 == null) {
@@ -112,7 +114,7 @@ static void changeUsername(String oldUsername, String newUsername) {
112114
}
113115
}
114116

115-
private static String toSteamId3(String steamId) {
117+
static String asSteamId3(String steamId) {
116118
String steamId3 = steamId;
117119
if (steamId != null) {
118120
String[] chunks = steamId.split(":");
@@ -131,6 +133,29 @@ private static String toSteamId3(String steamId) {
131133
return steamId3;
132134
}
133135

136+
static String getSteamId3FromProfile(String profileId) throws Exception {
137+
String steamId3 = null;
138+
if (profileId != null) {
139+
String html = FileUtilities.getHtml("https://steamrep.com/search?q=" + profileId);
140+
String needle = "steam3ID:";
141+
int needlePosition = html.indexOf(needle);
142+
if (needlePosition != -1) {
143+
steamId3 = html.substring(needlePosition + needle.length(), html.indexOf('|', needlePosition)).trim();
144+
}
145+
}
146+
return steamId3;
147+
}
148+
149+
static String getSteamProfileId(String profileUrl) {
150+
String profileId = null;
151+
Pattern pattern = Pattern.compile("^(?:http(?:s?):\\/\\/)?(?:www\\.)?steamcommunity\\.com\\/(?:profiles|id)\\/(.+)$");
152+
Matcher matcher = pattern.matcher(profileUrl);
153+
if (matcher.find()) {
154+
profileId = matcher.group(1);
155+
}
156+
return profileId;
157+
}
158+
134159
String getSteamId3() {
135160
return steamId3;
136161
}

src/com/joelchristophel/sourceradio/Playlist.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,18 +1044,9 @@ private static String normalizeQuery(String query) {
10441044
}
10451045

10461046
static String getLatestVersion(String githubUser, String repository) throws Exception {
1047-
StringBuilder result = new StringBuilder();
1048-
URL url = new URL("https://github.com/" + githubUser + "/" + repository + "/releases/latest");
1049-
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
1050-
conn.setRequestMethod("GET");
1051-
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
1052-
String line;
1053-
while ((line = reader.readLine()) != null) {
1054-
result.append(line);
1055-
}
1056-
reader.close();
1047+
String url = "https://github.com/" + githubUser + "/" + repository + "/releases/latest";
1048+
String html = FileUtilities.getHtml(url);
10571049
String needle = repository + "/tree/";
1058-
String html = result.toString();
10591050
int needlePosition = html.indexOf(needle);
10601051
String version = html.substring(needlePosition + needle.length(), html.indexOf('"', needlePosition));
10611052
return version.charAt(0) == 'v' ? version.substring(1) : version;

src/com/joelchristophel/sourceradio/Properties.java

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,10 @@ private File getUserDirectory(String steamPath) {
166166
Set<Player> getAdmins() {
167167
Set<Player> admins = new HashSet<Player>();
168168
try {
169-
for (String steamId : FileUtilities.getLines(ADMINS_PATH, false)) {
170-
if (steamId.matches("[0-9]+(\\s*//.*)?")) {
171-
admins.add(Player.getPlayerFromSteamId(steamId.split("\\s*//")[0]));
172-
}
173-
}
169+
admins = getPlayers(ADMINS_PATH);
174170
} catch (IOException e) {
175-
new IOException("Error: Failed to read the admins list at " + ADMINS_PATH + ".", e).printStackTrace();
171+
new IOException("Error: Failed to read the admins list from " + ADMINS_PATH + ".", e).printStackTrace();
176172
}
177-
admins.add(getOwner());
178173
return admins;
179174
}
180175

@@ -186,17 +181,46 @@ Set<Player> getAdmins() {
186181
Set<Player> getBannedPlayers() {
187182
Set<Player> bannedPlayers = new HashSet<Player>();
188183
try {
189-
for (String line : FileUtilities.getLines(BANNED_PLAYERS_PATH, false)) {
190-
if (line.matches("[0-9]+(\\s*//.*)?")) {
191-
bannedPlayers.add(Player.getPlayerFromSteamId(line.split("\\s*//")[0]));
192-
}
193-
}
184+
bannedPlayers = getPlayers(BANNED_PLAYERS_PATH);
194185
} catch (IOException e) {
195-
new IOException("Error: Failed to read the banned players list from " + BANNED_PLAYERS_PATH + ".", e);
186+
String message = "Error: Failed to read the banned players list from " + BANNED_PLAYERS_PATH + ".";
187+
new IOException(message, e).printStackTrace();
196188
}
197189
return bannedPlayers;
198190
}
199191

192+
private Set<Player> getPlayers(String path) throws IOException {
193+
Set<Player> players = new HashSet<Player>();
194+
boolean rewriteFile = false;
195+
String[] lines = FileUtilities.getLines(path, false);
196+
for (int i = 0; i < lines.length; i++) {
197+
lines[i] = lines[i].replaceAll("http(?:s?)://", "").trim();
198+
String steamId = lines[i].split("\\s*//")[0];
199+
String profileId = Player.getSteamProfileId(steamId); // if line is a profile URL
200+
try {
201+
if (profileId != null) {
202+
rewriteFile = true;
203+
int commentPosition = lines[i].indexOf("//");
204+
String comment = commentPosition == -1 ? steamId : lines[i].split("//")[1].trim();
205+
if (lines[i].contains("profiles")) {
206+
steamId = profileId;
207+
} else {
208+
steamId = Player.getSteamId3FromProfile(profileId);
209+
}
210+
lines[i] = steamId + " // " + comment;
211+
}
212+
players.add(Player.getPlayerFromSteamId(steamId));
213+
} catch (Exception e) {
214+
String message = "Error: Failed while converting Steam profile URL to SteamID.";
215+
new Exception(message, e).printStackTrace();
216+
}
217+
}
218+
if (rewriteFile) {
219+
FileUtilities.writeFile(path, lines);
220+
}
221+
return players;
222+
}
223+
200224
Set<String> getBlockedSongs() {
201225
Set<String> blockedSongs = new HashSet<String>();
202226
try {
@@ -256,10 +280,7 @@ void writeProperty(String property, String value) {
256280
if (!foundProperty) {
257281
fileText += property + DELIMITER + " " + value + System.lineSeparator();
258282
}
259-
File file = new File(PROPERTIES_PATH);
260-
file.delete();
261-
Files.write(Paths.get(PROPERTIES_PATH), fileText.getBytes(), StandardOpenOption.CREATE);
262-
FileUtilities.trimFile(PROPERTIES_PATH);
283+
FileUtilities.writeFile(PROPERTIES_PATH, fileText);
263284
} catch (IOException e) {
264285
String message = "Error: Failed to write \"" + property + "\" property to " + PROPERTIES_PATH + ".";
265286
new IOException(message, e).printStackTrace();
@@ -400,10 +421,7 @@ private void updateAndSyncProperties(Map<String, String> newProperties) {
400421
fileText += propertyNames[i] + DELIMITER + " " + properties.get(propertyNames[i]) + seperator;
401422
}
402423
try {
403-
File file = new File(PROPERTIES_PATH);
404-
file.delete();
405-
Files.write(Paths.get(PROPERTIES_PATH), fileText.getBytes(), StandardOpenOption.CREATE);
406-
FileUtilities.trimFile(PROPERTIES_PATH);
424+
FileUtilities.writeFile(PROPERTIES_PATH, fileText);
407425
} catch (IOException e) {
408426
e.printStackTrace();
409427
}
@@ -434,8 +452,8 @@ private Map<String, String> readProperties(String path, String[] propertiesToRea
434452
for (String property : properties.keySet()) {
435453
if (line.toLowerCase().startsWith(property + DELIMITER)) {
436454
if (propertiesRead.contains(property)) {
437-
throw new IOException("The property \"" + property
438-
+ "\" is listed more than once in \"" + filename + "\".");
455+
throw new IOException("The property \"" + property + "\" is listed more than once in \""
456+
+ filename + "\".");
439457
}
440458
if (line.matches(".*//.*")) {
441459
line = line.replaceAll("\\s*//.*", "");

0 commit comments

Comments
 (0)