dtcterm 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dtcterm.rb +156 -128
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa8822858a504b7308995e2be55be28c4cdadab4
4
- data.tar.gz: eb49a9747038d33355fc544e0244b9acfdda9414
3
+ metadata.gz: d842481e6c506a5203953357616a94012c6b21b2
4
+ data.tar.gz: c76ce49a90611925641bf9515e33e123952032f7
5
5
  SHA512:
6
- metadata.gz: e7434d69a5f9c8393e56429f43e049e40e79a95373a0868e4dc2f0bbad98925585e97238784a106071b7e2a52d78d20acd2d2e97dce0df545e81f7b2df053d05
7
- data.tar.gz: f06b292f800507eed643195d624917125f96b7a40f875a69ab8c250e28bc550e3839f6432fc6e2544319fcd5a12a3d6b8be67d071da89d4ef730a992e3293f66
6
+ metadata.gz: 93ae2dd1e594fa7621c1264115759a3e003f51b36b7703a12e828d0db855e201fe588069434a8330be3a6be6518dbc5e9364206889f45a98a01ec12a6ee0117f
7
+ data.tar.gz: 3812a0e252d61ab02e2342070b9006a1dac0222ec6592c8b1381115f8568be4e101b7bc6d17515efa1e1f3ed832a697ee227224472bca129e583d9bc340face6
data/lib/dtcterm.rb CHANGED
@@ -1,175 +1,203 @@
1
1
  module Dtcterm
2
- begin
3
- require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
4
- rescue LoadError
5
- raise 'win32console is needed to use color on Windows. Try `gem install win32console`'
6
- end
2
+ begin
3
+ require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
4
+ rescue LoadError
5
+ raise 'win32console is needed to use color on Windows. Try `gem install win32console`'
6
+ end
7
7
 
8
- require 'htmlentities'
9
- require 'optparse'
10
- require 'open-uri'
11
- require 'nokogiri'
12
- require 'rubygems'
8
+ require 'htmlentities'
9
+ require 'optparse'
10
+ require 'open-uri'
11
+ require 'nokogiri'
12
+ require 'rubygems'
13
13
 
14
14
 
15
- class << self
15
+ class << self
16
16
 
17
- $using_color = true
18
- $dtc_choice = ""
17
+ $using_color = true
18
+ $dtc_choice = ""
19
+ $page = 1
19
20
 
20
- BASE_QUOTE_LINK = "http://danstonchat.com/id.html"
21
+ BASE_QUOTE_LINK = "http://danstonchat.com/id.html"
21
22
 
22
- DTC_FLOP_URL = "http://danstonchat.com/flop50.html"
23
- DTC_LATEST_URL = "http://danstonchat.com/latest.html"
24
- DTC_RANDOM_URL = "http://danstonchat.com/random.html"
25
- DTC_RANDOM_POSITIVE_URL = "http://danstonchat.com/random0.html"
26
- DTC_TOP_URL = "http://danstonchat.com/top50.html"
23
+ DTC_FLOP_URL = "http://danstonchat.com/flop50.html"
24
+ DTC_CHRONOLOGICAL_ORDER = "http://danstonchat.com/browse.html"
25
+ DTC_LATEST_URL = "http://danstonchat.com/latest.html"
26
+ DTC_RANDOM_URL = "http://danstonchat.com/random.html"
27
+ DTC_RANDOM_POSITIVE_URL = "http://danstonchat.com/random0.html"
28
+ DTC_TOP_URL = "http://danstonchat.com/top50.html"
27
29
 
28
- def version
29
- '0.2.1'
30
- end
30
+ def version
31
+ '0.3.0'
32
+ end
31
33
 
32
34
 
33
35
 
34
- class Quote
35
- attr_accessor :id
36
- attr_accessor :link
37
- attr_accessor :quote
36
+ class Quote
37
+ attr_accessor :id
38
+ attr_accessor :link
39
+ attr_accessor :quote
38
40
 
39
- def initialize
40
- @id = 0
41
- @link = ""
42
- @quote = Array.new { Array.new(2) }
43
- end
41
+ def initialize
42
+ @id = 0
43
+ @link = ""
44
+ @quote = Array.new { Array.new(2) }
45
+ end
44
46
 
45
- def colorize(text)
46
- "\e[32m#{text}\e[0m"
47
- end
47
+ def colorize(text)
48
+ "\e[32m#{text}\e[0m"
49
+ end
48
50
 
49
- def display
50
- print "Quote ##{id}\n"
51
- print "Link: #{link}\n"
52
- @quote.each do |item|
53
- print "#{$using_color ? colorize(item[0]) : item[0]} #{item[1]}\n"
54
- end
55
- end
51
+ def display
52
+ print "Quote ##{id}\n"
53
+ print "Link: #{link}\n"
54
+ @quote.each do |item|
55
+ print "#{$using_color ? colorize(item[0]) : item[0]} #{item[1]}\n"
56
56
  end
57
+ end
58
+ end
57
59
 
58
60
 
59
61
 
60
- def get_page_html(url)
61
- begin
62
- Nokogiri::HTML(open(url))
63
- rescue
64
- puts "Can't reach the server."
65
- exit 1
66
- end
67
- end
68
-
69
- def get_quote_list(html)
70
- quote_list = Array.new
71
-
72
- html.css('div.item-listing').css('div.item').each do |item|
73
- quote = Quote.new
74
- quote.id = item["class"].gsub(/[^\d]/, '')
75
- quote.link = BASE_QUOTE_LINK.gsub("id", quote.id)
62
+ def get_page_html(url)
63
+ begin
64
+ Nokogiri::HTML(open(url))
65
+ rescue
66
+ puts "Can't reach the server."
67
+ exit 1
68
+ end
69
+ end
76
70
 
77
- quote_item = item.children()[0].children()[0]
71
+ def get_quote_list(html)
72
+ quote_list = Array.new
78
73
 
79
- quote_item.children().each do |i|
80
- if i["class"] == "decoration"
81
- i.content = "|newline|" + i.content + "|decorationclass|"
82
- end
83
- end
74
+ html.css('div.item-listing').css('div.item').each do |item|
75
+ quote = Quote.new
76
+ quote.id = item["class"].gsub(/[^\d]/, '')
77
+ quote.link = BASE_QUOTE_LINK.gsub("id", quote.id)
84
78
 
85
- while quote_item
86
- str = HTMLEntities.new.decode quote_item.content.gsub(/\|newline\|/, "\n")
79
+ quote_item = item.children()[0].children()[0]
87
80
 
88
- str.each_line do |line|
89
- unless line =~ /^[[:space:]]+$/
90
- first, second = line.split(/\|decorationclass\|/)
91
- quote.quote << [first.strip(), second ? second.strip() : second]
92
- end
93
- end
81
+ quote_item.children().each do |i|
82
+ if i["class"] == "decoration"
83
+ i.content = "|newline|" + i.content + "|decorationclass|"
84
+ end
85
+ end
94
86
 
95
- quote_list << quote
96
- if (quote_item != nil)
97
- quote_item = quote_item.next()
98
- end
99
- end
87
+ while quote_item
88
+ str = HTMLEntities.new.decode quote_item.content.gsub(/\|newline\|/, "\n")
100
89
 
90
+ str.each_line do |line|
91
+ unless line =~ /^[[:space:]]+$/
92
+ first, second = line.split(/\|decorationclass\|/)
93
+ quote.quote << [first.strip(), second ? second.strip() : second]
101
94
  end
95
+ end
102
96
 
103
- quote_list
104
- end
105
-
106
- def display_quote_list(quote_list)
107
- quote_list.each do |quote|
108
- quote.display
109
- print "\n\n"
110
- end
97
+ quote_list << quote
98
+ if (quote_item != nil)
99
+ quote_item = quote_item.next()
100
+ end
111
101
  end
112
102
 
103
+ end
113
104
 
105
+ quote_list
106
+ end
114
107
 
115
- def main(options)
116
- def self.quote_already_set
117
- unless $dtc_choice.empty?
118
- puts("Un choix de quote a déjà été fait.")
119
- exit 1
120
- end
121
- end
108
+ def display_quote_list(quote_list)
109
+ quote_list.each do |quote|
110
+ quote.display
111
+ print "\n\n"
112
+ end
113
+ end
122
114
 
123
- OptionParser.new do |opts|
124
115
 
125
- opts.banner = "Usage: dtcterm one_quote_option [color_option]"
126
116
 
127
- opts.version = version
117
+ def main(options)
118
+ def self.quote_already_set
119
+ unless $dtc_choice.empty?
120
+ puts("Un choix de quote a déjà été fait.")
121
+ exit 1
122
+ end
123
+ end
128
124
 
129
- opts.on("-c", "--no-color", "Enlève la couleur.") do
130
- $using_color = false
131
- end
125
+ OptionParser.new do |opts|
132
126
 
133
- opts.on("-f", "--flop", "Rubrique `flop50`.") do
134
- quote_already_set
135
- $dtc_choice = DTC_FLOP_URL
136
- end
127
+ opts.banner = "Usage: dtcterm one_quote_option [color_option]"
137
128
 
138
- opts.on("-l", "--latest", "Rubrique `Derniers ajours`.") do
139
- quote_already_set
140
- $dtc_choice = DTC_LATEST_URL
141
- end
129
+ opts.version = version
142
130
 
143
- opts.on("-r", "--random", "Rubrique `random`.") do
144
- quote_already_set
145
- $dtc_choice = DTC_RANDOM_URL
146
- end
131
+ opts.on("-c", "--no-color", "Enlève la couleur.") do
132
+ $using_color = false
133
+ end
147
134
 
148
- opts.on("-R", "--random0", "Rubrique `random > 0`") do
149
- quote_already_set
150
- $dtc_choice = DTC_RANDOM_POSITIVE_URL
151
- end
135
+ opts.on("-f", "--flop", "Rubrique `flop50`.") do
136
+ quote_already_set
137
+ $dtc_choice = DTC_FLOP_URL
138
+ end
152
139
 
153
- opts.on("-t", "--top", "Rubrique `top50`") do
154
- quote_already_set
155
- $dtc_choice = DTC_TOP_URL
156
- end
140
+ opts.on("-l", "--latest", "Rubrique `Derniers ajours`.") do
141
+ quote_already_set
142
+ $dtc_choice = DTC_LATEST_URL
143
+ end
157
144
 
158
- end.parse!(options)
145
+ opts.on("-o", "--order", "Rubrique `Ordre chronologique`.") do
146
+ quote_already_set
147
+ $dtc_choice = DTC_CHRONOLOGICAL_ORDER
148
+ end
159
149
 
160
- if $dtc_choice.empty?
161
- puts("Il faut choisir une rubrique.")
162
- exit 1
150
+ opts.on("-p", "--page PAGE", "Page choisie") do |p|
151
+ begin
152
+ $page = Integer(p)
153
+ if $page < 1
154
+ raise ArgumentError
163
155
  end
156
+ rescue
157
+ puts("L'argument page n'est pas un entier correct (doit être >= 1).")
158
+ exit 1
159
+ end
160
+ end
164
161
 
165
- unless options.empty?
166
- puts("Liste d'argument incorrects.")
167
- exit 1
168
- end
162
+ opts.on("-r", "--random", "Rubrique `random`.") do
163
+ quote_already_set
164
+ $dtc_choice = DTC_RANDOM_URL
165
+ end
169
166
 
170
- html = get_page_html($dtc_choice)
171
- quote_list = get_quote_list(html)
172
- display_quote_list(quote_list)
167
+ opts.on("-R", "--random0", "Rubrique `random > 0`") do
168
+ quote_already_set
169
+ $dtc_choice = DTC_RANDOM_POSITIVE_URL
173
170
  end
171
+
172
+ opts.on("-t", "--top", "Rubrique `top50`") do
173
+ quote_already_set
174
+ $dtc_choice = DTC_TOP_URL
175
+ end
176
+ end.parse!(options)
177
+
178
+ if $dtc_choice.empty?
179
+ puts("Il faut choisir une rubrique.")
180
+ exit 1
181
+ end
182
+
183
+ unless options.empty?
184
+ puts("Liste d'argument incorrects.")
185
+ exit 1
186
+ end
187
+
188
+ if $page > 1
189
+ if $dtc != DTC_LATEST_URL and $dtc_choice != DTC_CHRONOLOGICAL_ORDER
190
+ puts("Impossible de choisir une page pour cette catégorie.")
191
+ exit 1
192
+ else
193
+ fp = $dtc_choice.split(/\.html/,)
194
+ $dtc_choice = fp[0] + "/" + $page.to_s + ".html"
195
+ end
196
+ end
197
+
198
+ html = get_page_html($dtc_choice)
199
+ quote_list = get_quote_list(html)
200
+ display_quote_list(quote_list)
174
201
  end
202
+ end
175
203
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dtcterm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Loïc Runarvot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-14 00:00:00.000000000 Z
11
+ date: 2014-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri