Parsing youtube url

前端 未结 3 1799
野的像风
野的像风 2021-02-03 13:06

I\'ve written a ruby youtube url parser. It\'s designed to take an input of a youtube url of one of the following structures (these are currently the youtube url structures tha

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-03 13:32

    require 'uri'
    require 'cgi'
    
    urls = %w[http://youtu.be/sGE4HMvDe-Q 
              http://www.youtube.com/watch?v=Lp7E973zozc&feature=relmfu
              http://www.youtube.com/p/A0C3C1D163BE880A?hl=en_US&fs=1]
    
    def parse_youtube url
      u = URI.parse url
      if u.path =~ /watch/
        p CGI::parse(u.query)["v"].first
      else
        p u.path
      end
    end
    
    urls.each { |url| parse_youtube url }
    #=> "/sGE4HMvDe-Q"
    #=> "Lp7E973zozc"
    #=> "/p/A0C3C1D163BE880A"
    

提交回复
热议问题