discuz_robot 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.rvmrc ADDED
@@ -0,0 +1,3 @@
1
+ export rvm_gemset_create_on_use_flag=1
2
+
3
+ rvm use 1.8.7@discuz_robot
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "jeweler"
5
+ end
6
+
7
+ group :test do
8
+ gem "rspec", "~>1.3.0"
9
+ gem "rspec-rails", "~>1.3.0"
10
+ end
11
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Guoliang Cao
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = discuz_robot
2
+
3
+ A Discuz robot created specifically for my other project (http://github.com/gcao/gocool).
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Guoliang Cao. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "discuz_robot"
8
+ gem.summary = %Q{Discuz robot}
9
+ gem.description = %Q{Created for http://www.github.com/gcao/gocool}
10
+ gem.email = "gcao99@gmail.com"
11
+ gem.homepage = "http://github.com/gcao/discuz_robot"
12
+ gem.authors = ["Guoliang Cao"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "discuz_robot #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/discuz_robot ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby -rubygems
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'discuz_robot'
5
+
6
+ user = %x(/usr/bin/env robot_user).strip
7
+ pass = %x(/usr/bin/env robot_pass).strip
8
+
9
+ robot = Discuz::Robot.new
10
+ robot.login user, pass
11
+ robot.post 18, 'TEST SUBJECT', 'TEST MESSAGE'
@@ -0,0 +1,56 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{discuz_robot}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Guoliang Cao"]
12
+ s.date = %q{2011-11-28}
13
+ s.default_executable = %q{discuz_robot}
14
+ s.description = %q{Created for http://www.github.com/gcao/gocool}
15
+ s.email = %q{gcao99@gmail.com}
16
+ s.executables = ["discuz_robot"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".rvmrc",
24
+ "Gemfile",
25
+ "LICENSE",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "bin/discuz_robot",
30
+ "discuz_robot.gemspec",
31
+ "lib/discuz_robot.rb",
32
+ "spec/discuz_robot_spec.rb",
33
+ "spec/spec.opts",
34
+ "spec/spec_helper.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/gcao/discuz_robot}
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.4.2}
39
+ s.summary = %q{Discuz robot}
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
46
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
47
+ else
48
+ s.add_dependency(%q<jeweler>, [">= 0"])
49
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<jeweler>, [">= 0"])
53
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
54
+ end
55
+ end
56
+
@@ -0,0 +1,67 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+
4
+ module Discuz
5
+ class Robot
6
+ def initialize base_url = "http://localhost/bbs/"
7
+ @base_url = base_url
8
+ @agent = CurlAgent.new
9
+ end
10
+
11
+ def login username, password
12
+ form_url = @base_url + "logging.php?action=login"
13
+ form_resp = @agent.get form_url, :save_cookie => true
14
+ form_doc = Nokogiri::HTML(form_resp)
15
+ formhash_input = form_doc.css("form input[name=formhash]")
16
+ if formhash_input.first
17
+ # Login
18
+ formhash = formhash_input.first['value']
19
+ login_url = @base_url + "logging.php?action=login&loginsubmit=yes&inajax=0"
20
+ @agent.post login_url,
21
+ "submit=true&formhash=#{formhash}&loginfield=username&username=#{username}&password=#{password}&questionid=0&answer=&cookietime=2592000",
22
+ :save_cookie => true
23
+ else
24
+ # Already logged in (this can happen if login is called multiple times in one session)
25
+ end
26
+ end
27
+
28
+ def post forum_id, subject, message, options = {}
29
+ form_url = @base_url + "post.php?action=newthread&fid=#{forum_id}"
30
+ form_resp = @agent.get form_url
31
+ # puts form_resp
32
+ form_doc = Nokogiri::HTML(form_resp)
33
+ formhash = form_doc.css("form input[name=formhash]").first['value']
34
+ posttime = form_doc.css("form input[name=posttime]").first['value']
35
+
36
+ post_url = @base_url + "post.php?action=newthread&fid=#{forum_id}&extra=&topicsubmit=yes"
37
+ @agent.post post_url, "action=newthread&fid=#{forum_id}&formhash=#{formhash}&posttime=#{posttime}&subject=#{subject}&message=#{message}"
38
+ end
39
+ end
40
+
41
+ class CurlAgent
42
+ def initialize
43
+ @cookie_file = "/tmp/c.txt"
44
+ `test #{@cookie_file} && rm #{@cookie_file}` # remove existing cookie file if any
45
+
46
+ user_agent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6"
47
+ @curl_cmd = %Q(curl --stderr /dev/null -A "#{user_agent}" -b #{@cookie_file})
48
+ end
49
+
50
+ def get url, options = {}
51
+ cmd = @curl_cmd
52
+ cmd += " -D #{@cookie_file}" if options[:save_cookie]
53
+ cmd += %Q( "#{url}")
54
+ puts cmd
55
+ %x(#{cmd})
56
+ end
57
+
58
+ def post url, data, options = {}
59
+ cmd = @curl_cmd
60
+ cmd += " -D #{@cookie_file}" if options[:save_cookie]
61
+ cmd += %Q( -d "#{data}")
62
+ cmd += %Q( "#{url}")
63
+ puts cmd
64
+ %x(#{cmd})
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "DiscuzRobot" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'discuz_robot'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: discuz_robot
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Guoliang Cao
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-11-28 00:00:00 -05:00
19
+ default_executable: discuz_robot
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ name: jeweler
24
+ type: :development
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ requirement: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ prerelease: false
37
+ name: rspec
38
+ type: :development
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 13
45
+ segments:
46
+ - 1
47
+ - 2
48
+ - 9
49
+ version: 1.2.9
50
+ requirement: *id002
51
+ description: Created for http://www.github.com/gcao/gocool
52
+ email: gcao99@gmail.com
53
+ executables:
54
+ - discuz_robot
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - LICENSE
59
+ - README.rdoc
60
+ files:
61
+ - .document
62
+ - .rvmrc
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.rdoc
66
+ - Rakefile
67
+ - VERSION
68
+ - bin/discuz_robot
69
+ - discuz_robot.gemspec
70
+ - lib/discuz_robot.rb
71
+ - spec/discuz_robot_spec.rb
72
+ - spec/spec.opts
73
+ - spec/spec_helper.rb
74
+ has_rdoc: true
75
+ homepage: http://github.com/gcao/discuz_robot
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options: []
80
+
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ requirements: []
102
+
103
+ rubyforge_project:
104
+ rubygems_version: 1.4.2
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Discuz robot
108
+ test_files: []
109
+