e360-tuiguang 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f6fa54d802fea8f124c23c023d0823e24137e30
4
- data.tar.gz: b9abb6f0f9b74bc7fe885458d4a139844c37be38
3
+ metadata.gz: bdc98be1e89c0a0319d0b93f41738cd6838cf628
4
+ data.tar.gz: 8b8420fc0e8d9e7d1558f4611c893b949e3feafe
5
5
  SHA512:
6
- metadata.gz: ecc47a1cb07381a9593a152c71ef5e0c0559ea5d0f38c57fc639f466fad02b92f39c2bc6cdda4ad7bc6928db1d4faf471cdc98f7ce73885eff1f984e07eaea96
7
- data.tar.gz: 021bca90c867e4cfe384934d65101d5d4d9d4e83011c7983f4f8855b85f1ca5c5ea9223ad3ebeaf55fb8d66f5f67c37b01e2def104be51627ad5f8bb7bef8078
6
+ metadata.gz: 05e4e960d2c87b19e224c9b451f7e49f151e59cf5fda4251a6510640e230a872432e64c1ea98ce6a67461e9f7b4fd1c3cdfb3b0979d965c4027382c2647baeff
7
+ data.tar.gz: 56b0d0457745fe45cb75dffb5cb1cb7c945b14668ae736d20ddf11659e6dab7ac9670cb02e9781a29448ba0e0118f3e6b171f48469d2ffaf06ae5746e2435254
@@ -8,12 +8,14 @@ require 'stringio'
8
8
  module E360
9
9
  module Tuiguang
10
10
  class Action
11
- attr_reader :req_url
12
- attr_reader :req_params
13
- attr_reader :resp
11
+ attr_reader :url
12
+ attr_reader :params
13
+ attr_reader :res
14
14
 
15
15
  def initialize(cookies)
16
16
  @cookies = cookies
17
+ @token = nil
18
+ @expire_times = nil
17
19
  end
18
20
 
19
21
  def basic_info
@@ -83,8 +85,6 @@ module E360
83
85
  next if index < 2
84
86
 
85
87
  item = {}
86
- #
87
- pattern = %r(<input type=checkbox name="check-id" data-gid="(?<group_id>\d+)" data-pid="(?<plan_id>\d+)" data-name="(?<keyword>[^"]+)" data-price="(?<price>[\d\\.]+)" data-left3price="[.\d]+" data-leftprice="[.\d]+" data-minprice="[.\d]+" data-status="" value="(?<keyword_id>\d+)" plan_device="0">.+?<td class="g-status-[^"]+_ f-tal"[^>]*>(?<status>.+?)<a[^>]* data-status="?(?<status_code>-?\d+)"?[^>]*>.+?<td class="(g-status-[^"]+)?_"[^>]*>(?<mobile_status>[\s\S]+?)</td>.+?<td class="effect">\s*<span data-per="?(?<quality_score>-?\d+)"?.+?.+?<td class="effect">\s*<span data-per="?(?<mobile_quality_score>-?\d+)"?.+?<i[^>]* data-match="(?<match>[^"]+)">.+?<i class="b-url [^"]+" data-url="(?<url>[^"]+)">.+?<i class="b-mUrl [^"]+" data-url="(?<mobile_url>[^"]*)")
88
88
 
89
89
  %r{<input type=checkbox[^>]* value="(?<id>\d+)"}.match(tr) {|m| item['id'] = m['id']}
90
90
  %r{<input type=checkbox[^>]* data-name="(?<keyword>[^"]+)"}.match(tr) {|m| item['keyword'] = m['keyword']}
@@ -119,41 +119,61 @@ module E360
119
119
  end
120
120
 
121
121
  def keyword_pause(keyword_id)
122
- result = ajax_request('http://dianjing.e.360.cn/keyword/upstatus', wd: keyword_id, token: @token, expire_times: @expire_times, json: 1, status: 1)
123
- result['status'] && result['msg'] == '修改成功'
122
+ keyword_pause_and_enable(keyword_id)
124
123
  end
125
124
 
126
125
  def keyword_enable(keyword_id)
127
- result = ajax_request('http://dianjing.e.360.cn/keyword/upstatus', wd: keyword_id, token: @token, expire_times: @expire_times, json: 1, status: -5)
126
+ keyword_pause_and_enable(keyword_id, false)
127
+ end
128
+
129
+ def keywords_pause(keyword_ids = [])
130
+ keywords_pause_and_enable(keyword_ids)
131
+ end
132
+
133
+ def keywords_enable(keyword_ids = [])
134
+ keywords_pause_and_enable(keyword_ids, false)
135
+ end
136
+
137
+ def keyword_pause_and_enable(keyword_id, pause = true)
138
+ result = ajax_request('http://dianjing.e.360.cn/keyword/upstatus', wd: keyword_id, json: 1, status: pause ? -5 : 1)
139
+ result['status'] && result['msg'] == '修改成功'
140
+ end
141
+
142
+ def keywords_pause_and_enable(keyword_ids = [], pause = true)
143
+ params = {oper_type: pause ? -5 : 1, json: 1}
144
+ oper_id = {}
145
+ keyword_ids.map{|keyword_id| oper_id[keyword_id.to_s] = ''}
146
+ params[:oper_id] = oper_id.to_json
147
+ result = ajax_request('http://dianjing.e.360.cn/keyword/upallstatus', params)
128
148
  result['status'] && result['msg'] == '修改成功'
129
149
  end
130
150
 
131
151
  def request(url)
132
- url = URI(url)
133
- params = URI.decode_www_form(url.query)
152
+ uri = URI(url)
153
+ params = []
154
+ params = URI.decode_www_form(uri.query) unless uri.query.nil?
134
155
  params.unshift ['_', Time.now.strftime('%s%L')]
135
156
  if @token && @expire_times
136
157
  params.unshift ['expireTimes', @expire_times]
137
158
  params.unshift ['token', @token]
138
159
  end
139
- url.query = URI.encode_www_form(params)
140
- @req_url = url.to_s
141
- http = Net::HTTP.new(url.host, url.port)
142
- request = Net::HTTP::Get.new(url)
160
+ uri.query = URI.encode_www_form(params)
161
+ @url = uri.to_s
162
+ http = Net::HTTP.new(uri.host, uri.port)
163
+ request = Net::HTTP::Get.new(uri)
143
164
  request["accept"] = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
144
165
  request["accept-encoding"] = 'gzip, deflate, sdch'
145
166
  request["accept-language"] = 'zh-CN,zh;q=0.8'
146
167
  request["cache-control"] = 'no-cache'
147
168
  request["connection"] = 'keep-alive'
148
169
  request["cookie"] = @cookies
149
- request["host"] = 'dianjing.e.360.cn'
150
170
  request["pragma"] = 'no-cache'
151
171
  request["referer"] = "http://dianjing.e.360.cn/adrank?_=#{Time.now.strftime('%s%L')}"
152
172
  request["upgrade-insecure-requests"] = '1'
153
173
  request["user-agent"] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
154
174
  retry_times = 0
155
175
  begin
156
- puts "Url: #{@req_url}"
176
+ puts "Url: #{@url}"
157
177
  response = http.request(request)
158
178
  if response.is_a?(Net::HTTPRedirection)
159
179
  p "Redirection: #{response['location']}"
@@ -168,21 +188,25 @@ module E360
168
188
  end
169
189
  end if !response.is_a?(Net::HTTPSuccess) && retry_times < 10
170
190
  raise RequestError, "HTTP Response: #{response.code} #{response.message}" unless response.is_a?(Net::HTTPSuccess)
171
- @resp = response.read_body
191
+ @res = response.read_body
172
192
  if response['Content-Encoding'] && response['Content-Encoding'].include?('gzip')
173
- @resp = Zlib::GzipReader.new(StringIO.new(@resp)).read
193
+ @res = Zlib::GzipReader.new(StringIO.new(@res)).read
174
194
  end
175
- %r("token", *"([^"]+)").match(@resp) do |tm|
195
+ %r("token", *"([^"]+)").match(@res) do |tm|
176
196
  @token = tm[1]
177
- %r("expireTimes", *"([^"]+)").match(@resp) do |etm|
197
+ %r("expireTimes", *"([^"]+)").match(@res) do |etm|
178
198
  @expire_times = etm[1]
179
199
  end
180
200
  end
181
- @resp
201
+ @res
182
202
  end
183
203
 
184
204
  def ajax_request(url, params = {})
185
- url = URI(@req_url = url)
205
+ data = params
206
+ data = params.to_a if params.is_a?(Hash)
207
+ data << ['token', @token]
208
+ data << ['expire_times', @expire_times]
209
+ url = URI(@url = url)
186
210
  http = Net::HTTP.new(url.host, url.port)
187
211
  request = Net::HTTP::Post.new(url)
188
212
  request["accept"] = 'application/json, text/javascript, */*; q=0.01'
@@ -201,17 +225,17 @@ module E360
201
225
  request["token"] = @token
202
226
  request["user-agent"] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
203
227
  request["x-requested-with"] = 'XMLHttpRequest'
204
- request.set_form_data(@req_params = params)
228
+ request.set_form_data(@params = data)
205
229
  response = http.request(request)
206
230
  puts "Redirection: #{response['location']}" if response.is_a?(Net::HTTPRedirection)
207
231
  raise TokenExpired if response.is_a?(Net::HTTPRedirection) && response['location'] == 'http://dianjing.e.360.cn/forbidden/'
208
232
  raise RequestError, "HTTP Response: #{response.code} #{response.message}" unless response.is_a?(Net::HTTPSuccess)
209
- @resp = response.read_body
233
+ @res = response.read_body
210
234
  if response['Content-Encoding'] && response['Content-Encoding'].include?('gzip')
211
- @resp = Zlib::GzipReader.new(StringIO.new(@resp)).read
235
+ @res = Zlib::GzipReader.new(StringIO.new(@res)).read
212
236
  end
213
- p @resp
214
- JSON.parse(@resp)
237
+ p @res
238
+ JSON.parse(@res)
215
239
  end
216
240
  end
217
241
 
@@ -1,5 +1,5 @@
1
1
  module E360
2
2
  module Tuiguang
3
- VERSION = "0.1.6"
3
+ VERSION = "0.1.7"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: e360-tuiguang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xiao Jie