-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.4.1 <=> Edge cases related to IMDB badges fixed
- Loading branch information
Showing
10 changed files
with
12,709 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.4.0 | ||
1.4.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package updatetool.common; | ||
|
||
import java.net.URLDecoder; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import javax.annotation.concurrent.NotThreadSafe; | ||
import org.tinylog.Logger; | ||
|
||
@NotThreadSafe | ||
public final class ExtraData { | ||
private Map<String, String> mapping = new LinkedHashMap<>(); | ||
|
||
private ExtraData() {} | ||
|
||
public boolean contains(Pair<String, String> mapping) { | ||
String v = this.mapping.get(mapping.getKey()); | ||
if(Objects.equals(v, mapping.getValue())) | ||
return true; | ||
return false; | ||
} | ||
|
||
public boolean containsAny(List<Pair<String, String>> mapping) { | ||
for(var kv : mapping) { | ||
if(contains(kv)) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public boolean containsKey(String key) { | ||
return mapping.containsKey(key); | ||
} | ||
|
||
public void deleteKey(String key) { | ||
mapping.remove(key); | ||
} | ||
|
||
public void prepend(String key, String value) { | ||
var map = new LinkedHashMap<String, String>(); | ||
map.put(key, value); | ||
mapping.remove(key); | ||
map.putAll(mapping); | ||
this.mapping = map; | ||
} | ||
|
||
public String toURI() { | ||
if(mapping.isEmpty()) | ||
return ""; | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
for(var entry : mapping.entrySet()) { | ||
sb.append(handlePlexEncoding(entry.getKey())); | ||
sb.append("="); | ||
sb.append(handlePlexEncoding(entry.getValue())); | ||
sb.append("&"); | ||
} | ||
sb.deleteCharAt(sb.length()-1); | ||
|
||
return sb.toString(); | ||
} | ||
|
||
private String handlePlexEncoding(String s) { | ||
// Plex has some interesting encoding behavior that does not match with the java default implementation | ||
return URLEncoder.encode(s, StandardCharsets.UTF_8) | ||
.replaceAll("\\.", "%2E") | ||
.replaceAll("\\%2D", "-") | ||
.replaceAll("\\_", "%5F") | ||
.replaceAll("\\*", "%2A") | ||
.replaceAll("\\%7E", "~"); | ||
} | ||
|
||
public static ExtraData of(String uri) { | ||
ExtraData u = new ExtraData(); | ||
|
||
if(uri != null) { | ||
String[] components = uri.split("\\&"); | ||
for(String pair : components) { | ||
if(pair.isBlank()) | ||
continue; | ||
String[] kv = pair.split("\\="); | ||
|
||
String key, value; | ||
|
||
if(kv.length > 2) { | ||
Logger.warn("ParsedURI: Encountered more than 2 K/V entries. Garbage date or bug? ({})", uri); | ||
continue; | ||
} else if(kv.length == 1) { | ||
key = kv[0].trim(); | ||
value = ""; | ||
} else { | ||
key = kv[0].trim(); | ||
value = kv[1].trim(); | ||
} | ||
|
||
u.mapping.put(URLDecoder.decode(key, StandardCharsets.UTF_8), URLDecoder.decode(value, StandardCharsets.UTF_8)); | ||
} | ||
} | ||
|
||
return u; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ExtraData [mapping=" + mapping + "]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package updatetool.common; | ||
|
||
import java.util.Objects; | ||
|
||
public final class Pair<K, V> { | ||
private final K key; | ||
private final V value; | ||
|
||
private Pair(K key, V value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
|
||
public K getKey() { | ||
return key; | ||
} | ||
|
||
public V getValue() { | ||
return value; | ||
} | ||
|
||
public static <K, V> Pair<K, V> of(K key, V value) { | ||
Objects.requireNonNull(key, "key must not be null"); | ||
return new Pair<>(key, value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(key, value); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
@SuppressWarnings("rawtypes") | ||
Pair other = (Pair) obj; | ||
return Objects.equals(key, other.key) && Objects.equals(value, other.value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.4.0 | ||
1.4.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import org.junit.jupiter.api.Test; | ||
import updatetool.common.ExtraData; | ||
|
||
public class URITest { | ||
|
||
@Test | ||
public void testUriEncoding() throws IOException, URISyntaxException { | ||
Files.lines(Paths.get(getClass().getResource("/uritestdata.txt").toURI())).forEach(l -> { | ||
ExtraData e = ExtraData.of(l); | ||
assertEquals(l, e.toURI()); | ||
}); | ||
} | ||
} |
Oops, something went wrong.