qq_weibo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in qq_weibo.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 chenglong
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
+ # QqWeibo
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'qq_weibo'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install qq_weibo
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/lib/qq_weibo.rb ADDED
@@ -0,0 +1,190 @@
1
+ require 'hashie'
2
+ require 'openssl'
3
+ require 'net/https'
4
+ require 'net/http'
5
+ require 'uri'
6
+ require 'json'
7
+ require 'cgi'
8
+ require "qq_weibo/version"
9
+
10
+ module QqWeibo
11
+ #对腾讯微博返回的用户信息封装
12
+ def self.merge_info(d)
13
+ user_info = Hashie::Mash.new
14
+ user_info.merge!({:user_id=>d["openid"]})
15
+ user_info.merge!({:head_url=>d["head"] == "" ? "http://mat1.gtimg.com/www/mb/img/p1/head_normal_50.png" : "#{d["head"]}"})
16
+ if !d["gender"].nil? && !d["gender"].blank?
17
+ user_info.merge!({:sex=>d["gender"]=='m' ? 1 : 0})
18
+ else
19
+ user_info.merge!({:sex=>2})
20
+ end
21
+ user_info.merge!({:fans_number=>d["followers_count"]})
22
+ user_info.merge!({:location =>d["location"]})
23
+ user_info.merge!({:name=>d["name"]})
24
+ user_info.merge!({:nick=>d["nick"]})
25
+ user_info.merge!({:weburl=>"http://weibo.com/"})
26
+ user_info.merge!({:verified_type=>d["isvip"]})
27
+ return user_info
28
+ end
29
+
30
+ #取消关注
31
+ def self.destroy_focus(appkey, appsecret, openid, openkey, fopenid)
32
+ url = URI("http://open.t.qq.com/api/friends/del")
33
+ current_sig = Digest::SHA1.hexdigest "#{URI.escape("/user/info")}&#{URI.escape("fopenid=#{fopenid}&oauth_version=2.a&scope=all")}&#{appsecret}"
34
+ http = Net::HTTP.new(url.host, "443")
35
+ http.use_ssl = true
36
+ request = Net::HTTP::Post.new(url.path)
37
+ request.set_form_data({:appid => appkey, :format => "json", :fopenid => fopenid, :openid => openid, :openkey => openkey, :reqtime => Time.now.to_i, :sig => current_sig, :wbversion => 1})
38
+ http.request(request).body
39
+ end
40
+
41
+ #关注
42
+ def self.add_focus(appkey, appsecret, openid, openkey, fopenid)
43
+ url = URI("http://open.t.qq.com/api/friends/add")
44
+ current_sig = Digest::SHA1.hexdigest "#{URI.escape("/friends/add")}&#{URI.escape("fopenids=#{fopenid}")}&#{appsecret}"
45
+ http = Net::HTTP.new(url.host, "443")
46
+ http.use_ssl = true
47
+ request = Net::HTTP::Post.new(url.path)
48
+ request.set_form_data({:appid => appkey, :format => "json", :fopenids => fopenid, :openid => openid, :openkey => openkey, :reqtime => Time.now.to_i, :sig => current_sig, :wbversion => 1})
49
+ http.request(request).body
50
+ end
51
+
52
+ def self.encoderunmber(str)
53
+ str = str.to_s
54
+ sup_str = ""
55
+ 0.upto(str.length-1) do |i|
56
+ s = i%2==0 ? "#{newpass(2)}" : "#{newpass(1)}"
57
+ new_str = "#{str.at(i)}" + "#{s}"
58
+ sup_str += "#{new_str}"
59
+ end
60
+ return sup_str.to_s
61
+ end
62
+
63
+ def self.decoderunmber(str)
64
+ str = str.to_s
65
+ n_st = ""
66
+ i = str.length%5 == 0 ? str.length/5 : (str.length/5)+1
67
+ 1.upto i do |j|
68
+ h = (j-1)*5
69
+ dup = str[h..(h+4)]
70
+ n_st += dup.at(0)
71
+ n_st += dup.at(3) if !dup.at(3).blank?
72
+ end
73
+ return n_st.to_s
74
+ end
75
+
76
+ def self.newpass(len)
77
+ chars = ("a".."z").to_a + ("A".."Z").to_a + (0..9).to_a
78
+ newpass = ""
79
+ 1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
80
+ return newpass
81
+ end
82
+
83
+ #获得单个的用户信息
84
+ def self.get_user_info(appkey, appsecret, openid, openkey)
85
+ url = URI("https://open.t.qq.com/api/user/info")
86
+ current_sig = Digest::SHA1.hexdigest "#{URI.escape("/user/info")}&#{URI.escape("openid=#{openid}&oauth_version=2.a&scope=all")}&#{appsecret}"
87
+ http = Net::HTTP.new(url.host, url.port)
88
+ http.use_ssl = true
89
+ request = Net::HTTP::Get.new(url.path+"?appid=#{appkey}&format=json&openid=#{openid}&openkey=#{openkey}&reqtime=#{Time.now.to_i}&sig=#{current_sig}&wbversion=1")
90
+ return merge_info(JSON.parse(http.request(request).body)["data"])
91
+ end
92
+
93
+ #获得单个的用户信息
94
+ def self.get_user_infos(appkey, appsecret, openid, openkey, openids)
95
+ url = URI("https://open.t.qq.com/api/user/infos")
96
+ current_sig = Digest::SHA1.hexdigest "#{URI.escape("/user/infos")}&#{URI.escape("names=#{openids}")}&#{appsecret}&#{appsecret}"
97
+ http = Net::HTTP.new(url.host, url.port)
98
+ http.use_ssl = true
99
+ request = Net::HTTP::Get.new(url.path+"?appid=#{appkey}&format=json&openid=#{openid}&openkey=#{openkey}&names=#{openids}&reqtime=#{Time.now.to_i}&sig=#{current_sig}&wbversion=1")
100
+ return JSON.parse(http.request(request).body)["data"]
101
+ end
102
+
103
+ #判断账户的收听关系
104
+ def self.friendship_show(appkey, appsecret, openid, openkey, openids)
105
+ url = URI("https://open.t.qq.com/api/friends/check")
106
+ current_sig = Digest::SHA1.hexdigest "#{URI.escape("/friends/check")}&#{URI.escape("fopenids=#{openids}&flag=1")}&#{appsecret}"
107
+ http = Net::HTTP.new(url.host, url.port)
108
+ http.use_ssl = true
109
+ request = Net::HTTP::Get.new(url.path+"?appid=#{appkey}&format=json&openid=#{openid}&openkey=#{openkey}&fopenids=#{openids}&flag=1&reqtime=#{Time.now.to_i}&sig=#{current_sig}&wbversion=1")
110
+ begin
111
+ return JSON.parse(http.request(request).body)["data"][openids]
112
+ rescue Exception => e
113
+ return []
114
+ end
115
+ end
116
+
117
+ #获取用户的双向收听列表
118
+ def self.friends_bilateral(appkey, appsecret, openid, openkey, page)
119
+ url = URI("http://open.t.qq.com/api/friends/mutual_list")
120
+ current_sig = Digest::SHA1.hexdigest "#{URI.escape("/friends/mutual_list")}&#{URI.escape("fopenid=#{openid}&reqnum=30&startindex=#{page}")}&#{appsecret}"
121
+ http = Net::HTTP.new(url.host, url.port)
122
+ # http.use_ssl = true
123
+ request = Net::HTTP::Get.new(url.path+"?appid=#{appkey}&format=json&openid=#{openid}&openkey=#{openkey}&fopenid=#{openid}&reqnum=30&startindex=#{page}&reqtime=#{Time.now.to_i}&sig=#{current_sig}&wbversion=1")
124
+ begin
125
+
126
+ return JSON.parse(http.request(request).body)["data"]["info"]
127
+ rescue Exception => e
128
+ return nil
129
+ end
130
+ end
131
+
132
+ #判断账户的收听关系
133
+ def self.attention_qq(appkey, token, openid, ip, name)
134
+ url = URI("http://open.t.qq.com/api/friends/check")
135
+ http = Net::HTTP.new(url.host, "443")
136
+ http.use_ssl = true
137
+ request = Net::HTTP::Get.new(url.path+"?oauth_consumer_key=#{appkey}&access_token=#{token}&openid=#{openid}&clientip=#{ip}&oauth_version=2.a&names=#{name}&flag=1")
138
+ return JSON.parse(http.request(request).body)["data"].values[0]
139
+ end
140
+
141
+ #收听某用户
142
+ def self.attention_info(appkey, token, openid, ip, name)
143
+ url = URI("http://open.t.qq.com/api/friends/add")
144
+ http = Net::HTTP.new(url.host, "443")
145
+ http.use_ssl = true
146
+ request = Net::HTTP::Get.new(url.path+"?oauth_consumer_key=#{appkey}&access_token=#{token}&openid=#{openid}&clientip=#{ip}&oauth_version=2.a&name=#{URI.escape(name)}")
147
+ #Rails.logger.info(http.request(request).body)
148
+ end
149
+
150
+ #发表一条微博信息
151
+ def self.put(appkey, appsecret, openid, openkey, content, ip)
152
+ url = URI("http://open.t.qq.com/api/t/add")
153
+ current_sig = Digest::SHA1.hexdigest "#{URI.escape("/t/add")}&#{URI.escape("content=#{content}&clientip=#{ip}")}&#{appsecret}"
154
+ http = Net::HTTP.new(url.host, "443")
155
+ http.use_ssl = true
156
+ request = Net::HTTP::Post.new(url.path)
157
+ request.set_form_data({:appid => appkey, :format => "json", :clientip => ip, :content => content, :openid => openid, :openkey => openkey, :reqtime => Time.now.to_i, :sig => current_sig, :wbversion => 1})
158
+ http.request(request).body
159
+ end
160
+
161
+
162
+ def self.call_put(appkey, token, openid, ip, content)
163
+ url = URI("http://open.t.qq.com/api/t/add")
164
+ http = Net::HTTP.new(url.host, "443")
165
+ http.use_ssl = true
166
+ request = Net::HTTP::Post.new(url.path)
167
+ request.set_form_data({:oauth_consumer_key => appkey, :access_token => token, :openid => openid, :clientip => ip, :oauth_version => "2.a", :scope => "all", :content => content})
168
+ http.request(request).body
169
+ end
170
+
171
+ #转发一条微博
172
+ def self.truan_put(appkey, token, openid, ip, content, reid)
173
+ url = URI("http://open.t.qq.com/api/t/re_add")
174
+ http = Net::HTTP.new(url.host, "443")
175
+ http.use_ssl = true
176
+ request = Net::HTTP::Post.new(url.path)
177
+ request.set_form_data({:oauth_consumer_key => appkey, :access_token => token, :openid => openid, :clientip => ip, :oauth_version => "2.a", :scope => "all", :content => content, :reid => reid})
178
+ Rails.logger.info(http.request(request).body)
179
+ end
180
+
181
+ #评论一条微博
182
+ def self.sub_comment(appkey, token, openid, ip, content, reid)
183
+ url = URI("http://open.t.qq.com/api/t/comment")
184
+ http = Net::HTTP.new(url.host, "443")
185
+ http.use_ssl = true
186
+ request = Net::HTTP::Post.new(url.path)
187
+ request.set_form_data({:oauth_consumer_key => appkey, :access_token => token, :openid => openid, :clientip => ip, :oauth_version => "2.a", :scope => "all", :content => content, :reid => reid})
188
+ Rails.logger.info(http.request(request).body)
189
+ end
190
+ end
@@ -0,0 +1,3 @@
1
+ module QqWeibo
2
+ VERSION = "0.0.1"
3
+ end
data/qq_weibo.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'qq_weibo/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "qq_weibo"
8
+ gem.version = QqWeibo::VERSION
9
+ gem.authors = ["chenglong"]
10
+ gem.email = ["vtedndx@gmail.com"]
11
+ gem.description = %q{qq_weibo}
12
+ gem.summary = %q{}
13
+ gem.homepage = ""
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
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qq_weibo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - chenglong
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-09 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: qq_weibo
15
+ email:
16
+ - vtedndx@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/qq_weibo.rb
27
+ - lib/qq_weibo/version.rb
28
+ - qq_weibo.gemspec
29
+ homepage: ''
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.24
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: ''
53
+ test_files: []