Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What do you think of this proc

46 views
Skip to first unread message

Cecil Westerhof

unread,
Mar 30, 2018, 2:14:06 PM3/30/18
to
At the moment I have two scripts where I do something with a YouTube
URL: in one I play the video with mpv in the other I download it with
youtube-dl.

For this I wrote the following proc:
proc ::dcblUtilities::getYouTubeURL {URL} {
# for playing/downloading use:
# set startURL https://www.youtube.com/watch?v=
# "{startURL}${id}[dict get ${YouTubeURL} identifier]"
dict set YouTubeURL original ${URL}
dict set YouTubeURL valid False
set expression {[\?&]v=([-a-zA-Z0-9_]{11})(&|$)}
set startURL https://www.youtube.com/watch?
set length [string length ${startURL}]

if {${startURL} == [string range ${URL} 0 [incr length -1]]} {
if {[regexp ${expression} ${URL} _ identifier]} {
dict set YouTubeURL identifier ${identifier}
dict set YouTubeURL valid True
}
}
return ${YouTubeURL}
}

Can I improve on this?

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Rich

unread,
Mar 30, 2018, 3:30:14 PM3/30/18
to
Cecil Westerhof <Ce...@decebal.nl> wrote:
> At the moment I have two scripts where I do something with a YouTube
> URL: in one I play the video with mpv in the other I download it with
> youtube-dl.
>
> For this I wrote the following proc:
> proc ::dcblUtilities::getYouTubeURL {URL} {
> # for playing/downloading use:
> # set startURL https://www.youtube.com/watch?v=
> # "{startURL}${id}[dict get ${YouTubeURL} identifier]"
> dict set YouTubeURL original ${URL}
> dict set YouTubeURL valid False
> set expression {[\?&]v=([-a-zA-Z0-9_]{11})(&|$)}
> set startURL https://www.youtube.com/watch?
> set length [string length ${startURL}]
>
> if {${startURL} == [string range ${URL} 0 [incr length -1]]} {
> if {[regexp ${expression} ${URL} _ identifier]} {
> dict set YouTubeURL identifier ${identifier}
> dict set YouTubeURL valid True
> }
> }
> return ${YouTubeURL}
> }
>
> Can I improve on this?
>

You might be able to do the checking all in one regexp call:

dict set YouTubeURL original $URL
dict set YouTubeURL valid [regexp {^https://www.youtube.com/watch?v=(([-a-zA-Z0-9_]{11})$} -> identifier]
dict set YouTubeURL identifier $identifier

But you realize this only matches "www.youtube.com" urls, not the
yout.ube and other weird urls that google creates, right?
0 new messages