nishanthg92

nishanthg92

Validating youtube link

How to validate different kind of a youtube link?
for example:

http://youtu.be/dQw4w9WgXcQ
http://www.youtube.com/watch?v=dQw4w9WgXcQ
http://www.youtube.com/?v=dQw4w9WgXckQ

and convert this link to embed link like http://www.youtube.com/embed/dQw4w9WgXcQ to embed video in our application?

First Post!

wmnnd

wmnnd

YouTube links can easily be matched with regular expressions.

Check out this introduction to regular expressions if you’re not familiar with them yet:

If you want to learn how to use regular expressions in Elixir, take a look at the documentation of the Regex module which has a nice introduction to using them and detailed information about the individual methods:

Most Liked

NobbZ

NobbZ

If the video IDs are guaranteed to be at the end, you can do a pattern match, which is probably a lot faster:

def get_video_id("http://youtu.be/" <> id), do: id
def get_video_id("http://www.youtube.com/watch?v=" <> id), do: id
def get_video_id("http://www.youtube.com/?v=" <> id), do: id

How you can append that video ID to the string "http://www.youtube.com/embed/" is left as an exercise.

I’d prefer to make it HTTPS ready and solve it via meta programming, roughly like this:

Enum.each(["http", "https"], fn protocol ->
  Enum.each(["youtu.be/", "www.youtube.com/watch?v=", "www.youtube.com/?v="], fn prefix ->
    def get_video_id(unquote(protocol <> "://" <> prefix) <> id), do: id
  end)
end)

But probably the best version is to properly parse the URL and extract the video ID from the parsed URL. Pattern matching as shown by me will not work correctly when there are other parameters in the query string, and regular expressions that cover those possibilities tend to grow into an unreadable and slow monstrosity.

Last Post!

nishanthg92

nishanthg92

Thank you

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement