Class: Qzone
- Inherits:
-
Object
- Object
- Qzone
- Defined in:
- lib/qzone.rb,
lib/qzone/version.rb,
lib/qzone/interface.rb,
lib/qzone/qzone_login.rb
Defined Under Namespace
Classes: Login
Constant Summary collapse
- VERSION =
"0.1.0"- Interface =
{ "ablum" => "http://h5.qzone.qq.com/proxy/domain/tjalist.photo.qq.com/fcgi-bin/fcg_list_album_v3?g_tk=%s&callback=shine0_Callback&t=419043014&hostUin=%s&uin=%s&appid=4&inCharset=utf-8&outCharset=utf-8&source=qzone&plat=qzone&format=jsonp¬ice=0&filter=1&handset=4&pageNumModeSort=40&pageNumModeClass=15&needUserInfo=1&idcNum=5&callbackFun=shine0&_=1475649079634", "photo" => "http://h5.qzone.qq.com/proxy/domain/tjplist.photo.qzone.qq.com/fcgi-bin/cgi_list_photo?g_tk=%s&callback=shine0_Callback&t=775643811&mode=0&idcNum=5&hostUin=%s&topicId=%s&noTopic=0&uin=%s&pageStart=%s&pageNum=30&skipCmtCount=0&singleurl=1&batchId=¬ice=0&appid=4&inCharset=utf-8&outCharset=utf-8&source=qzone&plat=qzone&outstyle=json&format=jsonp&json_esc=1&question=&answer=&callbackFun=shine0&_=1475649257165", "friend" => "https://h5.qzone.qq.com/proxy/domain/r.qzone.qq.com/cgi-bin/tfriend/friend_ship_manager.cgi?uin=%s&do=1&rd=0.9983570276719183&fupdate=1&clean=1&g_tk=%s" }
Instance Method Summary collapse
- #ablums(dest_qq) ⇒ Object
- #cookies ⇒ Object
- #cookies_valid? ⇒ Boolean
- #download_photos(photos, path) ⇒ Object
- #friends ⇒ Object
- #gtk(skey) ⇒ Object
-
#initialize(user, password) ⇒ Qzone
constructor
A new instance of Qzone.
-
#json(str) ⇒ Object
private.
- #photos_in_ablum(ablum) ⇒ Object
Constructor Details
#initialize(user, password) ⇒ Qzone
Returns a new instance of Qzone.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/qzone.rb', line 6 def initialize(user, password) @user = user @password = password @spider = Mechanize.new if File.exist? "cookie#{@user}" @spider..load "cookie#{@user}" skey = @spider..find do |e| e.name == 'p_skey' end @gtk = self.gtk(skey) # if cookie is not valid begin unless self. File.delete "cookie#{@user}" self. end rescue self. end else self. end end |
Instance Method Details
#ablums(dest_qq) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/qzone.rb', line 69 def ablums(dest_qq) result = @spider.get(sprintf(Interface["ablum"], @gtk.to_s, dest_qq, @user)) result = self.json result.content ablums = [] return ablums if result['data']['albumListModeSort'].nil? result['data']['albumListModeSort'].each do |e| temp = {} temp[:host] = dest_qq temp[:id] = e['id'] temp[:name] = e['name'] temp[:total] = e['total'] temp[:allowAccess] = e['allowAccess'].to_i # temp[:allowAccess] = 0 if temp[:allowAccess] != 1 ablums.push temp end ablums end |
#cookies ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/qzone.rb', line 33 def account = {} account[:user] = @user account[:password] = @password qzone = Login.new @user, @password, 5 qzone.login @cookies = qzone. qzone.close # construct cookies @cookies.each do || [:expires] = Date.today + 1 if [:expires].nil? = Mechanize::Cookie.new( domain: [:domain], name: [:name], value: [:value], path: [:path], expires: [:expires].to_s ) @spider. << end skey = @spider..find do |e| e.name == 'p_skey' end @gtk = self.gtk skey @spider.request_headers = { 'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36' } @spider..save_as "cookie#{@user}", session: true end |
#cookies_valid? ⇒ Boolean
180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/qzone.rb', line 180 def begin result = @spider.get(sprintf(Interface["ablum"], @gtk.to_s, @user, @user)) rescue Exception => error if error.response_code == '403' raise 'Connection refused' else raise 'unknown error happened in checking cookies' end end !result.content.toutf8.include? '尚未登录或者登录超时' end |
#download_photos(photos, path) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/qzone.rb', line 148 def download_photos(photos, path) worker = Mechanize.new worker.request_headers = { 'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36', "Referer" => "http://qzone.qq.com/" } photos.each do |e| result = worker.get e[:url] result.save_as path + e[:name] + "_#{Time.now.hash}" + '.jpg' end end |
#friends ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/qzone.rb', line 125 def friends result = @spider.get(sprintf(Interface["friend"], @user, @gtk)) result = self.json result.content friends = [] return friends if result['data']['items_list'].nil? result['data']['items_list'].each do |e| friend = {} friend[:uin] = e['uin'] friend[:name] = e['name'] # 亲密度 friend[:score] = e['score'] friend[:img] = e['img'] friends.push friend end friends end |
#gtk(skey) ⇒ Object
161 162 163 164 165 166 167 168 169 |
# File 'lib/qzone.rb', line 161 def gtk(skey) raise "Skey can't be nil" if skey.nil? hashes = 5381 skey.value.each_char do |c| hashes += (hashes << 5) + c.ord end hashes & 0x7fffffff end |
#json(str) ⇒ Object
private
172 173 174 175 176 177 178 |
# File 'lib/qzone.rb', line 172 def json(str) # str.gsub!(/shine0_Callback\(/, "" ) str.sub!(/.*_Callback\(/, '') str.sub!(/\);/, '') JSON.parse str end |
#photos_in_ablum(ablum) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/qzone.rb', line 92 def photos_in_ablum(ablum) if ablum[:allowAccess] != 1 raise "can't access to ablum \"#{ablum[:name]}\"" end photos = [] return photos if ablum[:total].to_i.zero? total_photos = ablum[:total].to_i pages = total_photos / 30 pages += 1 if total_photos % 30 != 0 pages.times do |e| result = @spider.get(sprintf(Interface["photo"], @gtk, ablum[:host], ablum[:id], @user, (e*30).to_s)) result = self.json result.content return photos if result['data']['photoList'].nil? result['data']['photoList'].each do |e| photo = {} photo[:id] = e['id'] photo[:name] = e['name'] photo[:url] = e['url'] photos.push photo end end photos end |