]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - lib/whatever-dl/websites/bliptv.rb
Move all of the 'src' code under the more-standard 'lib'.
[dead/whatever-dl.git] / lib / whatever-dl / 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 require 'cgi'
21
22 class BliptvMediaFormat
23 # This is just a convenience class for parsing two parameters out of
24 # an RSS feed: 'url' and 'blip:role'.
25 def initialize(line)
26 @url = nil
27 @role = nil
28
29 url_regex = /url=\"([^\"]+)\"/
30 role_regex = /blip:role=\"([^\"]+)\"/
31 url_matches = url_regex.match(line)
32 role_matches = role_regex.match(line)
33
34 if not url_matches.nil? and (url_matches.length) > 1
35 @url = url_matches[1]
36 end
37
38 if not role_matches.nil? and (role_matches.length > 1)
39 @role = role_matches[1]
40 end
41 end
42
43 def role
44 return @role
45 end
46
47 def url
48 return @url
49 end
50 end
51
52
53 class Bliptv < Website
54
55 VALID_BLIPTV_URL_REGEX = /^(http:\/\/)?blip\.tv\/.*?(\d+)$/
56
57 def self.owns_url?(url)
58 return url =~ VALID_BLIPTV_URL_REGEX
59 end
60
61
62 def get_video_url()
63 video_id = self.parse_video_id()
64 rss_page_url = "http://blip.tv/rss/flash/#{video_id}"
65 rss_data = get_page_data(rss_page_url)
66 video_url = parse_video_url(rss_data)
67
68 return video_url
69 end
70
71
72 protected;
73
74 def parse_video_id()
75 video_id_regex = /(\d+)$/
76 matches = video_id_regex.match(@url)
77
78 if matches.nil? or (matches.length < 2)
79 raise StandardError.new("Couldn't parse the video id from the URL.")
80 else
81 return matches[1]
82 end
83 end
84
85
86 def choose_best_format(formats)
87 # 'formats' is assumed to be an array of BliptvMediaFormat. We
88 # return the best one (in terms of video quality).
89 formats.each do |f|
90 if f.url.nil? or f.role.nil?
91 formats.delete(f)
92 next
93 end
94
95 return f if f.role == "Source"
96 end
97
98 if formats.length == 0
99 raise StandardError.new("No valid formats in the RSS feed.")
100 else
101 # Return whatever's left if we don't have a 'Source' video.
102 return formats[0]
103 end
104 end
105
106 def parse_video_url(page_data)
107 # All of the elements containing video URLs begin like this.
108 media_regex = /^\s*<media:content/
109
110 formats = []
111 # Create an array of BliptvMediaFormat from lines matching
112 # media_regex.
113 page_data.lines.each do |line|
114 if (line =~ media_regex)
115 bp = BliptvMediaFormat.new(line)
116 formats << bp
117 end
118 end
119
120 # And return the URL from the best one.
121 # choose_best_format will raise an error if need be.
122 return choose_best_format(formats).url
123 end
124
125 end