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