wx_ext 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b41727e13812d26ea3ef4949f2576c899a4de8a3
4
- data.tar.gz: 11b3b1f0ddd636b43ab554c81848e1ec356c201f
3
+ metadata.gz: 53373828789ebd3ab2f05921de302c7e3cec984e
4
+ data.tar.gz: 5ffb60baac576500d9e1e7cea5404903d27e2b0d
5
5
  SHA512:
6
- metadata.gz: e2620ed495169871657c6577564b3923fc1c62c9e38ea07a908f57a45195d5354b6d22b92bc66539704e756efa0278a47d0defcaaddddc17b7efaf0e3c37bd52
7
- data.tar.gz: 7bb3b9d0c3ca7c18c2013c5a37f65d7aaeb0d585b6464d6d85f46339db43ce3cfd7986b795880e26f01cbef0d0874877a5948dce5b44a02fe05684a4d64818f4
6
+ metadata.gz: f7ba4f709afccc8075780147c451b763c1f388403b3beb23caa7781d459ab4ee39f62baebcd378e289a91ddd2db2327c1a8e5cb1a8bdc39b87dc7e5176b4e7af
7
+ data.tar.gz: e46026e8ded4ae222b94ba7aa9cc269ad24f463c7700f3d07f0340e75f673348770a612fbd24bfe832e5472e88ca919aa37aa2be75df5b2660bb9e99397f780c
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ *.gem
data/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
- gem 'wx_ext', :git => 'https://github.com/FlowerWrong/wx_ext.git'
10
+ gem 'wx_ext', '~> 0.0.2'
11
11
  ```
12
12
 
13
13
  And then execute:
@@ -16,7 +16,12 @@ And then execute:
16
16
 
17
17
  ## Usage
18
18
 
19
- TODO: 用户管理接口
19
+ 使用请看spec/wx_ext/
20
+
21
+ ## 测试
22
+
23
+ 1. 修改 spec/wx_ext/weixin_spec.rb 文件中的账户密码
24
+ 2. rspec spec/wx_ext/weixin_spec.rb
20
25
 
21
26
  ## Contributing
22
27
 
@@ -1,3 +1,3 @@
1
1
  module WxExt
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/wx_ext.rb CHANGED
@@ -1,11 +1,14 @@
1
- require "wx_ext/version"
1
+ # encoding: UTF-8
2
+ require 'wx_ext/version'
2
3
  require 'digest'
3
4
  require 'rest_client'
4
5
  require 'json'
6
+ require 'nokogiri'
5
7
 
6
8
  module WxExt
7
9
  class WeiXin
8
- attr_accessor :account, :password, :home_url, :token, :user_name, :ticket_id, :ticket, :cookies, :operation_seq
10
+ attr_accessor :account, :password, :home_url, :token, :user_name, \
11
+ :ticket_id, :ticket, :cookies, :operation_seq
9
12
  def initialize(account, password)
10
13
  @account = account
11
14
  @password = password
@@ -67,21 +70,26 @@ module WxExt
67
70
  end
68
71
 
69
72
  # 上传图片素材到素材中心
70
- def upload_file(file, file_name, folder = "/cgi-bin/uploads")
73
+ def upload_file(file, file_name, folder = '/cgi-bin/uploads')
71
74
  upload_url = "https://mp.weixin.qq.com/cgi-bin/filetransfer?action=upload_material&f=json&writetype=doublewrite&groupid=1&ticket_id=#{@ticket_id}&ticket=#{@ticket}&token=#{@token}&lang=zh_CN"
72
- response = RestClient.post upload_url, :file => file, \
73
- :Filename => file_name, \
74
- :folder => folder
75
- res_hash = JSON.parse response.to_s
75
+ response = RestClient.post upload_url, file: file, \
76
+ Filename: file_name, \
77
+ folder: folder
78
+ JSON.parse response.to_s
76
79
  end
77
80
 
78
81
  # 发送单条图文消息到素材中心
79
82
  def upload_single_msg(single_msg_params)
80
- post_single_msg_uri = "cgi-bin/operate_appmsg?t=ajax-response&sub=create&type=10&token=#{@token}&lang=zh_CN"
83
+ post_single_msg_uri = 'cgi-bin/operate_appmsg'\
84
+ '?t=ajax-response&sub=create&type=10&token'\
85
+ "=#{@token}&lang=zh_CN"
81
86
  post_single_msg_headers = {
82
- referer: "https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit&action=edit&type=10&isMul=0&isNew=1&lang=zh_CN&token=#{@token}"
87
+ referer: 'https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit'\
88
+ "&action=edit&type=10&isMul=0&isNew=1&lang=zh_CN&token=#{@token}"
83
89
  }
84
- post_single_msg_resource = RestClient::Resource.new(@home_url, headers: post_single_msg_headers, cookies: @cookies)
90
+ post_single_msg_resource = RestClient::Resource.new(@home_url,
91
+ headers: post_single_msg_headers,
92
+ cookies: @cookies)
85
93
  post_single_msg_res = post_single_msg_resource[post_single_msg_uri].post single_msg_params
86
94
  # {"ret":"0", "msg":"OK"}
87
95
  res_hash = JSON.parse post_single_msg_res.to_s
@@ -118,6 +126,102 @@ module WxExt
118
126
  app_msg_url = "https://mp.weixin.qq.com/cgi-bin/appmsg?type=10&action=list&begin=#{msg_begin}&count=#{msg_count}&f=json&token=#{@token}&lang=zh_CN&token=#{@token}&lang=zh_CN&f=json&ajax=1&random=#{rand}"
119
127
  msg_json = RestClient.get app_msg_url, {cookies: @cookies}
120
128
  app_msg_hash = JSON.parse msg_json.to_s
129
+ end
130
+
131
+ # 轮训新消息条数
132
+ def get_new_msg_num(last_msg_id)
133
+ uri = "cgi-bin/getnewmsgnum?f=json&t=ajax-getmsgnum&lastmsgid=#{last_msg_id}&token=#{@token}&lang=zh_CN"
134
+ post_params = {
135
+ ajax: 1,
136
+ f: 'json',
137
+ lang: 'zh_CN',
138
+ random: rand,
139
+ token: @token
140
+ }
141
+ post_headers = {
142
+ referer: "https://mp.weixin.qq.com/cgi-bin/message?t=message/list&count=20&day=7&token=#{@token}&lang=zh_CN"
143
+ }
144
+ resource = RestClient::Resource.new(@home_url, :headers => post_headers, cookies: @cookies)
145
+ res = resource[uri].post post_params
146
+ res_json = JSON.parse res.to_s
147
+ end
148
+
149
+ # 获取联系人信息
150
+ def get_contact_info(fakeid)
151
+ uri = "cgi-bin/getcontactinfo?t=ajax-getcontactinfo&lang=zh_CN&fakeid=#{fakeid}"
152
+ post_params = {
153
+ ajax: 1,
154
+ f: 'json',
155
+ lang: 'zh_CN',
156
+ random: rand,
157
+ token: @token
158
+ }
159
+ post_headers = {
160
+ referer: "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=10&pageidx=0&type=0&token=#{@token}&lang=zh_CN"
161
+ }
162
+ resource = RestClient::Resource.new(@home_url, :headers => post_headers, cookies: @cookies)
163
+ res = resource[uri].post post_params
164
+ res_json = JSON.parse res.to_s
165
+ end
166
+
167
+ # 获取国家列表
168
+ def get_country_list
169
+ url = "https://mp.weixin.qq.com/cgi-bin/getregions?t=setting/ajax-getregions&id=0&token=#{@token}&lang=zh_CN&token=#{@token}&lang=zh_CN&f=json&ajax=1&random=#{rand}"
170
+ resource = RestClient::Resource.new(url, cookies: @cookies)
171
+ res = resource.get
172
+ res_json = JSON.parse res.to_s
173
+ end
174
+
175
+ # 获取每日可推送消息数
176
+ def get_day_msg_count
177
+ url = "https://mp.weixin.qq.com/cgi-bin/masssendpage?t=mass/send&token=#{@token}&lang=zh_CN"
178
+ res = RestClient.get(url, {cookies: @cookies})
179
+ day_msg_count = 0
180
+ msg_count_reg = /.*mass_send_left\s*:\s*can_verify_apply\s*\?\s*\'(\d*)\'\*/
181
+ if msg_count_reg =~ res.to_s
182
+ day_msg_count = $1
183
+ end
184
+ day_msg_count.to_i
185
+ end
186
+
187
+ # todo list
188
+ # 获取 last_msg_id 和 fake_id
189
+ def get_ids # 未完成
190
+ url = "https://mp.weixin.qq.com/cgi-bin/message?t=message/list&count=20&day=7&token=#{@token}&lang=zh_CN"
191
+ resource = RestClient::Resource.new(url, cookies: @cookies)
192
+ res = resource.get
193
+ doc = Nokogiri::HTML(res.to_s)
194
+ doc.css('ul.message_list li').each do |li|
195
+ puts li.content
196
+ end
197
+ end
198
+
199
+ # https://mp.weixin.qq.com/cgi-bin/message?t=message/list&token=1664040225&count=20&day=7
200
+ # https://mp.weixin.qq.com/cgi-bin/message?t=message/list&count=20&day=7&token=1664040225&lang=zh_CN
201
+
202
+ # https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&f=json&token=1664040225&lang=zh_CN
203
+ def quick_reply
204
+ end
205
+
206
+ # https://mp.weixin.qq.com/cgi-bin/setstarmessage?t=ajax-setstarmessage&token=1664040225&lang=zh_CN
207
+ def collect_msg
208
+ end
209
+
210
+ private
211
+ def set_cookie(page, k, v)
212
+ case Capybara.current_session.driver
213
+ when Capybara::Poltergeist::Driver
214
+ page.driver.set_cookie(k,v)
215
+ when Capybara::RackTest::Driver
216
+ headers = {}
217
+ Rack::Utils.set_cookie_header!(headers,k,v)
218
+ cookie_string = headers['Set-Cookie']
219
+ Capybara.current_session.driver.browser.set_cookie(cookie_string)
220
+ when Capybara::Selenium::Driver
221
+ page.driver.browser.manage.add_cookie(:name=>k, :value=>v)
222
+ else
223
+ raise "no cookie-setter implemented for driver #{Capybara.current_session.driver.class.name}"
224
+ end
121
225
  end
122
226
  end
123
227
  end
@@ -1,16 +1,54 @@
1
+ # encoding: UTF-8
1
2
  require 'spec_helper'
2
3
 
3
-
4
4
  describe WxExt::WeiXin do
5
5
  before(:all) do
6
- @weixin = WxExt::WeiXin.new "login_account", "login_pass"
6
+ @weixin = WxExt::WeiXin.new 'flowerwrong@hotmail.com', '1*flower@wrong*1'
7
7
  end
8
8
 
9
- it "init method should init all params" do
9
+ it 'init method should init all params' do
10
10
  flag = @weixin.init
11
- token = @weixin.token
12
- puts "token = " + token
13
- expect(token).to match(/\d+/)
11
+ if flag
12
+ token = @weixin.token
13
+ puts "token = #{token}"
14
+ expect(token).to match(/\d+/)
15
+ end
16
+ end
17
+
18
+ it 'should get day msg count' do
19
+ flag = @weixin.init
20
+ if flag
21
+ day_msg_count = @weixin.get_day_msg_count
22
+ expect(day_msg_count.to_s).to match(/\d*/)
23
+ end
24
+ end
25
+
26
+ # 未完待续
27
+ it 'should get fakeids and msg ids' do
28
+ flag = @weixin.init
29
+ res = @weixin.get_ids if flag
30
+ puts res.to_s
31
+ end
32
+
33
+ it "should get new msg num" do
34
+ flag = @weixin.init
35
+ res_hash = @weixin.get_new_msg_num('201004139')
36
+ puts res_hash
37
+ expect(res_hash['ret'].to_s).to eql('0')
38
+ end
39
+
40
+ it "should get contact info" do
41
+ flag = @weixin.init
42
+ res_hash = @weixin.get_contact_info('204060720')
43
+ puts res_hash
44
+ expect(res_hash['base_resp']['ret'].to_s).to eql('0')
45
+ end
46
+
47
+ it "should return a country list" do
48
+ flag = @weixin.init
49
+ res_hash = @weixin.get_country_list
50
+ puts res_hash
51
+ expect(res_hash["num"].to_s).to match(/\d+/)
14
52
  end
15
53
 
16
54
  it "upload_file method should return a right hash" do
@@ -128,5 +166,4 @@ describe WxExt::WeiXin do
128
166
  puts msg_hash["app_msg_info"]["item"][0]["app_id"]
129
167
  expect(msg_hash["base_resp"]["ret"].to_s).to eql("0")
130
168
  end
131
-
132
- end
169
+ end
data/wx_ext.gemspec CHANGED
@@ -4,23 +4,28 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'wx_ext/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "wx_ext"
7
+ spec.name = 'wx_ext'
8
8
  spec.version = WxExt::VERSION
9
- spec.authors = ["flowerwrong"]
10
- spec.email = ["sysuyangkang@gmail.com"]
9
+ spec.authors = ['flowerwrong']
10
+ spec.email = ['sysuyangkang@gmail.com']
11
11
  spec.summary = %q{a gem to hack mp.weixin.qq.com}
12
12
  spec.description = %q{a gem to hack mp.weixin.qq.com}
13
- spec.homepage = "http://thecampus.cc"
14
- spec.license = "MIT"
13
+ spec.homepage = 'http://thecampus.cc'
14
+ spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.7"
22
- spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
23
 
24
- spec.add_development_dependency "rspec"
25
- spec.add_development_dependency "rest_client"
24
+ spec.add_development_dependency 'rspec'
25
+ spec.add_dependency 'rest_client'
26
+ spec.add_dependency 'nokogiri'
27
+ # spec.add_dependency 'mechanize'
28
+ # spec.add_dependency 'phantomjs', '~> 1.9.7.1'
29
+ # spec.add_dependency 'capybara'
30
+ # spec.add_dependency 'poltergeist'
26
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wx_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - flowerwrong
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-13 00:00:00.000000000 Z
11
+ date: 2014-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -59,7 +59,21 @@ dependencies:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
- type: :development
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
@@ -103,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
117
  version: '0'
104
118
  requirements: []
105
119
  rubyforge_project:
106
- rubygems_version: 2.4.2
120
+ rubygems_version: 2.4.4
107
121
  signing_key:
108
122
  specification_version: 4
109
123
  summary: a gem to hack mp.weixin.qq.com