-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_imdb.rb
executable file
·113 lines (91 loc) · 2.4 KB
/
check_imdb.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
#!/usr/bin/env ruby
# Coypright (C) 2018 Christian Mayer <christian@fox21.at>
# Check on IMDb if a TV series has ended.
require 'net/http'
require 'json'
require 'optparse'
require 'cgi'
class ImdbError < RuntimeError
attr_reader :state
def initialize(msg = 'UNKNOWN', state = 3)
@state = state
super(msg)
end
end
IMDB_TITLE_BASE_URL = 'https://www.imdb.com/title/%s/'
STATES = ['OK', 'WARNING', 'CRITICAL', 'UNKNOWN']
@options = {
:url => nil,
:title => nil,
:is_series => false,
}
opts = OptionParser.new do |o|
o.banner = 'Usage: [--url <string>|--title <id>] [--series]'
o.separator('')
o.on('-u', '--url <string>', 'URL') do |url|
@options[:url] = url
end
o.on('-t', '--title <id>', 'ID. E.g. tt1837492') do |title|
@options[:title] = title
@options[:url] = IMDB_TITLE_BASE_URL % [title]
end
o.on('-s', '--series', 'Title is a TV series.') do
@options[:is_series] = true
end
o.on_tail('-h', '--help', 'Show this message.') do
puts o
puts
exit 3
end
end
ARGV << '-h' if ARGV.count == 0
commands = opts.parse(ARGV)
# Build URL.
url = @options[:url]
uri = URI(url)
# Make response to API.
response = Net::HTTP.get(uri)
state = 0
msg = 'N/A'
begin
if @options[:is_series]
regex = /<meta property='og:title' content="(.{1,40}) \(/
names = response.scan(regex)
if names.nil? || names.length == 0 || names.first.nil? || names.first.length == 0
regex = /<title>(.{1,40}) \(/
titles = response.scan(regex)
if titles.nil? || titles.length == 0 || titles.first.nil? || titles.first.length == 0
raise ImdbError.new('No Name found (<title>)')
else
name = CGI.unescapeHTML(titles.first.first)
end
else
name = names.first.first
end
regex = />TV Series \((\d{4})[^0-9]{1,3}(\d{0,4})/
dates = response.scan(regex)
if dates.nil? || dates.length == 0 || dates.first.nil? || dates.first.length == 0
raise ImdbError.new('No series: "%s"' % [name])
end
inner = dates.shift
begin_year, end_year = inner.map{ |s| s.to_i }
if end_year != 0 && end_year >= begin_year
state = 2
msg = 'TV Series "%s" (%d-%d) has ended' % [name, begin_year, end_year]
else
state = 0
msg = 'TV Series "%s" (%d)' % [name, begin_year]
end
else
raise ImdbError.new('Invalid options')
end
rescue ImdbError => e
state = e.state
msg = e.message
end
state_name = STATES[state]
perf_data = [
state_name, msg, # Normal Output
]
puts "%s: %s" % perf_data
exit state