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

let QMPlay2 handle youtube:XXYYZZ URIs #619

Closed
wants to merge 2 commits into from

Conversation

RJVB
Copy link
Contributor

@RJVB RJVB commented Jun 19, 2023

There are times where I want to refer to a YouTube video in shorthand notation that isn't usually recognised as "direct link" (e.g. on discussion forums where direct linking to videos with specific content isn't allowed). The obvious choice is youtube:XXYYZZ where XXYYZZ is the video identifier.

This patch enables QMPlay2 to process such URIs.

(This PR is a bit for fun and/or just-in-case)

Committed from host : Bola
@RJVB
Copy link
Contributor Author

RJVB commented Jun 19, 2023

Figured out how to register the scheme on *nix .

@zaps166
Copy link
Owner

zaps166 commented Jun 26, 2023

The (stupid) idea is that QMPlay2 doesn't know about YT, because it's an extension. But I'll check it soon!

@RJVB
Copy link
Contributor Author

RJVB commented Jun 26, 2023 via email

// the leading '/' from the path to recover the intact vID. This implementation
// also supports youtube:/vID as a side effect ... why not!
auto vid = url.mid(8);
while (vid.at(0) == '/')
Copy link
Owner

Choose a reason for hiding this comment

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

Crash if vid is empty string.

Btw. do we really need the youtube:/ (single slash) ? 😄

}
return maybeExtensionAddress("http://www.youtube.com/watch?v=" + vid);
}
else
Copy link
Owner

Choose a reason for hiding this comment

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

else not needed - previous block of code returns - no need to add indentation and else.

@@ -2035,7 +2035,9 @@ bool MainWidget::eventFilter(QObject *obj, QEvent *event)
#ifdef Q_OS_MACOS
else if (event->type() == QEvent::FileOpen)
{
filesToAdd.append(((QFileOpenEvent *)event)->file());
auto url = ((QFileOpenEvent *)event)->url();
Copy link
Owner

Choose a reason for hiding this comment

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

Could you add auto fileOpenEvent = (QFileOpenEvent *)event; and use it?

@RJVB
Copy link
Contributor Author

RJVB commented Jul 31, 2023 via email

@zaps166
Copy link
Owner

zaps166 commented Jul 31, 2023

Indeed. Old habit ("compiler might be able to generate more efficient code"), plus I find it makes it easier to get reacquainted with the code flow if you haven't been in that area for a while.

My compiler (clang) generates the same code w/o else (same checksum of the .so file).

I'd suggest (this should not trigger the undefined behavior and looks simpler + uses https):

QString Functions::maybeExtensionAddress(const QString &url)
{
    if (url.startsWith("youtube:"))
    {
        // we want to support both youtube:vID and youtube://vID but the latter form
        // is often passed to us as "youtube://vid" (vID interpreted as the remote host).
        // So we also support the full URI form with an empty host string and strip
        // the leading '/' from the path to recover the intact vID. This implementation
        // also supports youtube:/vID as a side effect ... why not!
        const auto vid = url.mid(8).remove('/');
        while (!vid.isEmpty())
            return maybeExtensionAddress("https://www.youtube.com/watch?v=" + vid);
    }

    for (const QMPlay2Extensions *QMPlay2Ext : QMPlay2Extensions::QMPlay2ExtensionsList())
    {
        const QString prefix = QMPlay2Ext->matchAddress(url);
        if (!prefix.isEmpty())
            return prefix + "://{" + url + "}";
    }
    return url;
}

@zaps166
Copy link
Owner

zaps166 commented Jul 31, 2023

Or this:

diff --git a/src/modules/Extensions/YouTube.cpp b/src/modules/Extensions/YouTube.cpp
index d838ccb4..19e8dab4 100644
--- a/src/modules/Extensions/YouTube.cpp
+++ b/src/modules/Extensions/YouTube.cpp
@@ -436,7 +436,7 @@ bool YouTube::canConvertAddress() const
 QString YouTube::matchAddress(const QString &url) const
 {
     const QUrl qurl(url);
-    if (qurl.scheme().startsWith("http") && (qurl.host().contains("youtube.") || qurl.host().contains("youtu.be")))
+    if (qurl.scheme() == "youtube" || (qurl.scheme().startsWith("http") && (qurl.host().contains("youtube.") || qurl.host().contains("youtu.be"))))
         return "YouTube";
     if (qurl.scheme().startsWith("http") && qurl.host().contains("twitch.tv"))
         return "youtube-dl";
@@ -460,7 +460,7 @@ void YouTube::convertAddress(const QString &prefix, const QString &url, const QS
         if (ioCtrl && (stream_url || name))
         {
             auto &youTubeDl = ioCtrl->toRef<YouTubeDL>();
-            const QStringList youTubeVideo = getYouTubeVideo(param, url, youTubeDl);
+            const QStringList youTubeVideo = getYouTubeVideo(param, url.startsWith("youtube:") ? url.mid(8).remove('/') : url, youTubeDl);
             if (youTubeVideo.count() == 4)
             {
                 if (stream_url)

No need to modify QMPlay2, just the YT extension 😄

@zaps166
Copy link
Owner

zaps166 commented Jul 31, 2023

Btw. where youtube: (without //) is needed? Without this static QString fileArg(const QString &arg) doesn't need to be modified, too 🙂

@RJVB
Copy link
Contributor Author

RJVB commented Jul 31, 2023 via email

@zaps166
Copy link
Owner

zaps166 commented Jul 31, 2023

Btw. where youtube: (without //) is needed?
Well, yes, that was in fact my original intention (think of the mailto: protocol). IIRC I've had to support the slashes because they could get added by the OS (probably the Mac's) but I cannot really remember.

On Linux (xdg-open) works correctly with slashes, so if macOS needs slashes, let's drop the : behavior and no need to additional conditions in Main.cpp 🙂

@RJVB
Copy link
Contributor Author

RJVB commented Jul 31, 2023 via email

@zaps166
Copy link
Owner

zaps166 commented Aug 3, 2023

Sorry, I wasn't clear: as per the issue title it was my intention to support the syntax without slashes. You can argue that this is more appropriate too: "youtube" is not a service protocoI (think of it more as a hostnm as used with rsync) but there's also the concern to keep it simple for the user (me, for now ;)). I'll have to check if I was obliged to add support for the syntax with the slashes or if that was a conscious choice.

Just keep the condition in Main.cpp and add #619 (comment) instead of modifying Functions::maybeExtensionAddress 🙂

@zaps166 zaps166 marked this pull request as draft April 6, 2024 09:22
@zaps166
Copy link
Owner

zaps166 commented Dec 11, 2024

Not changed according to my comments for over a year, closing.

@zaps166 zaps166 closed this Dec 11, 2024
@RJVB
Copy link
Contributor Author

RJVB commented Dec 11, 2024 via email

@zaps166
Copy link
Owner

zaps166 commented Dec 11, 2024

I don't remember, it's been 1.5 year 🙂

I also suggested more simple solution and got no feedback: #619 (comment)

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.

2 participants