dtcterm 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/bin/dtcterm +4 -0
  3. data/lib/dtcterm.rb +168 -0
  4. data/test/test_dtcterm.rb +89 -0
  5. metadata +79 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b69f76d2f3f2366574fc3e4006a0dcabdd8505c8
4
+ data.tar.gz: c6908cde29359c58dca917f10ae2035a44e4959b
5
+ SHA512:
6
+ metadata.gz: 69fb41bef5e5c81f5c56229248ff37db426b769b9a4f1ef64331bcdbd2eae31f1964b033e6d4bf921e37d98001f52cd055f6f758d19e7f4b51aab7f4b44e1ac8
7
+ data.tar.gz: d2cafb6ddba4ad747c2db47a65b918acf8e313dad2795cc22f1514705c2ff386625e6fbda03fe5ebecc1e443c6a49ccb4a887b98f744ea67a7e329efb1887855
data/bin/dtcterm ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'dtcterm'
4
+ Dtcterm.main(ARGV)
data/lib/dtcterm.rb ADDED
@@ -0,0 +1,168 @@
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
7
+
8
+ require 'optparse'
9
+ require 'nokogiri'
10
+ require 'open-uri'
11
+ require 'rubygems'
12
+
13
+ class << self
14
+
15
+ $using_color = true
16
+ $dtc_choice = ""
17
+
18
+ BASE_QUOTE_LINK = "http://danstonchat.com/id.html"
19
+
20
+ DTC_FLOP_URL = "http://danstonchat.com/flop50.html"
21
+ DTC_LATEST_URL = "http://danstonchat.com/latest.html"
22
+ DTC_RANDOM_URL = "http://danstonchat.com/random.html"
23
+ DTC_RANDOM_POSITIVE_URL = "http://danstonchat.com/random0.html"
24
+ DTC_TOP_URL = "http://danstonchat.com/top50.html"
25
+
26
+ def version
27
+ '0.1.0'
28
+ end
29
+
30
+
31
+
32
+ class Quote
33
+ attr_accessor :id
34
+ attr_accessor :link
35
+ attr_accessor :quote
36
+
37
+ def initialize
38
+ @id = 0
39
+ @link = ""
40
+ @quote = Array.new { Array.new(2) }
41
+ end
42
+
43
+ def colorize(text)
44
+ "\e[32m#{text}\e[0m"
45
+ end
46
+
47
+ def display
48
+ print "Quote ##{id}\n"
49
+ print "Link: #{link}\n"
50
+ @quote.each do |item|
51
+ print "#{$using_color ? colorize(item[0]) : item[0]} #{item[1]}\n"
52
+ end
53
+ end
54
+ end
55
+
56
+
57
+
58
+ def get_page_html(url)
59
+ begin
60
+ Nokogiri::HTML(open(url))
61
+ rescue
62
+ puts "Can't reach the server."
63
+ exit 1
64
+ end
65
+ end
66
+
67
+ def get_quote_list(html)
68
+ quote_list = Array.new
69
+
70
+ html.css('div.item-listing').css('div.item').each do |item|
71
+ quote = Quote.new
72
+ quote.id = item["class"].gsub(/[^\d]/, '')
73
+ quote.link = BASE_QUOTE_LINK.gsub("id", quote.id)
74
+
75
+ quote_item = item.children()[0].children()[0].children()[0]
76
+ while quote_item
77
+ # A quote is always like : <span class="decoration">text</span> text
78
+ # The "decoration" class means the username
79
+
80
+ # Getting the span text.
81
+ first = quote_item.text.sub(/\<br>/, '').strip()
82
+
83
+ # Getting the rest of text
84
+ quote_item = quote_item.next()
85
+ if (quote_item != nil)
86
+ second = quote_item.text.sub(/'\<br>/, '').strip()
87
+ quote_item = quote_item.next()
88
+ end
89
+
90
+ quote.quote << [first, second]
91
+ end
92
+
93
+ quote_list << quote
94
+ end
95
+
96
+ quote_list
97
+ end
98
+
99
+ def display_quote_list(quote_list)
100
+ quote_list.each do |quote|
101
+ quote.display
102
+ print "\n\n"
103
+ end
104
+ end
105
+
106
+
107
+
108
+ def main(options)
109
+ def self.quote_already_set
110
+ unless $dtc_choice.empty?
111
+ puts("Un choix de quote a déjà été fait.")
112
+ exit 1
113
+ end
114
+ end
115
+
116
+ OptionParser.new do |opts|
117
+
118
+ opts.banner = "Usage: dtcterm one_quote_option [color_option]"
119
+
120
+ opts.version = VERSION
121
+
122
+ opts.on("-c", "--no-color", "Enlève la couleur.") do
123
+ $using_color = false
124
+ end
125
+
126
+ opts.on("-f", "--flop", "Rubrique `flop50`.") do
127
+ quote_already_set
128
+ $dtc_choice = DTC_FLOP_URL
129
+ end
130
+
131
+ opts.on("-l", "--latest", "Rubrique `Derniers ajours`.") do
132
+ quote_already_set
133
+ $dtc_choice = DTC_LATEST_URL
134
+ end
135
+
136
+ opts.on("-r", "--random", "Rubrique `random`.") do
137
+ quote_already_set
138
+ $dtc_choice = DTC_RANDOM_URL
139
+ end
140
+
141
+ opts.on("-R", "--random0", "Rubrique `random > 0`") do
142
+ quote_already_set
143
+ $dtc_choice = DTC_RANDOM_POSITIVE_URL
144
+ end
145
+
146
+ opts.on("-t", "--top", "Rubrique `top50`") do
147
+ quote_already_set
148
+ $dtc_choice = DTC_TOP_URL
149
+ end
150
+
151
+ end.parse!(options)
152
+
153
+ if $dtc_choice.empty?
154
+ puts("Il faut choisir une rubrique.")
155
+ exit 1
156
+ end
157
+
158
+ unless options.empty?
159
+ puts("Liste d'argument incorrects.")
160
+ exit 1
161
+ end
162
+
163
+ html = get_page_html(DTC_RANDOM_POSITIVE_URL)
164
+ quote_list = get_quote_list(html)
165
+ display_quote_list(quote_list)
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,89 @@
1
+ require 'test/unit'
2
+ require 'dtcterm'
3
+ require 'nokogiri'
4
+
5
+ class DtctermTest < Test::Unit::TestCase
6
+
7
+ def test_version
8
+ assert(Dtcterm.version =~ /\d.\d.\d/)
9
+ end
10
+
11
+ def test_get_quote_list
12
+ html = <<-eos
13
+ <div class="item-listing">
14
+ <h1>Random things</h1>
15
+ <div class="item item1234">
16
+ <p class="item-content">
17
+ <a href="useless_link">
18
+ <span class="decoration">Person1: </span>Hello there !
19
+ <span class="decoration">Person2: </span>Hello you !
20
+ <span class="decoration">Person1: </span>How are you ?
21
+ <span class="decoration">Person2: </span>It's a flesh wound
22
+ <span class="decoration">Person1: </span>DUCK DUCK !
23
+ <span class="decoration">Person2: </span>Nice holy grenade !
24
+ <span class="decoration">Person1: </span>Oh.
25
+ </a>
26
+ </p>
27
+ </div>
28
+ <div class="item item4567">
29
+ <p class="item-content">
30
+ <a href="useless_link">
31
+ <span class="decoration">mew > </span>Groumpf
32
+ <span class="decoration">two > </span>Some test
33
+ </a>
34
+ </p>
35
+ </div>
36
+ </div>
37
+ eos
38
+
39
+ html = html.gsub(/\n/, '').strip.gsub(/[[:space:]]{2,}/, '')
40
+ quote_list = Dtcterm::get_quote_list(Nokogiri::XML(html))
41
+
42
+ quote = quote_list[0]
43
+ assert_equal quote.id, "1234"
44
+ assert_equal quote.link, "http://danstonchat.com/1234.html"
45
+
46
+ q = quote.quote[0]
47
+ assert_equal q[0], "Person1:"
48
+ assert_equal q[1], "Hello there !"
49
+
50
+ q = quote.quote[1]
51
+ assert_equal q[0], "Person2:"
52
+ assert_equal q[1], "Hello you !"
53
+
54
+ q = quote.quote[2]
55
+ assert_equal q[0], "Person1:"
56
+ assert_equal q[1], "How are you ?"
57
+
58
+ q = quote.quote[3]
59
+ assert_equal q[0], "Person2:"
60
+ assert_equal q[1], "It's a flesh wound"
61
+
62
+ q = quote.quote[4]
63
+ assert_equal q[0], "Person1:"
64
+ assert_equal q[1], "DUCK DUCK !"
65
+
66
+ q = quote.quote[5]
67
+ assert_equal q[0], "Person2:"
68
+ assert_equal q[1], "Nice holy grenade !"
69
+
70
+ q = quote.quote[6]
71
+ assert_equal q[0], "Person1:"
72
+ assert_equal q[1], "Oh."
73
+
74
+
75
+
76
+ quote = quote_list[1]
77
+ assert_equal quote.id, "4567"
78
+ assert_equal quote.link, "http://danstonchat.com/4567.html"
79
+
80
+ q = quote.quote[0]
81
+ assert_equal q[0], "mew >"
82
+ assert_equal q[1], "Groumpf"
83
+
84
+ q = quote.quote[1]
85
+ assert_equal q[0], "two >"
86
+ assert_equal q[1], "Some test"
87
+ end
88
+
89
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dtcterm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Loïc Runarvot
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.5.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.5.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.5.6
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.5.6
41
+ description: |-
42
+ DansTonChar viewer in terminal application.\
43
+ Can display different categories from the website. \
44
+ http://www.danstonchat.com
45
+ email: loic.runarvot@gmail.com
46
+ executables:
47
+ - dtcterm
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - lib/dtcterm.rb
52
+ - test/test_dtcterm.rb
53
+ - bin/dtcterm
54
+ homepage: https://github.com/Aaylor/DansTonChatTerm
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.0.14
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: DansTonChat viewer in terminal application
78
+ test_files:
79
+ - test/test_dtcterm.rb