pttcrawler 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 73fd83df21fe327e1d3f7a8900d5b68b67acd7f5
4
+ data.tar.gz: 9c2b2d37cd6e2353dca3a2698b9a954aa660ff48
5
+ SHA512:
6
+ metadata.gz: 6e256254e66b1d950ff386b27906a57e3aebd12ca385f3511e2f0f20e4d68e9e301d4213f835d00ffef076d55456171f69c1d6d380f4deb132851846bec5cf0d
7
+ data.tar.gz: 31b2799ab097414c7836461ddb0a7ae1f1d2e3e53f0ad156cff765c6bf3b8341ed17c91d2fbae8198b93d9e941a616f2077e36c50f8cb757f068bdddb6a6613c
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pttcrawler.gemspec
4
+ gemspec
5
+ gem 'test-unit'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 chang-ning
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Pttcrawler
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/pttcrawler`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'pttcrawler'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install pttcrawler
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pttcrawler. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pttcrawler"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/pttcrawler ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'pttcrawler'
5
+
6
+ if ARGV.size != 3 then
7
+ print("bin/pttcrawler [usr] [pwd] [board]\n")
8
+ exit(0)
9
+ end
10
+
11
+ USR = ARGV[0]
12
+ PWD = ARGV[1]
13
+ TARGET_BOARD = ARGV[2]
14
+
15
+ crawler = Pttcrawler::Crawler.new(log_level:Pttcrawler::PTT_DEBUG)
16
+ crawler.login USR, PWD do
17
+ crawler.get_today_article_list(TARGET_BOARD)
18
+ end
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module Pttcrawler
2
+ VERSION = "0.1.1"
3
+ end
data/lib/pttcrawler.rb ADDED
@@ -0,0 +1,169 @@
1
+ require "pttcrawler/version"
2
+ require "net/telnet"
3
+ require "logger"
4
+ require "date"
5
+
6
+ module Pttcrawler
7
+
8
+ PTT_HOST = 'ptt.cc'
9
+
10
+ # some keyword to detect recv server data finish.
11
+ ACCOUNT_MSG = 'guest.+new(?>[^:]+):(?>\s*)'
12
+ PASSWORD_MSG = '\xB1\x4B\xBD\x58:'
13
+ PRESSKEY_CONTINUE = '\xBD\xD0\xAB\xF6\xA5\xF4\xB7\x4E\xC1\xE4\xC4\x7E\xC4\xF2'
14
+ ANSI_DISP_ATTR = '\e\[(?>(?>(?>\d+;)*\d+)?)'
15
+ ANSI_CURSOR = '\e\[(?>(?>\d+;\d+)?)H'
16
+ CALLER = '\[\xA9\x49\xA5\x73\xBE\xB9\]'
17
+ ARTICLES_LIST = '\xb6\x69\xaa\x4f\xb5\x65\xad\xb1'
18
+
19
+ # debug level
20
+ PTT_DEBUG = Logger::DEBUG
21
+ PTT_INFO = Logger::INFO
22
+ PTT_WARN = Logger::WARN
23
+ PTT_ERROR = Logger::ERROR
24
+ PTT_FATAL = Logger::FATAL
25
+
26
+ class Crawler
27
+ # crawling ptt article
28
+ #
29
+ # Example:
30
+ # >> c = Crawler.new()
31
+ #
32
+ # Arguments
33
+ # usr: ptt account
34
+ # pwd: ptt password
35
+
36
+ def initialize(port:23, timeout:3, waittime:1, log_level:PTT_DEBUG)
37
+ @ptt = Net::Telnet.new('Host' => PTT_HOST,
38
+ 'Port' => port,
39
+ 'Timeout' => timeout,
40
+ 'Waittime' => waittime)
41
+ @log_level = log_level
42
+ @ptt_logger = Logger.new(STDOUT)
43
+ @ptt_logger.formatter = proc do |severity, datetime, progname, msg|
44
+ "#{msg}"
45
+ end
46
+ end
47
+
48
+ public
49
+
50
+ def login(usr, pwd)
51
+ # step1: connect to server
52
+ @ptt.waitfor(/#{ACCOUNT_MSG}#{ANSI_DISP_ATTR}.*(?>\b+)$/n) do |s|
53
+ @ptt_logger.debug(s)
54
+ end
55
+ # step2: send user account
56
+ @ptt.cmd('String' => " " + usr,
57
+ 'Match' => /#{PASSWORD_MSG}/n) do |s|
58
+ @ptt_logger.debug(s)
59
+ end
60
+ # step3: send password
61
+ @ptt.cmd('String' => pwd,
62
+ 'Match' => /#{PRESSKEY_CONTINUE}/n) do |s|
63
+ @ptt_logger.debug(s)
64
+ end
65
+ # step4: home page
66
+ # FIXME: currently, no check double login
67
+ @ptt.print("\n")
68
+ @ptt.waitfor(/#{CALLER}/n) do |s|
69
+ @ptt_logger.debug(s)
70
+ end
71
+ yield
72
+ end
73
+
74
+ def get_today_article_list(board)
75
+ line = checkin(board)
76
+ article_list = []
77
+ begin
78
+ loop do
79
+ line = gusb_ansi_by_space(line)
80
+ info = get_authors_and_articles(line)
81
+ if false == info.any? {|num, date, author| Date.today === Date.parse(date)}
82
+ raise StopIteration
83
+ end
84
+ info.sort! { |x,y| y[0].to_i <=> x[0].to_i }
85
+ info.each do |num, date, author|
86
+ if Date.today === Date.parse(date)
87
+ article_list.push([num, date, author])
88
+ end
89
+ end
90
+ line = article_list_page_up
91
+ end
92
+ ensure
93
+ @ptt_logger.debug article_list
94
+ end
95
+ end
96
+
97
+ private
98
+
99
+ def checkin(board)
100
+ # step1: search board
101
+ @ptt.print("s")
102
+ @ptt.waitfor(/\):(?>\s*)#{ANSI_DISP_ATTR}.*#{ANSI_CURSOR}/n) do |s|
103
+ @ptt_logger.debug(s)
104
+ end
105
+ # step2: get entrance board string
106
+ line = @ptt.cmd('String' => board,
107
+ 'Match' => /(?>#{PRESSKEY_CONTINUE}|#{ARTICLES_LIST})/n) do |s|
108
+ @ptt_logger.debug(s)
109
+ end
110
+ # check get articles list
111
+ begin
112
+ if /#{PRESSKEY_CONTINUE}/n =~ line
113
+ @ptt.print('\n')
114
+ line = @ptt.waitfor(/#{ARTICLES_LIST}/n) do |s|
115
+ @ptt_logger.debug(s)
116
+ end
117
+ end
118
+ rescue Net::ReadTimeout
119
+ retry
120
+ end
121
+ return line
122
+ end
123
+
124
+ def gusb_ansi_by_space(s)
125
+ raise ArgumentError unless s.kind_of? String
126
+ s.gsub!(/#{ANSI_DISP_ATTR}m|#{ANSI_CURSOR}|\e\[K/) do |m|
127
+ if m[m.size - 1] == 'K'
128
+ "\n"
129
+ else
130
+ " "
131
+ end
132
+ end
133
+ end
134
+
135
+ def get_authors_and_articles(s)
136
+ raise ArgumentError unless s.kind_of? String
137
+ info_list = []
138
+ s.scan(/\s(\d+)(?>\s+)
139
+ (?>(?:[~+mMsS!](?=\s))?)
140
+ (?>(?>\s*(?>\xC3\x7A|XX|X\d|\d+)(?=\s))?)
141
+ (?>\s*)(\d+\/\d+)(?>\s+)
142
+ (?>\s*)(?!(?>\d+\s))(\w{2,})\s+
143
+ /nx) do |num, date, author|
144
+ info_list.push([num, date, author])
145
+ end
146
+ info_list
147
+ end
148
+
149
+ def article_list_page_up
150
+ @ptt.print('P')
151
+ @ptt.waitfor(/#{ANSI_CURSOR}/n) do |s|
152
+ @ptt_logger.debug(s)
153
+ end
154
+ @ptt.print('b')
155
+ @ptt.waitfor(/#{PRESSKEY_CONTINUE}/n) do |s|
156
+ @ptt_logger.debug(s)
157
+ end
158
+ begin
159
+ @ptt.print('r')
160
+ @ptt.waitfor(/#{ARTICLES_LIST}/n) do |s|
161
+ @ptt_logger.debug(s)
162
+ end
163
+ rescue Net::ReadTimeout
164
+ retry
165
+ end
166
+ end
167
+
168
+ end
169
+ end
Binary file
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pttcrawler/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pttcrawler"
8
+ spec.version = Pttcrawler::VERSION
9
+ spec.authors = ["chang-ning"]
10
+ spec.email = ["spiderpower02@gmail.com"]
11
+
12
+ spec.summary = "Ruby ptt crawler"
13
+ spec.description = "Ruby ptt crawling tool"
14
+ spec.homepage = "https://github.com/crazyguitar/ruby_pttcrawler"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pttcrawler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - chang-ning
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Ruby ptt crawling tool
42
+ email:
43
+ - spiderpower02@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - CODE_OF_CONDUCT.md
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/pttcrawler
57
+ - bin/setup
58
+ - lib/pttcrawler.rb
59
+ - lib/pttcrawler/version.rb
60
+ - pttcrawler-0.1.0.gem
61
+ - pttcrawler.gemspec
62
+ homepage: https://github.com/crazyguitar/ruby_pttcrawler
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.4.6
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Ruby ptt crawler
86
+ test_files: []