1

I want to check if the contents of $url contain v= anywhere after https://youtube.com/watch?.

Example output (take note of the present and missing instances of v=):

> https://youtube.com/watch?list=PL3O9DAQHAxSTXqOH5VV6DO0BBupZ4feqz&v=apdJBcDF6vA&index=2
URL allowed.

> https://youtube.com/watch?list=PL3O9DAQHAxSTXqOH5VV6DO0BBupZ4feqz&index=2
URL NOT allowed.

> https://youtube.com/watch?v=apdJBcDF6vA
URL allowed.

> https://youtube.com/v=apdJBcDF6vA
URL NOT allowed.

> https://youtube.com/watch?v=apdJBcDF6vA&list=PL3O9DAQHAxSTXqOH5VV6DO0BBupZ4feqz&index=2
URL allowed.

> https://youtube.com/watch?v=apdJBcDF6vA&list=PL3O9DAQHAxSTXqOH5VV6DO0BBupZ4feqz
URL allowed.

> https://youv=tube.com/watch?v=apdJBcDF6vA
URL NOT allowed.

> v=https://youtube.com/watch?v=apdJBcDF6vA
URL NOT allowed.

I would try some code to test, but I honestly am not sure what to use.

OS is macOS High Sierra 10.13.6 and bash version is 3.2.57(1)-release

leetbacoon
  • 1,111
  • 2
  • 9
  • 32
  • 2
    Might I mention that searching for `v=` in the url to decide whether or not someone's allowed to visit it is a very weak form of doing it. If you know a little about how URLs are built it can be tricked by adding any parameter to the query string that ends with a `v`, for example `https://www.youtube.com/channel/UC-9-kyTW8ZkZNDHQJ6FgpwQ?env=2` will pass and youtube will just ignore the parameteter `env`. – brunorey Feb 11 '19 at 17:55
  • @brunorey my main goal was to get the eleven characters after `v=` (aka the video ID) and ignore all the rest. is there a better way of going about this? i can't think of any other way – leetbacoon Feb 11 '19 at 18:29
  • @brunorey wouldn't it be a safer idea to look for `watch?v=` and `&v=` instead? i.e. if `watch?v=` isn't found, try looking for `&v=`. – leetbacoon Feb 13 '19 at 10:17

2 Answers2

2

Inside double square brackets, the == operator supports globs:

if [[ $url == 'https://youtube.com/watch?'*v=* ]]
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
1

If what you want is to get the video ID, it's quite a simple thing to achieve with a regular expression. See How do I get the YouTube video ID from a URL? or try googling for 'regex to get youtube video id'

If you don't know it, consider using parts of youtube-dl, they can parse every possible url and have more tools to do many things.

brunorey
  • 2,135
  • 1
  • 18
  • 26
  • This definitely looks useful, however I prefer to not use JS. I already have `youtube-dl` and will have to look into using it for the video ID. By the way my goal (for now) is to only grab the ID of one video, not all the IDs from a channel or playlist. – leetbacoon Feb 11 '19 at 18:55