]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - src/websites/bliptv.rb
Add blip.tv support for URLs of the form http://blip.tv/play/<foo>.
[dead/whatever-dl.git] / src / websites / bliptv.rb
1 #
2 # Copyright Michael Orlitzky
3 #
4 # http://michael.orlitzky.com/
5 #
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.
10 #
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.
15 #
16 # http://www.fsf.org/licensing/licenses/gpl.html
17 #
18
19 require 'src/website'
20
21 class Bliptv < Website
22
23 VALID_BLIPTV_URL_REGEX = /^(http:\/\/)?([[:alnum:]\-]+\.)?blip\.tv\/file\/(\d+)(.*)?$/
24 VALID_BLIPTV_REDIR_URL_REGEX = /^(http:\/\/)?([[:alnum:]\-]+\.)?blip\.tv\/play\/[[:alnum:]]+$/
25
26 def self.owns_url?(url)
27 return (url =~ VALID_BLIPTV_URL_REGEX ||
28 url =~ VALID_BLIPTV_REDIR_URL_REGEX)
29 end
30
31
32 def get_video_url()
33 page_data = ''
34
35 if (@url =~ VALID_BLIPTV_URL_REGEX)
36 page_data = self.get_page_data(@url)
37 else
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)
42
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
45 # place.
46 real_page_url = parse_page_url(rss_data)
47 page_data = self.get_page_data(real_page_url)
48 end
49
50 filepath = parse_video_url(page_data)
51
52 return filepath
53 end
54
55
56 protected;
57
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)
62
63 if matches.nil?
64 raise StandardError.new("Couldn't parse the real page URL from the RSS page.")
65 end
66
67 return matches[0]
68 end
69
70 def parse_rss_url(url)
71 rss_id_regex = /\/flash\/(\d+)/
72 matches = rss_id_regex.match(url)
73
74 if matches.nil? or (matches.length < 2)
75 raise StandardError.new("Couldn't parse the video ID from the redirect URL: #{url}")
76 end
77
78 return "http://blip.tv/rss/flash/#{matches[1]}"
79 end
80
81 def get_redirect_url
82 uri = URI.parse(@url)
83
84 response = Net::HTTP.start(uri.host, uri.port) do |http|
85 http.get(uri.request_uri, {})
86 end
87
88 return response['location']
89 end
90
91
92 def parse_video_url(page_data)
93 # First, try to find the MOV video. The source videos are usually
94 # encoded with MOV.
95 video_url_regex = /"Quicktime \(\.mov\)", "attribute" : "(.*?\.mov)/i
96 matches = video_url_regex.match(page_data)
97
98 if not matches.nil? and (matches.length > 1)
99 return matches[1]
100 end
101
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)
105
106 if not matches.nil? and (matches.length > 1)
107 return matches[1]
108 end
109
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)
114
115 if not matches.nil? and (matches.length > 1)
116 return matches[1]
117 end
118
119
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)
124
125 if matches.nil? or (matches.length < 2)
126 raise StandardError.new("Couldn't parse any of the video format URLs.")
127 end
128
129 return matches[1]
130 end
131
132 end