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

Catch errors while processing timestamp-links #6851

Merged

Conversation

litetex
Copy link
Member

@litetex litetex commented Aug 4, 2021

What is it?

  • Bugfix (user facing)
  • Feature (user facing)
  • Codebase improvement (dev facing)
  • Meta improvement to the project (dev facing)

Description of the changes in your PR

Catches errors while processing timestamp-links.

Fixes the following issue(s)

APK testing

The APK can be found by going to the "Checks" tab below the title. On the left pane, click on "CI", scroll down to "artifacts" and click "app" to download the zip file which contains the debug APK of this PR.

Due diligence

Otherwise the complete app crashes, which is bad
@TobiGr
Copy link
Member

TobiGr commented Aug 4, 2021

Why is that method even called? This fixes the symtoms of parsing an unexpactedly structured URL. Shouldn't we take a closer look at the PATTERN?

(\\d+:)?(\\d+)?:(\\d+)

This regex looks like it is accepting more than it should.

@Stypox
Copy link
Member

Stypox commented Aug 5, 2021

Maybe we should use:

(\\d{1,2}:)?(\\d{1,2})?:(\\d{1,2})

Co-authored-by: Stypox <stypox@pm.me>
@litetex
Copy link
Member Author

litetex commented Aug 5, 2021

I think a problem with using

(\\d{1,2}:)?(\\d{1,2})?:(\\d{1,2})

Is that you get cut-off results like:
grafik

Should we stick with the old implementation?

Btw: Is it correct that even results like :12 are consider valid?

@Stypox
Copy link
Member

Stypox commented Aug 6, 2021

Is that you get cut-off results like:

Oh ok, then nevermind. Just keep the current one, but check that minutes and seconds are <= 60

Btw: Is it correct that even results like :12 are consider valid?

I think so, but ask @ TiA4f8R Edit: according to the regex in TextLinkifier it's wrong.

@Stypox
Copy link
Member

Stypox commented Aug 6, 2021

Wait, but why isn't TextLinkifier used? There the correct timestamps regex is used (we worked on it together a couple hours on Element):

(?:^|(?!:)\W)(?:([0-5]?[0-9]):)?([0-5]?[0-9]):([0-5][0-9])(?=$|(?!:)\W)

@Stypox
Copy link
Member

Stypox commented Aug 6, 2021

Using TextLinkifier.TIMESTAMPS_PATTERN and this code for timestampLink seems to work for me. Using TextLinkifier wouldn't work since it would disable clicks on the text view.

    private final Linkify.TransformFilter timestampLink = new Linkify.TransformFilter() {
        @Override
        public String transformUrl(final Matcher match, final String url) {
            final int timestampStart = match.start(2);
            final int timestampEnd = match.end(3);
            final String parsedTimestamp = commentText.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 url;
            }
            return streamUrl + url.replace(match.group(0), "#timestamp=" + seconds);
        }
    };

@litetex
Copy link
Member Author

litetex commented Aug 6, 2021

I'm also currently writing some unit-tests and fun fact:
"Timestamp format: XX:XX:XX" never works because the wrong group (it should be group 1 but it's group 2) is matched 😆

So e.g. 1:23:45 becomes 1425s (23:45) when it should be 5025s

Fixing it on the fly

@litetex litetex marked this pull request as ready for review August 6, 2021 20:20
Copy link
Member

@Stypox Stypox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I like this implementation :-)

@litetex litetex merged commit 0683daf into TeamNewPipe:dev Aug 14, 2021
@litetex litetex deleted the make-parsing-of-timestamp-links-more-robust branch August 14, 2021 19:10
@opusforlife2
Copy link
Collaborator

@litetex Remember to always add a PR to the draft release whenever you merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clicking the 1st comment of this youtube video to expand exists & crashes newpipe to homescreen
4 participants