-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Catch errors while processing timestamp-links #6851
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
Merged
litetex
merged 6 commits into
TeamNewPipe:dev
from
litetex:make-parsing-of-timestamp-links-more-robust
Aug 14, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
51c60e5
Catch errors while processing timestamp-links
litetex 1d61bb5
Set loglevel to error
litetex 9f8b226
Use better pattern for matching timestamp in text and some reworks
litetex f9ab23b
Removed useless fiedl
litetex 0e4c8ea
Added tests for the ``TimestampExtractor``
litetex 5f3b8be
Fixed format
litetex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
79 changes: 79 additions & 0 deletions
79
app/src/main/java/org/schabi/newpipe/util/external_communication/TimestampExtractor.java
This file contains hidden or 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,79 @@ | ||
package org.schabi.newpipe.util.external_communication; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Extracts timestamps. | ||
*/ | ||
public final class TimestampExtractor { | ||
public static final Pattern TIMESTAMPS_PATTERN = Pattern.compile( | ||
"(?:^|(?!:)\\W)(?:([0-5]?[0-9]):)?([0-5]?[0-9]):([0-5][0-9])(?=$|(?!:)\\W)"); | ||
|
||
private TimestampExtractor() { | ||
// No impl pls | ||
} | ||
|
||
/** | ||
* Get's a single timestamp from a matcher. | ||
* | ||
* @param timestampMatches The matcher which was created using {@link #TIMESTAMPS_PATTERN} | ||
* @param baseText The text where the pattern was applied to / | ||
* where the matcher is based upon | ||
* @return If a match occurred: a {@link TimestampMatchDTO} filled with information.<br/> | ||
* If not <code>null</code>. | ||
*/ | ||
public static TimestampMatchDTO getTimestampFromMatcher( | ||
final Matcher timestampMatches, | ||
final String baseText) { | ||
int timestampStart = timestampMatches.start(1); | ||
if (timestampStart == -1) { | ||
timestampStart = timestampMatches.start(2); | ||
} | ||
final int timestampEnd = timestampMatches.end(3); | ||
|
||
final String parsedTimestamp = baseText.substring(timestampStart, timestampEnd); | ||
final String[] timestampParts = parsedTimestamp.split(":"); | ||
|
||
final int seconds; | ||
if (timestampParts.length == 3) { // timestamp format: XX:XX:XX | ||
seconds = Integer.parseInt(timestampParts[0]) * 3600 // hours | ||
+ Integer.parseInt(timestampParts[1]) * 60 // minutes | ||
+ Integer.parseInt(timestampParts[2]); // seconds | ||
} else if (timestampParts.length == 2) { // timestamp format: XX:XX | ||
seconds = Integer.parseInt(timestampParts[0]) * 60 // minutes | ||
+ Integer.parseInt(timestampParts[1]); // seconds | ||
} else { | ||
return null; | ||
} | ||
|
||
return new TimestampMatchDTO(timestampStart, timestampEnd, seconds); | ||
} | ||
|
||
public static class TimestampMatchDTO { | ||
private final int timestampStart; | ||
private final int timestampEnd; | ||
private final int seconds; | ||
|
||
public TimestampMatchDTO( | ||
final int timestampStart, | ||
final int timestampEnd, | ||
final int seconds) { | ||
this.timestampStart = timestampStart; | ||
this.timestampEnd = timestampEnd; | ||
this.seconds = seconds; | ||
} | ||
|
||
public int timestampStart() { | ||
return timestampStart; | ||
} | ||
|
||
public int timestampEnd() { | ||
return timestampEnd; | ||
} | ||
|
||
public int seconds() { | ||
return seconds; | ||
} | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
app/src/test/java/org/schabi/newpipe/util/external_communication/TimestampExtractorTest.java
This file contains hidden or 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,101 @@ | ||
package org.schabi.newpipe.util.external_communication; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.time.Duration; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.fail; | ||
|
||
@RunWith(Parameterized.class) | ||
public class TimestampExtractorTest { | ||
|
||
@Parameterized.Parameter(0) | ||
public Duration expected; | ||
|
||
@Parameterized.Parameter(1) | ||
public String stringToProcess; | ||
|
||
@Parameterized.Parameters(name = "Expecting {0} for \"{1}\"") | ||
litetex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public static List<Object[]> dataForTests() { | ||
return Arrays.asList(new Object[][]{ | ||
// Simple valid values | ||
{Duration.ofSeconds(1), "0:01"}, | ||
{Duration.ofSeconds(1), "00:01"}, | ||
{Duration.ofSeconds(1), "0:00:01"}, | ||
{Duration.ofSeconds(1), "00:00:01"}, | ||
{Duration.ofMinutes(1).plusSeconds(23), "1:23"}, | ||
{Duration.ofMinutes(1).plusSeconds(23), "01:23"}, | ||
{Duration.ofMinutes(1).plusSeconds(23), "0:01:23"}, | ||
{Duration.ofMinutes(1).plusSeconds(23), "00:01:23"}, | ||
{Duration.ofHours(1).plusMinutes(23).plusSeconds(45), "1:23:45"}, | ||
{Duration.ofHours(1).plusMinutes(23).plusSeconds(45), "01:23:45"}, | ||
// Check with additional text | ||
{Duration.ofSeconds(1), "Wow 0:01 words"}, | ||
{Duration.ofMinutes(1).plusSeconds(23), "Wow 1:23 words"}, | ||
{Duration.ofSeconds(1), "Wow 0:01 words! 33:"}, | ||
{null, "Wow0:01 abc"}, | ||
{null, "Wow 0:01abc"}, | ||
{null, "Wow0:01abc"}, | ||
{null, "Wow0:01"}, | ||
{null, "0:01abc"}, | ||
// Boundary checks | ||
{Duration.ofSeconds(0), "0:00"}, | ||
{Duration.ofHours(59).plusMinutes(59).plusSeconds(59), "59:59:59"}, | ||
{null, "60:59:59"}, | ||
{null, "60:59"}, | ||
{null, "0:60"}, | ||
// Format checks | ||
{null, "000:0"}, | ||
{null, "123:01"}, | ||
{null, "123:123"}, | ||
{null, "2:123"}, | ||
{null, "2:3"}, | ||
{null, "1:2:3"}, | ||
{null, ":3"}, | ||
{null, "01:"}, | ||
{null, ":01"}, | ||
{null, "a:b:c"}, | ||
{null, "abc:def:ghj"}, | ||
{null, "::"}, | ||
{null, ":"}, | ||
{null, ""} | ||
}); | ||
} | ||
|
||
@Test | ||
public void testExtract() { | ||
final Matcher m = TimestampExtractor.TIMESTAMPS_PATTERN.matcher(this.stringToProcess); | ||
|
||
if (!m.find()) { | ||
if (expected == null) { | ||
return; | ||
} | ||
fail("No match found but expected one"); | ||
} | ||
|
||
final TimestampExtractor.TimestampMatchDTO timestampMatchDTO = | ||
TimestampExtractor | ||
.getTimestampFromMatcher(m, this.stringToProcess); | ||
|
||
if (timestampMatchDTO == null) { | ||
if (expected == null) { | ||
return; | ||
} | ||
fail("Result shouldn't be null"); | ||
} else if (expected == null) { | ||
assertNull("Expected that the dto is null, but it isn't", timestampMatchDTO); | ||
return; | ||
} | ||
|
||
final int actualSeconds = timestampMatchDTO.seconds(); | ||
|
||
assertEquals(expected.getSeconds(), actualSeconds); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.