twoch 0.0.1

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.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in twoch.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ twoch (0.0.1)
5
+ mechanize
6
+ thor
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ diff-lcs (1.1.3)
12
+ domain_name (0.5.6)
13
+ unf (~> 0.0.3)
14
+ mechanize (2.5.1)
15
+ domain_name (~> 0.5, >= 0.5.1)
16
+ mime-types (~> 1.17, >= 1.17.2)
17
+ net-http-digest_auth (~> 1.1, >= 1.1.1)
18
+ net-http-persistent (~> 2.5, >= 2.5.2)
19
+ nokogiri (~> 1.4)
20
+ ntlm-http (~> 0.1, >= 0.1.1)
21
+ webrobots (~> 0.0, >= 0.0.9)
22
+ mime-types (1.19)
23
+ net-http-digest_auth (1.2.1)
24
+ net-http-persistent (2.8)
25
+ nokogiri (1.5.5)
26
+ ntlm-http (0.1.1)
27
+ rspec (2.12.0)
28
+ rspec-core (~> 2.12.0)
29
+ rspec-expectations (~> 2.12.0)
30
+ rspec-mocks (~> 2.12.0)
31
+ rspec-core (2.12.1)
32
+ rspec-expectations (2.12.0)
33
+ diff-lcs (~> 1.1.3)
34
+ rspec-mocks (2.12.0)
35
+ thor (0.16.0)
36
+ unf (0.0.5)
37
+ unf_ext
38
+ unf_ext (0.0.5)
39
+ webrobots (0.0.13)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ rspec
46
+ twoch!
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Kazuhiro Yamada
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Twoch
2
+
3
+ The 2channel client. 2channel is a Japanese textboard(http://www.2ch.net/)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'twoch'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install twoch
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/twoch ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ require 'twoch'
3
+ require 'thor'
4
+
5
+ result_text = Twoch::CLI.start
6
+
7
+ #tmp_file_name = "/tmp/twoch.temp" + Time.now.to_f.ceil.to_s
8
+ #File.open("#{tmp_file_name}", "w") do |file|
9
+ # file.write(result_text)
10
+ #end
11
+ #system("less -R #{tmp_file_name}")
12
+ #system("rm -rf #{tmp_file_name}")
data/lib/twoch.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "twoch/channel"
2
+ require "twoch/version"
3
+ require "twoch/cli"
4
+
5
+ module Twoch
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,134 @@
1
+ require 'thor'
2
+ require 'mechanize'
3
+ require 'pp'
4
+ module Twoch; module Channel
5
+ class << self
6
+
7
+ def search(words) #:nodoc:
8
+ @agent.get(make_uri_from_words(words))
9
+ @words = words
10
+ result = ""
11
+ get_search_assistances.each do |assistance|
12
+ result << set_color(assistance, :green) + "\n"
13
+ end
14
+
15
+ meanings = get_meanings
16
+ get_titles.each_with_index do |title, index|
17
+ result << set_color(title, :yellow) + "\n"
18
+ result << set_color(meanings[index]) + "\n"
19
+ end
20
+ result
21
+ end
22
+
23
+ def set_color(str, color)
24
+ Thor::Shell::Color.new.set_color(str, color)
25
+ end
26
+
27
+
28
+ def search_board(words)
29
+ url = "http://www2.2ch.net/bbsmenu.html"
30
+ a = Mechanize.new
31
+ a.get(url)
32
+ boards = []
33
+ a.page.search('a').map do |e|
34
+ if words == [] || match_search_condition?(e.inner_text, words)
35
+ boards << {:title => e.inner_text, :url => e[:href]}
36
+ end
37
+ end
38
+
39
+ boards.each_with_index do |board, i|
40
+ puts "#{i} : #{board[:title]}(#{board[:url]})"
41
+ end
42
+
43
+ print "input board no >"
44
+ no = STDIN.gets.to_i
45
+ show_thread_list(boards[no][:url])
46
+ end
47
+
48
+ def show_thread_list(board_url)
49
+ thread_list_url = board_url + "subback.html"
50
+ a = Mechanize.new
51
+ a.get(thread_list_url)
52
+ threads = []
53
+ a.page.search('a').map do |e|
54
+ /\d+:\s+(.*)$/ =~ e.inner_text
55
+ title = $1
56
+ pp e
57
+ threads << {:title => title, :url => e[:href]}
58
+ end
59
+
60
+ threads.each_with_index do |thread, i|
61
+ puts "#{i} : #{thread[:title]}(#{thread[:url]})"
62
+ end
63
+
64
+ print "input thread no >"
65
+ no = STDIN.gets.to_i
66
+ thread_url = make_thread_url(board_url, threads[no][:url])
67
+ show_thread(thread_url)
68
+ end
69
+
70
+ def make_thread_url(board_url, thread_id)
71
+ m = /http:\/\/.*?\//.match board_url
72
+ board_url.dup.insert(m[0].length, "bbs/read.cgi/") + thread_id
73
+ end
74
+
75
+ def show_thread(thread_url)
76
+ puts thread_url
77
+ a = Mechanize.new
78
+ a.get(thread_url)
79
+ comments = []
80
+ a.page.search('dt').map do |dt|
81
+ dttext = dt.inner_text.gsub(/\n/, "")
82
+ puts set_color(dttext, :yellow)
83
+ dd = dt.next
84
+ puts dd.inner_text
85
+ puts
86
+ end
87
+
88
+ end
89
+
90
+ def match_search_condition?(text, search_words)
91
+ search_words.each do |word|
92
+ return true if text =~ /#{word}/
93
+ end
94
+ false
95
+ end
96
+
97
+ def make_uri_from_words(words) #:nodoc:
98
+ return BASE_URI + make_query_from_words(words) + CHAR_CODE
99
+ end
100
+
101
+ def make_query_from_words(words) #:nodoc:
102
+ return words.map { |param| URI.encode(param) }.join('+')
103
+ end
104
+
105
+ def get_search_assistances #:nodoc:
106
+ assistances = []
107
+ @agent.page.search('div.sas strong').map do |h|
108
+ assistances << h.inner_text.chomp("\t\t\t\t\t\t")
109
+ end
110
+ assistances
111
+ end
112
+
113
+ def get_titles #:nodoc:
114
+ return @agent.page.search('li span.midashi').map do |midashi|
115
+ inner_text = midashi.inner_text
116
+ end
117
+ end
118
+
119
+ def get_meanings #:nodoc:
120
+ meanings = Array.new
121
+ @agent.page.search('li div').each do |div|
122
+ lis = div.search('li')
123
+ if lis.size != 0
124
+ meanings << lis.inject('') { |text, li| text + " " + li.inner_text + "\n" }
125
+ else
126
+ meanings << " " + div.inner_text
127
+ end
128
+ end
129
+ return meanings
130
+ end
131
+
132
+ end
133
+ end
134
+ end
data/lib/twoch/cli.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'thor'
2
+ require 'twoch/channel'
3
+
4
+ module Twoch
5
+ class CLI < Thor
6
+
7
+ desc "bs [WORD1 WORD2]", "board search"
8
+ def bs(*word)
9
+ Channel.search_board(word)
10
+ end
11
+
12
+ desc "bl", "board list"
13
+ def bl
14
+ Channel.show_board_list(word)
15
+ end
16
+
17
+ desc "ts [WORD1 WORD2]", "thread search"
18
+ def ts(*words)
19
+ Channel.search_thread(words)
20
+ end
21
+
22
+ desc "tl <thread_list_url>", "thread list"
23
+ def tl(thread_list_url)
24
+ Channel.show_thread_list(thread_list_url)
25
+ end
26
+
27
+ desc "tb <thread_url>", "thread browse"
28
+ def tb(thread_url)
29
+ Channel.show_thread(thread_url)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Twoch
2
+ VERSION = "0.0.1"
3
+ end
data/tmp_file_name ADDED
@@ -0,0 +1,3 @@
1
+ aaaaa
2
+ bbbb
3
+ cccc
data/twoch.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'twoch/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "twoch"
8
+ gem.version = Twoch::VERSION
9
+ gem.authors = ["Kazuhiro Yamada"]
10
+ gem.email = ["kyamada@sonix.asia"]
11
+ gem.description = %q{2channel client}
12
+ gem.summary = %q{2channel client}
13
+ gem.homepage = "https://github.com/k-yamada/twoch"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_dependency 'thor'
20
+ gem.add_dependency 'mechanize'
21
+ gem.add_development_dependency 'rspec'
22
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twoch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kazuhiro Yamada
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: mechanize
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: 2channel client
63
+ email:
64
+ - kyamada@sonix.asia
65
+ executables:
66
+ - twoch
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - Gemfile
71
+ - Gemfile.lock
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - bin/twoch
76
+ - lib/twoch.rb
77
+ - lib/twoch/channel.rb
78
+ - lib/twoch/cli.rb
79
+ - lib/twoch/version.rb
80
+ - tmp_file_name
81
+ - twoch.gemspec
82
+ homepage: https://github.com/k-yamada/twoch
83
+ licenses: []
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.23
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: 2channel client
106
+ test_files: []