-
Notifications
You must be signed in to change notification settings - Fork 118
/
GetShowDetails.rb
executable file
·142 lines (112 loc) · 3.82 KB
/
GetShowDetails.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env ruby
# This file is part of the TVShows source code.
# http://github.com/mattprice/TVShows
# TVShows is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
requires = [
'open-uri',
File.join(File.dirname(__FILE__), 'TVShowsScript/TVShowsConstants.rb'),
File.join(File.dirname(__FILE__), 'TVShowsScript/lib/simple-rss.rb'),
File.join(File.dirname(__FILE__), 'TVShowsScript/lib/plist.rb')
]
def die(message)
time = Time.new
$stderr.puts "#{time.strftime("%m/%d/%y %H:%M:%S")}\tTVShows Error: #{message}"
exit(-1)
end
def printError(message)
time = Time.new
$stderr.puts "#{time.strftime("%m/%d/%y %H:%M:%S")}\tTVShows Error: #{message}"
end
def printException(exception)
time = Time.new
$stderr.puts "#{time.strftime("%m/%d/%y %H:%M:%S")}\tTVShows Error: #{exception.inspect}\n\t#{exception.backtrace.join("\n\t")}"
end
if ( ARGV.length != 1 ) then
die("Usage: TVShowDetails.rb ShowName")
exit(-1)
end
requires.each { |r|
begin
die("Could not load #{r}") unless require r
rescue => e
printException(e)
end
}
class Show
def initialize(name)
@feed = "http://ezrss.it/search/index.php?show_name=%s&show_name_exact=true&mode=rss"
@name = name
end
def getDetails
url = @feed % @name
begin
rawEpisodes = SimpleRSS.parse(open(url))
rescue => exception
printError("Could not connect to ezrss.it (#{exception.message})")
raise "GetShowDetailsError"
end
episodesBySeasonAndEpisodeNumber = []
episodesByDate = []
episodesByTitle = []
rawEpisodes.items.each do |episode|
#nameMatch = /Show\ Name\s*:\ (.*?);/.match(episode.description)[1].sub(/\/|:/,'-')
titleMatch = /Show\s*Title\s*:\s*(.*?);/.match(episode.description)
seasonNoMatch = /Season\s*:\s*([0-9]*?);/.match(episode.description)
episodeNoMatch = /Episode\s*:\s*([0-9]*?)$/.match(episode.description)
dateMatch = /Episode\s*Date:\s*([0-9\-]+)$/.match(episode.description)
if ( !seasonNoMatch.nil? and !episodeNoMatch.nil? ) then
# Season/episode style of show (eg: Lost)
s = seasonNoMatch[1].to_i
e = episodeNoMatch[1].to_i
t = episode.pubDate
if ( episodesBySeasonAndEpisodeNumber.find_all { |ep| ep[SHOW_SEASON] == s and ep[SHOW_EPISODE] == e and (ep[SHOW_TIME] <=> t) > 0 }.length == 0 ) then
episodesBySeasonAndEpisodeNumber << {
SHOW_SEASON => s,
SHOW_EPISODE => e,
SHOW_TIME => t,
SHOW_TYPE => TYPE_SEASONEPISODE
}
end
elsif ( !dateMatch.nil? )
# Date style of show (eg: The Daily Show)
begin
d = Time.parse(dateMatch[1])
t = episode.pubDate
if ( episodesByDate.find_all { |ep| ep[SHOW_DATE] == d and (ep[SHOW_TIME] <=> t) > 0 }.length == 0 ) then
episodesByDate << {
SHOW_DATE => d,
SHOW_TIME => episode.pubDate,
SHOW_TYPE => TYPE_DATE
}
end
rescue Exception => e
end
elsif ( !titleMatch.nil? and titleMatch[1] != "n/a")
# Get-all-episodes style of show (eg: Discovery Channel)
ti = titleMatch[1]
t = episode.pubDate
if ( episodesByTitle.find_all { |ep| ep[SHOW_TIME] == ti and (ep[SHOW_TIME] <=> t) > 0 }.length == 0 ) then
episodesByTitle << {
SHOW_TITLE => titleMatch[1],
SHOW_TIME => episode.pubDate,
SHOW_TYPE => TYPE_TIME
}
end
else
# Should not happen
printError("Could not categorize episode.")
end
end
return [episodesBySeasonAndEpisodeNumber,episodesByDate,episodesByTitle].max{|a,b| a.length<=>b.length}.to_plist
end
end
begin
$stdout.puts Show.new(ARGV[0]).getDetails
rescue Timeout::Error, Exception => e
printError("GetShowDetails error (#{e.inspect})")
exit(1)
end
exit(0)