]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - src/website.rb
Factored out the Youtube implementation of get_page_data into the Website superclass.
[dead/whatever-dl.git] / src / website.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 # Necessary in a lot of subclasses; plus, we need it
20 # to parse the server name out of our URL.
21 require 'uri'
22
23 # This class keeps track of all its subclasses
24 # We use this to loop through every "website" in an
25 # attempt to determine to which site a URL belongs.
26 class Website
27
28 protected;
29
30 @url = nil
31
32
33 def self.inherited(subclass)
34 if superclass.respond_to? :inherited
35 superclass.inherited(subclass)
36 end
37
38 # Every time we're subclassed, add the new
39 # subclass to our list of subclasses.
40 @subclasses ||= []
41 @subclasses << subclass
42 end
43
44
45 def server
46 # Get the HTTP server portion of our URI
47 uri = URI.parse(@url)
48 return uri.host
49 end
50
51
52
53 def get_page_data(url)
54 # A naive implementation that just grabs the
55 # data from a page.
56 uri = URI.parse(url)
57
58 response = Net::HTTP.start(uri.host, uri.port) do |http|
59 http.get(uri.request_uri)
60 end
61
62 return response.body
63 end
64
65
66
67 public;
68
69 def initialize(url)
70 @url = url
71 end
72
73
74 def self.create(url)
75 # Factory method returning an instance of
76 # the appropriate subclass.
77
78 # Check the URL against each website's class.
79 # The class will know whether or not the URL
80 # "belongs" to its website.
81 @subclasses.each do |w|
82 if w.owns_url?(url)
83 return w.new(url)
84 end
85 end
86
87 # If nothing matched, we don't return an instance
88 # of anything.
89 return nil
90 end
91
92
93 # Abstract definition. Each subclass of Website
94 # should support it on its own.
95 def self.owns_url?(url)
96 raise NotImplementedError
97 end
98
99
100 # Same here. Abstract.
101 def get_video_url()
102 raise NotImplementedError
103 end
104
105
106 # The website class should be responsible for determining the
107 # video's filename. By default, we can take the last component
108 # of the video URL, but in some cases, subclasses will want
109 # to override this behavior.
110 def get_video_filename()
111 # Use whatever comes after the final front slash.
112 return get_video_url().split('/').pop()
113 end
114
115 end