2 # Copyright Michael Orlitzky
4 # http://michael.orlitzky.com/
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # http://www.fsf.org/licensing/licenses/gpl.html
21 class Bliptv
< Website
23 VALID_BLIPTV_URL_REGEX
= /^(http:\/\
/)?([[:alnum:]\-]+\.)?blip\.tv\/file\
/(\d+)(.*)?$/
24 VALID_BLIPTV_REDIR_URL_REGEX
= /^(http:\/\
/)?([[:alnum:]\-]+\.)?blip\.tv\/play\
/[[:alnum:]]+$/
26 def self.owns_url
?(url
)
27 return (url
=~ VALID_BLIPTV_URL_REGEX
||
28 url
=~ VALID_BLIPTV_REDIR_URL_REGEX
)
35 if (@url =~ VALID_BLIPTV_URL_REGEX
)
36 page_data
= self.get_page_data(@url)
38 # It's a redirect. Figure out the RSS page URL from the redirect.
39 redir_url
= get_redirect_url
40 rss_page_url
= parse_rss_url(redir_url
)
41 rss_data
= get_page_data(rss_page_url
)
43 # The "real" page URL is embedded in the RSS feed. Once we get the
44 # real URL, we can proceed as if we were given that URL in the first
46 real_page_url
= parse_page_url(rss_data
)
47 page_data
= self.get_page_data(real_page_url
)
50 filepath
= parse_video_url(page_data
)
58 def parse_page_url(data)
59 # A simplified VALID_BLIPTV_URL_REGEX.
60 page_url_regex
= /http:\/\
/blip\.tv\/file\
/\d+/
61 matches
= page_url_regex
.match(data)
64 raise StandardError
.new("Couldn't parse the real page URL from the RSS page.")
70 def parse_rss_url(url
)
71 rss_id_regex
= /\/flash\
/(\d+)/
72 matches
= rss_id_regex
.match(url
)
74 if matches
.nil? or (matches
.length
< 2)
75 raise StandardError
.new("Couldn't parse the video ID from the redirect URL: #{url}")
78 return "http://blip.tv/rss/flash/#{matches[1]}"
84 response
= Net
::HTTP.start(uri
.host
, uri
.port
) do |http
|
85 http
.get(uri
.request_uri
, {})
88 return response
['location']
92 def parse_video_url(page_data
)
93 # First, try to find the MOV video. The source videos are usually
95 video_url_regex
= /"Quicktime \(\.mov\)", "attribute" : "(.*?\.mov)/i
96 matches
= video_url_regex
.match(page_data
)
98 if not matches
.nil? and (matches
.length
> 1)
102 # I've seen some free software videos encoded as OGG/Vorbis, too.
103 video_url_regex
= /"Ogg Theora\/Vorbis \
(\
.og
[gv
]\
)", "attribute
" : "(.*?\
.og
[gv
])/i
104 matches
= video_url_regex
.match(page_data
)
106 if not matches
.nil? and (matches
.length
> 1)
110 # If that didn't work, try the WMV format, which is occasionally
111 # used for the source as well.
112 video_url_regex
= /"Windows Media \(\.wmv\)", "attribute" : "(.*?\.wmv)/i
113 matches
= video_url_regex
.match(page_data
)
115 if not matches
.nil? and (matches
.length
> 1)
120 # If neither of the source formats are present, just grab the
121 # video URL from the Flash variable and be done with it.
122 video_url_regex
= /setPrimaryMediaUrl\("(.*?\.(flv|mov|wmv|mp4|og[gv]))/i
123 matches
= video_url_regex
.match(page_data
)
125 if matches
.nil? or (matches
.length
< 2)
126 raise StandardError
.new("Couldn't parse any of the video format URLs.")