weixin_public 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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/weixin_public.rb +179 -0
- data/lib/weixin_public/version.rb +3 -0
- data/weixin_public.gemspec +23 -0
- metadata +82 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013 TODO: Write your name
|
|
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
|
+
# WeixinPublic
|
|
2
|
+
|
|
3
|
+
TODO: Write a gem description
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
gem 'weixin_public'
|
|
10
|
+
|
|
11
|
+
And then execute:
|
|
12
|
+
|
|
13
|
+
$ bundle
|
|
14
|
+
|
|
15
|
+
Or install it yourself as:
|
|
16
|
+
|
|
17
|
+
$ gem install weixin_public
|
|
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"
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
require "weixin_public/version"
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
require 'digest'
|
|
5
|
+
require 'faraday'
|
|
6
|
+
require 'json'
|
|
7
|
+
require 'net/http'
|
|
8
|
+
require 'nokogiri'
|
|
9
|
+
require 'openssl'
|
|
10
|
+
|
|
11
|
+
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
|
|
12
|
+
|
|
13
|
+
module WeixinPublic
|
|
14
|
+
|
|
15
|
+
class WeixinObject
|
|
16
|
+
def initialize(params)
|
|
17
|
+
params.each { |var,val| self.send "#{var}=", val if self.class.instance_methods.include?(var.to_sym)}
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class AppMsg < WeixinObject
|
|
22
|
+
attr_accessor :appId,:title,:time
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
class WeixinFan < WeixinObject
|
|
26
|
+
attr_accessor :fakeId,:nickName,:remarkName,:groupId
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
class WeixinMessage < WeixinObject
|
|
30
|
+
attr_accessor :fileId,:source,:fakeId,:hasReply,:nickName,:remarkName,:dateTime,:id,:type,:content
|
|
31
|
+
|
|
32
|
+
def filePath
|
|
33
|
+
file_type = {"2"=>"jpg","3"=>"mp3","4"=>"mp4"}[@type]
|
|
34
|
+
"#{@id}.#{file_type}"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def fileType
|
|
38
|
+
filetype={"2"=>"image/jpg","3"=>"audio/mp3","4"=>"video/mp4"}[@type]
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class WeixinPubClient
|
|
44
|
+
def initialize(username,password,sig=nil,cert=nil)
|
|
45
|
+
@username=username
|
|
46
|
+
@password=password
|
|
47
|
+
@sig = sig
|
|
48
|
+
@cert = cert
|
|
49
|
+
@conn = Faraday.new(:url => 'https://mp.weixin.qq.com')
|
|
50
|
+
@conn_multipart = Faraday.new(:url => 'https://mp.weixin.qq.com') do |faraday|
|
|
51
|
+
faraday.request :multipart
|
|
52
|
+
faraday.adapter :net_http
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def login(username,pwd)
|
|
58
|
+
pwd = Digest::MD5.hexdigest(pwd)
|
|
59
|
+
@cookie = @sig?"sig=#{@sig};cert=#{@cert}":""
|
|
60
|
+
params = {"username"=>username,"pwd"=>pwd,"imgcode"=>'',"f"=>'json'}
|
|
61
|
+
ret = request(:post,'/cgi-bin/login?lang=zh_CN',params,nil)
|
|
62
|
+
return 'login failed' if !ret.headers["set-cookie"]
|
|
63
|
+
ret.headers["set-cookie"].split(',').each do |c|
|
|
64
|
+
@cookie << c.split(';')[0] <<";"
|
|
65
|
+
end
|
|
66
|
+
msg = JSON.parse(ret.body)["ErrMsg"]
|
|
67
|
+
@token = msg[msg =~ /token/..-1].split('=')[1]
|
|
68
|
+
ret = request(:get,"/cgi-bin/indexpage?token=#{@token}&t=wxm-index&lang=zh_CN",{"f"=>'json'},nil)
|
|
69
|
+
return ret.status.to_s
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def get_fans
|
|
73
|
+
return if !@cookie && login(@username,@password) =~ /failed/
|
|
74
|
+
ret = []
|
|
75
|
+
for i in 0..100 do
|
|
76
|
+
res = request(:get,"/cgi-bin/contactmanagepage?t=wxm-friend&lang=zh_CN&pagesize=&pageidx=0&type=0&groupid=0&pageidx=#{i}",{},nil)
|
|
77
|
+
doc = Nokogiri::HTML(res.body)
|
|
78
|
+
fans = JSON.parse(doc.css('#json-friendList').to_s[/\[.*?\]/m])
|
|
79
|
+
break if fans.length == 0
|
|
80
|
+
fans.each { |f| ret<< WeixinFan.new(f) }
|
|
81
|
+
end
|
|
82
|
+
ret
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def get_posts
|
|
86
|
+
return if !@cookie && login(@username,@password) =~ /failed/
|
|
87
|
+
ret = request(:get,"/cgi-bin/operate_appmsg?sub=list&t=wxm-appmsgs-list-new&type=10&pageIdx=0&pagesize=10&subtype=3&f=json",{},nil)
|
|
88
|
+
doc = Nokogiri::HTML(ret.body)
|
|
89
|
+
posts = doc.css('#json-msglist').to_s
|
|
90
|
+
posts = JSON.parse(posts[posts.index(/{/)..posts.rindex(/}/)])["list"]
|
|
91
|
+
ret = []
|
|
92
|
+
posts.each {|po| ret << AppMsg.new(po)}
|
|
93
|
+
ret
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def get_messages(fakeId,download=false)
|
|
97
|
+
return if !@cookie && login(@username,@password) =~ /failed/
|
|
98
|
+
#doc.force_encoding('gbk')
|
|
99
|
+
#doc.encode!("utf-8", :undef => :replace, :invalid => :replace)
|
|
100
|
+
doc = Nokogiri::HTML(request(:get,"/cgi-bin/singlemsgpage?fromfakeid=#{fakeId}&msgid=&source=&count=20&t=wxm-singlechat&f=json",{},"https://mp.weixin.qq.com/cgi-bin/getmessage").body)
|
|
101
|
+
messages = doc.css('#json-msgList').to_s.encode("UTF-8", "UTF-8",:invalid => :replace)
|
|
102
|
+
messages = JSON.parse(messages[messages.index(/\[/)..messages.rindex(/\]/)])
|
|
103
|
+
ret = []
|
|
104
|
+
messages.each do |m|
|
|
105
|
+
next if m["type"]=="10" || m["fakeId"]!=fakeId
|
|
106
|
+
if m["type"]!="10" then
|
|
107
|
+
ret << WeixinMessage.new(m)
|
|
108
|
+
ret[-1].dateTime = Time.at(m["dateTime"].to_i)
|
|
109
|
+
end
|
|
110
|
+
if download then
|
|
111
|
+
download_file(ret[-1])
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
ret
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def download_file(message)
|
|
118
|
+
url="/cgi-bin/downloadfile?msgid=#{message.id}&source="
|
|
119
|
+
avatar_download(message.filePath,url)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def send_message(content,reciever,type="1")
|
|
123
|
+
return if !@cookie && login(@username,@password) =~ /failed/
|
|
124
|
+
body = {"error"=>false,"ajax"=>1,"f"=>"json","type"=>type,"tofakeid"=>reciever}
|
|
125
|
+
if type == "1"
|
|
126
|
+
body["content"]=content
|
|
127
|
+
else
|
|
128
|
+
body["fid"]= content
|
|
129
|
+
body["fileid"]=content
|
|
130
|
+
end
|
|
131
|
+
ret = request(:post,"/cgi-bin/singlesend?t=ajax-response&lang=zh_CN",body,"https://mp.weixin.qq.com/cgi-bin/singlemsgpage")
|
|
132
|
+
puts ret.body
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def send_post(appmsgid,reciever)
|
|
136
|
+
return if !@cookie && login(@username,@password) =~ /failed/
|
|
137
|
+
body = {"error"=>false,"ajax"=>1,"f"=>"json","tofakeid"=>reciever,"fid"=>appmsgid,"appmsgid"=>appmsgid,"type"=>10}
|
|
138
|
+
ret = request(:post,"/cgi-bin/singlesend?t=ajax-response&lang=zh_CN",body,"https://mp.weixin.qq.com/cgi-bin/singlemsgpage")
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def avatar_upload(avatar,type="image/png")
|
|
143
|
+
return if !@cookie && login(@username,@password) =~ /failed/
|
|
144
|
+
payload = { :uploadFile => Faraday::UploadIO.new(avatar, type)}
|
|
145
|
+
@conn_multipart.headers["Cookie"] = @cookie
|
|
146
|
+
@conn_multipart.headers["Referer"]="http://mp.weixin.qq.com/cgi-bin/indexpage"
|
|
147
|
+
ret=@conn_multipart.post "/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=0&token=#{@token}&t=iframe-uploadfile&lang=zh_CN&formId=null&f=json",payload
|
|
148
|
+
return ret.body.to_s[/\'.*?\'/m][1..-2] if ret.body =~ /suc/m
|
|
149
|
+
return nil
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def avatar_download(file,url)
|
|
153
|
+
puts "download-#{url}"
|
|
154
|
+
ret = request(:get,url,{},nil)
|
|
155
|
+
File.open(file, 'wb') { |fp| fp.write(ret.body) }
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def request(method,url,params,referer)
|
|
159
|
+
@conn.headers["Cookie"] = @cookie
|
|
160
|
+
@conn.headers["Referer"] = referer if referer
|
|
161
|
+
if method == :post then
|
|
162
|
+
ret = @conn.post do |req|
|
|
163
|
+
req.url url
|
|
164
|
+
req.body = params
|
|
165
|
+
req.body['token']=@token
|
|
166
|
+
end
|
|
167
|
+
else
|
|
168
|
+
ret = @conn.get do |req|
|
|
169
|
+
req.url url
|
|
170
|
+
@conn.params['token']=@token
|
|
171
|
+
#@conn.params['lang']="zh_CN"
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
ret
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'weixin_public/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "weixin_public"
|
|
8
|
+
spec.version = WeixinPublic::VERSION
|
|
9
|
+
spec.authors = "goxplanet"
|
|
10
|
+
spec.email = "otec.teng@gmail.com"
|
|
11
|
+
spec.description = %q{weixin public client }
|
|
12
|
+
spec.summary = %q{weixin public client,working with web page interface ,using HTTPS}
|
|
13
|
+
spec.homepage = ""
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files`.split($/)
|
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
|
+
spec.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
|
22
|
+
spec.add_development_dependency "rake"
|
|
23
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: weixin_public
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
prerelease:
|
|
5
|
+
version: 0.0.1
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- goxplanet
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
|
|
13
|
+
date: 2013-05-10 00:00:00 Z
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: bundler
|
|
17
|
+
prerelease: false
|
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
19
|
+
none: false
|
|
20
|
+
requirements:
|
|
21
|
+
- - ~>
|
|
22
|
+
- !ruby/object:Gem::Version
|
|
23
|
+
version: "1.3"
|
|
24
|
+
type: :development
|
|
25
|
+
version_requirements: *id001
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: rake
|
|
28
|
+
prerelease: false
|
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
|
30
|
+
none: false
|
|
31
|
+
requirements:
|
|
32
|
+
- - ">="
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: "0"
|
|
35
|
+
type: :development
|
|
36
|
+
version_requirements: *id002
|
|
37
|
+
description: "weixin public client "
|
|
38
|
+
email: otec.teng@gmail.com
|
|
39
|
+
executables: []
|
|
40
|
+
|
|
41
|
+
extensions: []
|
|
42
|
+
|
|
43
|
+
extra_rdoc_files: []
|
|
44
|
+
|
|
45
|
+
files:
|
|
46
|
+
- .gitignore
|
|
47
|
+
- Gemfile
|
|
48
|
+
- LICENSE.txt
|
|
49
|
+
- README.md
|
|
50
|
+
- Rakefile
|
|
51
|
+
- lib/weixin_public.rb
|
|
52
|
+
- lib/weixin_public/version.rb
|
|
53
|
+
- weixin_public.gemspec
|
|
54
|
+
homepage: ""
|
|
55
|
+
licenses:
|
|
56
|
+
- MIT
|
|
57
|
+
post_install_message:
|
|
58
|
+
rdoc_options: []
|
|
59
|
+
|
|
60
|
+
require_paths:
|
|
61
|
+
- lib
|
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
|
+
none: false
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: "0"
|
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
|
+
none: false
|
|
70
|
+
requirements:
|
|
71
|
+
- - ">="
|
|
72
|
+
- !ruby/object:Gem::Version
|
|
73
|
+
version: "0"
|
|
74
|
+
requirements: []
|
|
75
|
+
|
|
76
|
+
rubyforge_project:
|
|
77
|
+
rubygems_version: 1.8.10
|
|
78
|
+
signing_key:
|
|
79
|
+
specification_version: 3
|
|
80
|
+
summary: weixin public client,working with web page interface ,using HTTPS
|
|
81
|
+
test_files: []
|
|
82
|
+
|