]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - src/websites/veoh.rb
e10d9853d7ccaf45f4ffb823d6e5f13cd2362cec
[dead/whatever-dl.git] / src / websites / veoh.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 # Needed to download the page, which is in turn
22 # needed because it contains the video (redirect) URL.
23 require 'net/http'
24 require 'uri'
25
26
27 class Veoh < Website
28
29 VALID_VEOH_URL_REGEX = /^(http:\/\/)?(www\.)?veoh\.com\/videos\/([[:alnum:]]+)$/
30
31 def self.owns_url?(url)
32 return url =~ VALID_VEOH_URL_REGEX
33 end
34
35
36 def get_video_url(url)
37 # First, figure out the video id from the URL.
38 # Then, use the video id to construct the video details URL.
39 # Get the video details page, and parse the redirect
40 # URL from it. Now, I guess we *could* retrieve the video
41 # id from the redirect, but for now we're going to rely
42 # on our HTTP library to follow the redirect for us and
43 # save us a step.
44 video_id = parse_video_id(url)
45 details_url = "http://www.veoh.com/rest/video/#{video_id}/details"
46 details_data = get_page_data(details_url)
47 redirect_url = parse_redirect_url(details_data)
48
49 # Being slightly explicit about what we're doing here...
50 video_url = redirect_url
51
52 return video_url
53 end
54
55
56 protected;
57
58 def parse_video_id(url)
59 video_id_regex = /[[:alnum:]]+$/
60 matches = video_id_regex.match(url)
61 video_id = matches[0] if not matches.nil?
62
63 return video_id
64 end
65
66
67 # The main video page has the "video" URL buried
68 # in some javascript parameters.
69 def parse_redirect_url(page_data)
70 redirect_url_regex = /fullPreviewHashPath=\"(.*?)\"/
71 matches = redirect_url_regex.match(page_data)
72 redirect_url = matches[1] if not (matches.nil? || matches.length < 1)
73
74 return redirect_url
75 end
76
77
78 def get_page_data(url)
79 uri = URI.parse(url)
80
81 response = Net::HTTP.start(uri.host, uri.port) do |http|
82 http.get(uri.path)
83 end
84
85 return response.body
86 end
87
88 end