Class: TvDotCom::Show
- Inherits:
-
Object
- Object
- TvDotCom::Show
- Defined in:
- lib/tv_dot_com.rb
Instance Attribute Summary collapse
-
#raw_guide ⇒ Object
readonly
returns the unprocessed raw html of the episode guide.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #episode_list_url(season = 1) ⇒ Object
-
#find_episode_by_season_and_number(season, number) ⇒ Object
loads all the episodes for this show and selects the Episode by the season number, and episode number within that season.
-
#initialize(url) ⇒ Show
constructor
A new instance of Show.
-
#name ⇒ Object
the name of the show.
- #number_of_seasons ⇒ Object
- #seasons ⇒ Object
- #show_id ⇒ Object
- #show_url ⇒ Object
Constructor Details
#initialize(url) ⇒ Show
Returns a new instance of Show.
15 16 17 18 |
# File 'lib/tv_dot_com.rb', line 15 def initialize(url) @request = url @doc = Hpricot(open(@url = Show.guides_url(url))) end |
Instance Attribute Details
#raw_guide ⇒ Object (readonly)
returns the unprocessed raw html of the episode guide
32 33 34 |
# File 'lib/tv_dot_com.rb', line 32 def raw_guide @raw_guide end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
12 13 14 |
# File 'lib/tv_dot_com.rb', line 12 def request @request end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
11 12 13 |
# File 'lib/tv_dot_com.rb', line 11 def url @url end |
Class Method Details
.find_show_id(name) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/tv_dot_com.rb', line 44 def self.find_show_id(name) search = name.gsub(/([A-Z])/) { |s| '+' + ('%c' % s[0]).downcase }.gsub(/_/, '+').gsub(/'/,'').gsub(/[^+a-z0-9]/,'+').gsub(/\++/,'+') begin (Hpricot(open("http://www.tv.com/search.php?type=11&stype=program&qs=#{search}&tag=filter;tv_shows"))/"table[@id='search-results'] span[@class='f-18'] a").first.attributes['href'].match(/show\/(\d+)\/summary/)[1] rescue raise "Can't find show id for #{name}" end end |
.guides_url(name) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/tv_dot_com.rb', line 36 def self.guides_url(name) if name.match(/^http:\/\/(.*?)\.tv\.com/) name else "http://www.tv.com/show/#{find_show_id(name)}/episode_guide.html&printable=1" end end |
Instance Method Details
#episode_list_url(season = 1) ⇒ Object
60 61 62 |
# File 'lib/tv_dot_com.rb', line 60 def episode_list_url(season=1) "http://www.tv.com/show/#{show_id}/episode_listings.html?season=#{season}&tag=nav_bar;2" end |
#find_episode_by_season_and_number(season, number) ⇒ Object
loads all the episodes for this show and selects the Episode by the season number, and episode number within that season
27 28 29 |
# File 'lib/tv_dot_com.rb', line 27 def find_episode_by_season_and_number(season,number) seasons[season.to_i - 1].episodes[number.to_i - 1] end |
#name ⇒ Object
the name of the show
21 22 23 |
# File 'lib/tv_dot_com.rb', line 21 def name @name ||= @doc.search("//h2[@class='mb-10']").first.inner_html.sub(/ Episode Guide/, '') end |
#number_of_seasons ⇒ Object
64 65 66 67 68 |
# File 'lib/tv_dot_com.rb', line 64 def number_of_seasons @number_of_seasons ||= (Hpricot(open(episode_list_url))/"div[@id='season-dropdown'] option").size || 2 - 1 @number_of_seasons = 1 if @number_of_seasons < 1 @number_of_seasons end |
#seasons ⇒ Object
70 71 72 73 74 |
# File 'lib/tv_dot_com.rb', line 70 def seasons @seasons ||= (1..number_of_seasons).collect do |i| Season.new(self, i) end end |
#show_id ⇒ Object
52 53 54 |
# File 'lib/tv_dot_com.rb', line 52 def show_id @show_id ||= self.class.find_show_id(name) end |
#show_url ⇒ Object
56 57 58 |
# File 'lib/tv_dot_com.rb', line 56 def show_url "http://www.tv.com/show/#{show_id}/summary.html" end |