rtv 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/bin/new.sh +2 -0
  2. data/bin/rtv +9 -26
  3. data/lib/rtv.rb +87 -44
  4. metadata +6 -6
  5. data/lib/#rtv.rb# +0 -269
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/bash
2
+ echo $*
data/bin/rtv CHANGED
@@ -5,31 +5,14 @@ require 'rtv'
5
5
  require 'yaml'
6
6
 
7
7
  if __FILE__ == $0
8
- config = {:senderfilter=>["pro7", "arte", "3sat", "rtl2", "sat1", "vox", "rtl", "k1"],
9
- :senderressource=>"sendergruppeId:1,3",
10
- :charset=>"UTF-8",
11
- :senderlimit=>15,
12
- :eine_pro_sender=>false,
13
- :redundanz=>false}
14
-
15
- newconfig = begin
16
- configfile = File.join(Gem.user_home, '.rtv')
17
- YAML.load(File.read(configfile))
18
- rescue
19
- {}
8
+ begin
9
+ puts RTV::Manager.getTVString(ARGV.dup)
10
+ # puts RTV::Manager.getTVString(["20", "pro7"])
11
+ rescue RTV::NoConnectionException => e
12
+ puts e.message
13
+ rescue RTV::NotFoundException => e
14
+ puts e.message
15
+ rescue RTV::UsageException => e
16
+ puts e.message
20
17
  end
21
-
22
- config.update newconfig
23
-
24
- swtch = RTV::Switcher.new
25
- swtch.config = config
26
-
27
- rtv = RTV::Fetcher.new
28
- rtv.config = swtch.config
29
- rtv.options = swtch.uri_options(ARGV.dup)
30
- program = rtv.fetch
31
-
32
- prtr = RTV::Presenter.new rtv.config[:charset]
33
- prtr.config = swtch.config
34
- prtr.show program
35
18
  end
data/lib/rtv.rb CHANGED
@@ -13,6 +13,45 @@ $KCODE = 'u'
13
13
  Channel = %w{ard zdf rtl sat1 pro7 k1 rtl2 super vox arte tele5 3sat dvier br hr mdr n3 rbb swr wdr bralp tvb h1 orf1 orf2 atv sf1 sf2 prs dwtv extra fes mux ztk zdoku zinfo dsf euro mtv viva nick kika qvc tw1 teno cc bbc cnn euron n24 ntv blm dmax nl1 nl2 nl3 pspo1 dir1 gold heima aplan butv disco junio mgm plane scifi 13th hit24 disge foge blum blume arena axn kinow sat1c silve histo nasn toon pr1 pr2 pr3 pr4 disne prfc prff prn prser krimi}
14
14
 
15
15
  module RTV
16
+ class UsageException < Exception
17
+ end
18
+ class NoConnectionException < Exception
19
+ end
20
+ class NotFoundException < Exception
21
+ end
22
+ class Manager
23
+ def self.getTVString args
24
+ config = {:senderfilter=>["pro7", "arte", "3sat", "rtl2", "sat1", "vox", "rtl", "k1"],
25
+ :senderressource=>"sendergruppeId:1,3",
26
+ :charset=>"UTF-8",
27
+ :senderlimit=>15,
28
+ :eine_pro_sender=>false,
29
+ :redundanz=>false}
30
+
31
+ newconfig = begin
32
+ configfile = File.join(Gem.user_home, '.rtv')
33
+ YAML.load(File.read(configfile))
34
+ rescue
35
+ {}
36
+ end
37
+
38
+ config.update newconfig
39
+
40
+ swtch = RTV::Switcher.new
41
+ swtch.config = config
42
+
43
+ rtv = RTV::Fetcher.new
44
+ rtv.config = swtch.config
45
+ rtv.options = swtch.uri_options(args)
46
+
47
+ program = rtv.fetch
48
+
49
+ prtr = RTV::Presenter.new rtv.config[:charset]
50
+ prtr.config = swtch.config
51
+ return prtr.to_s(program)
52
+
53
+ end
54
+ end
16
55
  class Program
17
56
  attr_reader :channel
18
57
 
@@ -26,7 +65,7 @@ module RTV
26
65
  end
27
66
  class Show
28
67
  attr_accessor :channel, :name, :time, :date, :desc, :showview, :options
29
-
68
+
30
69
  Filler = "\t"
31
70
 
32
71
  def to_s *lastshow
@@ -41,7 +80,7 @@ module RTV
41
80
  self.to_a.zip(lastshow.to_a).map{|x,y| (x == y) ? (' ' * x.to_s.size) : x}.reject{|x| x.empty?}.join(Filler)
42
81
  end
43
82
  end
44
-
83
+
45
84
  def to_a
46
85
  tmp = [@channel, @date, @time, @name]
47
86
  tmp << @desc if @options[:desc]
@@ -50,12 +89,12 @@ module RTV
50
89
  end
51
90
  class Fetcher
52
91
  attr_accessor :options, :config
53
-
92
+
54
93
  def initialize
55
94
  @options = {}
56
95
  @shows = Program.new
57
96
  end
58
-
97
+
59
98
  def fetch
60
99
  while search_results?(doc = get_hdoc)
61
100
  extract_shows doc
@@ -65,7 +104,7 @@ module RTV
65
104
  puts if $DEBUG
66
105
  return @shows
67
106
  end
68
-
107
+
69
108
  def extract_shows doc
70
109
  if @options[:format] == 'search'
71
110
  daten = (doc/"div[@id='titel-uebersicht']")
@@ -75,19 +114,19 @@ module RTV
75
114
  zeiten = zeiten.search("span.tv-sendung-uhrzeit")
76
115
  else
77
116
  puts "Keine Suchergebnisse!" if $DEBUG
78
- exit
117
+ raise NotFoundException, "Keine Suchergebnisse!"
79
118
  end
80
119
  zeiten.each do |zeit|
81
120
  show = Show.new
82
- show.options = @config
121
+ show.options = @config
83
122
  show.time = zeit.innerText
84
- show.desc, show.showview = time_to_desc_and_showview(zeit)
123
+ show.desc, show.showview = time_to_desc_and_showview(zeit)
85
124
  show.name = time_to_name(zeit)
86
125
  show.channel = time_to_channel(zeit)
87
126
  show.date = datum.innerText[/^\D*(.+) Suchergebnis/, 1]
88
127
  add_show show
89
128
  end
90
- end
129
+ end
91
130
  else
92
131
  zeiten = (doc/"span.tv-sendung-uhrzeit")
93
132
  zeiten.each do |zeit|
@@ -101,35 +140,34 @@ module RTV
101
140
  end
102
141
  end
103
142
  end
104
-
143
+
105
144
  private
106
145
  def add_show show
107
146
  @shows << show
108
147
  end
109
-
148
+
110
149
  def get_hdoc
111
150
  begin
112
151
  uri = convert_to_url("http://www.klack.de/") + '?' + hsh_to_rqst(@options)
113
152
  puts "URL: #{uri}" if $DEBUG
114
153
  doc = Hpricot(open(uri))
115
154
  rescue
116
- puts "No Network Connection!"
117
- exit
118
- end
155
+ raise NoConnectionException, "No Network Connection!"
156
+ end
119
157
  end
120
-
158
+
121
159
  def search_results? doc
122
160
  (doc/"td.noresult").empty?
123
161
  end
124
-
162
+
125
163
  def hsh_to_rqst hsh
126
164
  hsh.to_a.map{|x| x.map{|y| CGI.escape(y.to_s)}.join('=')}.join('&')
127
165
  end
128
-
166
+
129
167
  def time_to_name zeit
130
168
  zeit.parent.next_sibling.next_sibling.at("a.tv-sendung-titel").innerText
131
169
  end
132
-
170
+
133
171
  def time_to_channel zeit
134
172
  zeit.parent.parent.at("span.tv-sendung-info/a").attributes["href"][/channelId=([^&]+)/, 1].downcase
135
173
  end
@@ -140,23 +178,24 @@ module RTV
140
178
  showview = desc_showv[/\s+Showview ([0-9-]+)/, 1]
141
179
  [desc, showview]
142
180
  end
143
-
181
+
144
182
  def convert_to_url str
145
- # entferne alle störenden Buchstaben und erstelle die URL
183
+ # entfernt alle störenden Buchstaben und erstellt die URL
146
184
  url = Base64.decode64("aHR0cDovL3d3dy50dnRvZGF5LmRlL3Byb2dyYW0yMDA3")
147
185
  end
148
186
  end
149
-
187
+
150
188
  class Presenter
151
189
  Source_charset = 'ISO-8859-1'
152
-
190
+
153
191
  attr_accessor :config
154
-
192
+
155
193
  def initialize charset = 'UTF-8'
156
194
  @target_charset = charset
157
195
  end
158
196
 
159
- def show program
197
+ def to_s program
198
+ returnstring = ""
160
199
  senderlimit = @config[:senderlimit]
161
200
  @config[:senderfilter].each do |channel|
162
201
  lastshow = nil
@@ -166,19 +205,24 @@ module RTV
166
205
  tmp.each do |show|
167
206
  return if (@config[:senderlimit] -= 1) < 1
168
207
 
169
- puts Iconv.iconv(@target_charset, Source_charset, show.to_s(lastshow))
170
-
208
+ returnstring << Iconv.iconv(@target_charset, Source_charset, show.to_s(lastshow)).to_s << "\n"
209
+
171
210
  break if @config[:eine_pro_sender]
172
- lastshow = show
211
+ lastshow = show
173
212
  end
174
213
  end
175
214
  end
215
+ returnstring
216
+ end
217
+
218
+ def show programm
219
+ puts to_s(programm)
176
220
  end
177
221
  end
178
-
222
+
179
223
  class Switcher
180
224
  attr_accessor :config
181
-
225
+
182
226
  Usage = 'RTV - a command line ruby tv guide
183
227
 
184
228
  USAGE
@@ -209,58 +253,57 @@ EXAMPLES
209
253
 
210
254
  Beliebige sinnvolle Kombinationen in beliebiger Reihenfolge sind möglich:
211
255
  tv pro7 -d 20 11.07. sat1 - Alle Sendungen auf Pro7 und Sat.1 um 20 Uhr am 11.07. mit Beschreibung'
212
-
256
+
213
257
  def initialize
214
258
  @uri_options = {}
215
259
  end
216
-
260
+
217
261
  def uri_options args
218
262
  set_defaults
219
-
263
+
220
264
  tmp_channel = @config[:senderfilter]
221
265
  @config[:senderfilter] = []
222
-
266
+
223
267
  args.each do |arg|
224
268
  case arg.downcase
225
-
269
+
226
270
  # Konfigurationenmodus
227
271
  when /(\w+):(\w+)/
228
272
  @config.update YAML.load("--- :" + $1 + ": " + $2)
229
-
273
+
230
274
  # Hilfemodus
231
275
  when "help"
232
- puts Usage
233
- exit
234
-
276
+ raise UsageException, Usage
277
+
235
278
  # Beschreibungmodus
236
279
  when "-d"
237
280
  @config[:desc] = true
238
-
281
+
239
282
  # Datummodus
240
283
  when /^\d{1,2}\.\d{1,2}\.$/
241
284
  @uri_options[:date] = arg + Time.now.year.to_s
242
-
285
+
243
286
  # Stundenmodus
244
287
  when /^\d{1,2}(?:\d{2})?$/
245
288
  @uri_options[:time] = arg
246
-
289
+
247
290
  # Channelname oder -alias
248
291
  when *Channel
249
292
  @config[:senderfilter] << arg.downcase
250
-
293
+
251
294
  # Suchmodus
252
295
  else
253
296
  @uri_options.update({:search => arg, :format => 'search', :time => 'all', :date => 'all', :slotIndex => 'all'})
254
297
  @config[:senderfilter] = Channel
255
298
  end
256
299
  end
257
-
300
+
258
301
  # when channels are specified, use them only
259
302
  @config[:senderfilter] = tmp_channel if @config[:senderfilter].empty?
260
303
 
261
304
  @uri_options
262
305
  end
263
-
306
+
264
307
  private
265
308
  def set_defaults
266
309
  # Haupt- und Regionalsender
metadata CHANGED
@@ -3,13 +3,13 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: rtv
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.6
7
- date: 2007-07-16 00:00:00 +02:00
6
+ version: 0.1.7
7
+ date: 2007-09-30 00:00:00 +02:00
8
8
  summary: RTV is a command line tv guide for german television.
9
9
  require_paths:
10
10
  - lib
11
11
  email: rretzbach@gmail.com
12
- homepage: http://rubyforge.org/projects/rtv/1
12
+ homepage: http://rubyforge.org/projects/rtv/
13
13
  rubyforge_project:
14
14
  description:
15
15
  autorequire: rtv
@@ -30,7 +30,7 @@ authors:
30
30
  - Robert Retzbach
31
31
  files:
32
32
  - bin/rtv
33
- - lib/#rtv.rb#
33
+ - bin/new.sh
34
34
  - lib/rtv.rb
35
35
  - doc/senderliste.txt
36
36
  - etc/config.yaml.example
@@ -41,8 +41,8 @@ rdoc_options: []
41
41
 
42
42
  extra_rdoc_files:
43
43
  - README
44
- executables:
45
- - rtv
44
+ executables: []
45
+
46
46
  extensions: []
47
47
 
48
48
  requirements: []
@@ -1,269 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'rubygems'
4
- require 'hpricot'
5
- require 'open-uri'
6
- require 'cgi'
7
- require 'iconv'
8
- require 'time'
9
- require 'base64'
10
-
11
- $KCODE = 'u'
12
-
13
- Channel = %w{ard zdf rtl sat1 pro7 k1 rtl2 super vox arte tele5 3sat dvier br hr mdr n3 rbb swr wdr bralp tvb h1 orf1 orf2 atv sf1 sf2 prs dwtv extra fes mux ztk zdoku zinfo dsf euro mtv viva nick kika qvc tw1 teno cc bbc cnn euron n24 ntv blm dmax nl1 nl2 nl3 pspo1 dir1 gold heima aplan butv disco junio mgm plane scifi 13th hit24 disge foge blum blume arena axn kinow sat1c silve histo nasn toon pr1 pr2 pr3 pr4 disne prfc prff prn prser krimi}
14
-
15
- module RTV
16
- class Program
17
- attr_reader :channel
18
-
19
- def initialize
20
- @channel = {}
21
- end
22
- def << show
23
- @channel[show.channel] ||= []
24
- @channel[show.channel] << show
25
- end
26
- end
27
- class Show
28
- attr_accessor :channel, :name, :time, :date, :desc, :showview, :options
29
-
30
- Filler = "\t"
31
-
32
- def to_s *lastshow
33
- if @options[:redundanz]
34
- self.to_a.compact.join(Filler)
35
- else
36
- lastshow = if lastshow.first.nil?
37
- [()] * to_a.size
38
- else
39
- lastshow.first
40
- end
41
- self.to_a.zip(lastshow.to_a).map{|x,y| (x == y) ? (' ' * x.to_s.size) : x}.reject{|x| x.empty?}.join(Filler)
42
- end
43
- end
44
-
45
- def to_a
46
- tmp = [@channel, @date, @time, @name]
47
- tmp << @desc if @options[:desc]
48
- tmp
49
- end
50
- end
51
- class Fetcher
52
- attr_accessor :options, :config
53
-
54
- def initialize
55
- @options = {}
56
- @shows = Program.new
57
- end
58
-
59
- def fetch
60
- while search_results?(doc = get_hdoc)
61
- extract_shows doc
62
- @options[:offset] += 1
63
- print "." if $DEBUG
64
- end
65
- puts if $DEBUG
66
- return @shows
67
- end
68
-
69
- def extract_shows doc
70
- if @options[:format] == 'search'
71
- daten = (doc/"div[@id='titel-uebersicht']")
72
- daten.each do |datum|
73
- zeiten = datum.parent.parent.parent.next_sibling
74
- unless zeiten.nil?
75
- zeiten = zeiten.search("span.tv-sendung-uhrzeit")
76
- else
77
- puts "Keine Suchergebnisse!" if $DEBUG
78
- exit
79
- end
80
- zeiten.each do |zeit|
81
- show = Show.new
82
- show.options = @config
83
- show.time = zeit.innerText
84
- show.desc, show.showview = time_to_desc_and_showview(zeit)
85
- show.name = time_to_name(zeit)
86
- show.channel = time_to_channel(zeit)
87
- show.date = datum.innerText[/^\D*(.+) Suchergebnis/, 1]
88
- add_show show
89
- end
90
- end
91
- else
92
- zeiten = (doc/"span.tv-sendung-uhrzeit")
93
- zeiten.each do |zeit|
94
- show = Show.new
95
- show.options = @config
96
- show.time = zeit.innerText
97
- show.desc, show.showview = time_to_desc_and_showview(zeit)
98
- show.name = time_to_name(zeit)
99
- show.channel = time_to_channel(zeit)
100
- add_show show
101
- end
102
- end
103
- end
104
-
105
- private
106
- def add_show show
107
- @shows << show
108
- end
109
-
110
- def get_hdoc
111
- begin
112
- uri = convert_to_url("http://www.klack.de/") + '?' + hsh_to_rqst(@options)
113
- puts "URL: #{uri}" if $DEBUG
114
- doc = Hpricot(open(uri))
115
- rescue
116
- puts "No Network Connection!"
117
- exit
118
- end
119
- nur ein test ae öpseraf
120
- end
121
-
122
- def search_results? doc
123
- (doc/"td.noresult").empty?
124
- end
125
-
126
- def hsh_to_rqst hsh
127
- hsh.to_a.map{|x| x.map{|y| CGI.escape(y.to_s)}.join('=')}.join('&')
128
- end
129
-
130
- def time_to_name zeit
131
- zeit.parent.next_sibling.next_sibling.at("a.tv-sendung-titel").innerText
132
- end
133
-
134
- def time_to_channel zeit
135
- zeit.parent.parent.at("span.tv-sendung-info/a").attributes["href"][/channelId=([^&]+)/, 1].downcase
136
- end
137
-
138
- def time_to_desc_and_showview zeit
139
- desc_showv = zeit.parent.parent.at("span.tv-sendung-info").innerText.strip.gsub(/[\t\r\n]+/, "\t")
140
- desc = desc_showv.sub(/^\([^()]+\)\s+/, '').sub(/\s+Showview [0-9-]+/, '')
141
- showview = desc_showv[/\s+Showview ([0-9-]+)/, 1]
142
- [desc, showview]
143
- end
144
-
145
- def convert_to_url str
146
- # entferne alle störenden Buchstaben und erstelle die URL
147
- url = Base64.decode64("aHR0cDovL3d3dy50dnRvZGF5LmRlL3Byb2dyYW0yMDA3")
148
- end
149
- end
150
-
151
- class Presenter
152
- Source_charset = 'ISO-8859-1'
153
-
154
- attr_accessor :config
155
-
156
- def initialize charset = 'UTF-8'
157
- @target_charset = charset
158
- end
159
-
160
- def show program
161
- senderlimit = @config[:senderlimit]
162
- @config[:senderfilter].each do |channel|
163
- lastshow = nil
164
- tmp = program.channel[channel]
165
-
166
- unless tmp.nil?
167
- tmp.each do |show|
168
- return if (@config[:senderlimit] -= 1) < 1
169
-
170
- puts Iconv.iconv(@target_charset, Source_charset, show.to_s(lastshow))
171
-
172
- break if @config[:eine_pro_sender]
173
- lastshow = show
174
- end
175
- end
176
- end
177
- end
178
- end
179
-
180
- class Switcher
181
- attr_accessor :config
182
-
183
- Usage = 'RTV - a command line ruby tv guide
184
-
185
- USAGE
186
-
187
- tv [ELEMENT] ...
188
-
189
- ELEMENT kann folgende Werte enthalten:
190
-
191
- a) TIME als Sendezeit im Format HH oder HH:MM (maximal eine TIME sinnvoll)
192
- b) DATE als Datum im Format DD.MM
193
- b) CHANNEL als Sender aus der Liste in doc/senderliste.txt
194
- c) PATTERN als Suchbegriff (darf kein Sendername sein, siehe b))
195
- d) DESCRIPTION als Beschreibung, feste Zeichenkette "-d"
196
- e) HELP als Hilfe, feste Zeichenkette "help" (gibt diese Seite aus)
197
-
198
-
199
- EXAMPLES
200
-
201
- tv - Aktuelle Sendungen für ausgewählte Sender
202
- tv 20 - Heute 20 Uhr alle Sendungen für ausgewählte Sender
203
- tv 24.12. - Alle Sendungen an Heilig Abend für ausgewählte Sender
204
- tv pro7 sat1 - Aktuelle Sendungen von nur Pro7 und Sat.1
205
- tv simpsons - Alle Sendungen über Die Simpsons für alle Sender
206
- tv -d - Aktuelle Sendungen für ausgewählte Sender mit Beschreibung
207
-
208
- Beliebige sinnvolle Kombinationen in beliebiger Reihenfolge sind möglich:
209
- tv pro7 -d 20 11.07. sat1 - Alle Sendungen auf Pro7 und Sat.1 um 20 Uhr am 11.07. mit Beschreibung'
210
-
211
- def initialize
212
- @uri_options = {}
213
- end
214
-
215
- def uri_options args
216
- set_defaults
217
-
218
- tmp_channel = @config[:senderfilter]
219
- @config[:senderfilter] = []
220
-
221
- args.each do |arg|
222
- case arg.downcase
223
-
224
- # Konfigurationenmodus
225
- when /(\w+):(\w+)/
226
- @config.update YAML.load("--- :" + $1 + ": " + $2)
227
-
228
- # Hilfemodus
229
- when "help"
230
- puts Usage
231
- exit
232
-
233
- # Beschreibungmodus
234
- when "-d"
235
- @config[:desc] = true
236
-
237
- # Datummodus
238
- when /^\d{1,2}\.\d{1,2}\.$/
239
- @uri_options[:date] = arg + Time.now.year.to_s
240
-
241
- # Stundenmodus
242
- when /^\d{1,2}(?:\d{2})?$/
243
- @uri_options[:time] = arg
244
-
245
- # Channelname oder -alias
246
- when *Channel
247
- @config[:senderfilter] << arg.downcase
248
-
249
- # Suchmodus
250
- else
251
- @uri_options.update({:search => arg, :format => 'search', :time => 'all', :date => 'all', :slotIndex => 'all'})
252
- @config[:senderfilter] = Channel
253
- end
254
- end
255
-
256
- # when channels are specified, use them only
257
- @config[:senderfilter] = tmp_channel if @config[:senderfilter].empty?
258
-
259
- @uri_options
260
- end
261
-
262
- private
263
- def set_defaults
264
- # Haupt- und Regionalsender
265
- @uri_options = {:date => 'now', :time => 'now', :format => 'genre', :channel => @config[:senderressource], :order => 'time', :offset => 0}
266
- end
267
- end
268
- end
269
-