Programmatically determine whether a Youtube video has been taken down

后端 未结 4 1852
耶瑟儿~
耶瑟儿~ 2020-12-09 22:12

I\'m getting most of the music on Rap Exegesis from YouTube (in the form of embedded players). Unfortunately, there\'s always the risk that one of the videos I\'m using will

4条回答
  •  借酒劲吻你
    2020-12-09 22:55

    As @seengee says, the "right" way to do this is to look for the yt:state tag in the XML representation of a YouTube video via the YouTube API

    To get this XML representation, you GET http://gdata.youtube.com/feeds/api/videos/VIDEO_ID (more details here). So implementing this check should be as easy as:

    def valid_embed_link?
      doc = Hpricot(open("http://gdata.youtube.com/feeds/api/videos/#{youtube_video_id}"))
      doc.at('yt:state').blank?
    end
    

    Unfortunately this yields false positives. For example, http://www.youtube.com/watch?v=MX6rC1krGp0 plays fine, but http://gdata.youtube.com/feeds/api/videos/MX6rC1krGp0 contains a yt:state tag. Therefore, I've gone with this hackier method:

    def valid_embed_link?
      doc = Hpricot(open("http://www.youtube.com/watch?v=#{youtube_video_id}"))
      return doc.at('.yt-alert-content').blank?
    end
    

提交回复
热议问题