]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - lib/whatever-dl/websites/howcast.rb
Move all of the 'src' code under the more-standard 'lib'.
[dead/whatever-dl.git] / lib / whatever-dl / websites / howcast.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 Howcast < Website
22
23 VALID_HOWCAST_URL_REGEX = /^(http:\/\/)?(www\.)?howcast\.com\/videos\/(\d+)-(.+)$/
24
25 def self.owns_url?(url)
26 return url =~ VALID_HOWCAST_URL_REGEX
27 end
28
29
30 def get_video_url()
31 video_id = parse_video_id()
32 xml_url = "http://www.howcast.com/videos/#{video_id}.xml"
33 xml_data = self.get_page_data(xml_url)
34 filepath = parse_file_path_from_xml(xml_data)
35
36 return "http://www.howcast.com#{filepath}"
37 end
38
39 protected;
40
41 def parse_video_id()
42 # This regex just pulls out the video id
43 id_regex = /\/(\d+)-/
44 matches = id_regex.match(@url)
45
46 if matches.nil?
47 raise StandardError.new('The URL is a valid Howcast URL, but does not match on the digit portion of the regex. Since the digit portion is a subset of the "valid" regex, this should never occur.')
48 end
49
50 return matches[1]
51 end
52
53
54 def parse_file_path_from_xml(data)
55 file_path_regex = /<filename>(.*?)<\/filename>/
56 matches = file_path_regex.match(data)
57
58 if matches.nil?
59 raise StandardError.new("Couldn't parse the <filename> tag from the XML file.")
60 end
61
62 return matches[1]
63 end
64
65 end