April 9, 2021

Quick YouTube Channel Search (AutoHotkey Tutorial)

Today I wanted to quickly search for videos inside a specific YouTube Channel.
The prominent search bar would search in whole YouTube. The search for the channel only is a bit hidden.
I share here how I have implemented a Quick Search functionality in the NWS PowerTool based on AutoHotkey.
This is more a on the fly implementation recording post.

Problem Description

The search in a channel in YouTube is a bit hidden. See the search icon on the bottom right on the picture below: this is where you have to click to search in the channel.



Moreover it is not so convenient to refine a search.

Implementation Recording

Lessons Learned

RegExp handling: 
for OR group with (?: |) (not [])
escape . and ? with \

Final Implementation

This is implemented in the NWS.ahk file.-> QuickSearch function.

Else If RegExMatch(sUrl,"youtube\.com/(?:c|channel)/") { ; YouTube Channel Search
    ; https://www.youtube.com/c/KevinStratvert/search?query=remove%20background
    ;https://www.youtube.com/channel/UCfJT_eYDTmDE-ovKaxVE1ig/search?query=background
    sPat = youtube\.com/(?:c|channel)/([^/]*)/search\?query=(.*)
    sDefSearch =
    If RegExMatch(sUrl,sPat, sMatch) {
        sDefSearch := StrReplace(sMatch2,"%20"," ")
        sDefSearch := StrReplace(sDefSearch,"+"," ")
        sChannelName := sMatch1
    } Else {
        sPat = youtube\.com/(?:c|channel)/([^/]*)
        RegExMatch(sUrl,sPat, sMatch)
        sChannelName := sMatch1
    } 
    
    InputBox, sSearch , YouTube Channel Search, Enter search string:,,640,125,,,,, %sDefSearch%
    if ErrorLevel
        return
    sSearch := Trim(sSearch)
    
    sSearchUrl = https://www.youtube.com/channel/%sChannelName%/search?query=%sSearch%
    SendInput ^t^l ; close current search window
    Clip_Paste(sSearchUrl)
    SendInput {Enter}
    SendInput ^{Tab}
    Sleep 500
    SendInput ^w

No comments:

Post a Comment