]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - src/websites/veoh.rb
c961d688e213b9500db2ab20726e966b2d896894
[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()
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 = self.parse_video_id()
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 # We trust our HTTP library to do the right thing here.
50 return redirect_url
51 end
52
53
54 def get_video_filename()
55 return (self.parse_video_id() + '.flv')
56 end
57
58 protected;
59
60 def parse_video_id()
61 video_id_regex = /[[:alnum:]]+$/
62 matches = video_id_regex.match(@url)
63 video_id = matches[0] if not matches.nil?
64
65 return video_id
66 end
67
68
69 # The main video page has the "video" URL buried
70 # in some javascript parameters.
71 def parse_redirect_url(page_data)
72 redirect_url_regex = /fullPreviewHashPath=\"(.*?)\"/
73 matches = redirect_url_regex.match(page_data)
74 redirect_url = matches[1] if not (matches.nil? || matches.length < 1)
75
76 return redirect_url
77 end
78
79
80 def get_page_data(url)
81 uri = URI.parse(url)
82
83 response = Net::HTTP.start(uri.host, uri.port) do |http|
84 http.get(uri.path)
85 end
86
87 return response.body
88 end
89
90 end