Class: Baidu::Tuiguang::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/baidu/tuiguang.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookies) ⇒ Action

Returns a new instance of Action.



16
17
18
# File 'lib/baidu/tuiguang.rb', line 16

def initialize(cookies)
  @cookies = cookies
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



13
14
15
# File 'lib/baidu/tuiguang.rb', line 13

def params
  @params
end

#resObject (readonly)

Returns the value of attribute res.



14
15
16
# File 'lib/baidu/tuiguang.rb', line 14

def res
  @res
end

#urlObject (readonly)

Returns the value of attribute url.



12
13
14
# File 'lib/baidu/tuiguang.rb', line 12

def url
  @url
end

Instance Method Details

#basic_infoObject



20
21
22
23
# File 'lib/baidu/tuiguang.rb', line 20

def basic_info
  data = request("path": 'GET/authInfo', "params": {})
  {'username' => data['username']}
end

#groups(plan_id, page = 1, page_size = 20) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/baidu/tuiguang.rb', line 42

def groups(plan_id, page = 1, page_size = 20)
  today = Time.now.to_date
  data = request("path": 'vega/GET/mtllist/unit', "params": {"fields": ["unitid", "planid", "pausestat", "unitname", "planname", "unitbid", "unitMPriceFactor", "shows", "clks", "paysum", "trans", "avgprice", "unitstat", "matchPriceFactor", "matchPriceStatus", "phonetrans", "clkrate", "extbind", "allnegativecnt", "showpay", "deviceprefer", "mPriceFactor", "adtype", "finebidstatus", "atstatus", "atbid", "planatbid", "bidprefer", "pcpricefactor", "planPcpricefactor"], "pageNo": page, "pageSize": page_size, "startTime": today, "endTime": today, "levelCond": {"planid": plan_id}})
  result = {'data' => [], 'count' => data['totalCount']}
  data['listData'].each do |item|
    result['data'] << {
        'id' => item['unitid'],
        'name' => item['unitname'],
        'pause' => item['pausestat'] == 1,
        'status' => ''
    }
  end
  result
end

#keyword_enable(keyword_id) ⇒ Object



102
103
104
# File 'lib/baidu/tuiguang.rb', line 102

def keyword_enable(keyword_id)
  keyword_pause_and_enable(keyword_id, false)
end

#keyword_pause(keyword_id) ⇒ Object



98
99
100
# File 'lib/baidu/tuiguang.rb', line 98

def keyword_pause(keyword_id)
  keyword_pause_and_enable(keyword_id)
end

#keyword_pause_and_enable(keyword_id, pause = true) ⇒ Object



114
115
116
# File 'lib/baidu/tuiguang.rb', line 114

def keyword_pause_and_enable(keyword_id, pause = true)
  request("path": 'vega/MOD/keyword', "params": {"winfoid": [keyword_id.to_i], "items": {"pausestat": pause ? 1 : 0}})
end

#keywords(plan_id, group_id, page = 1, page_size = 20) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/baidu/tuiguang.rb', line 57

def keywords(plan_id, group_id, page = 1, page_size = 20)
  today = Time.now.to_date
  data = request("path": 'vega/GET/mtllist/word', "params": {"fields": ["winfoid", "unitid", "planid", "wordid", "pausestat", "showword", "unitname", "planname", "wordstat", "bid", "unitbid", "pcqscore", "mqscore", "trans", "paysum", "shows", "clks", "avgprice", "wmatch", "shadow_wurl", "shadow_mwurl", "activestat", "wctrl", "wurl", "pageexp", "mwurl", "showqscore", "blockreason", "confidence", "pcblockreason", "pcconfidence", "matchPriceEnableStatus", "clkrate", "showpay", "adtype", "bidprefer", "mPriceGuide", "leftPriceGuide", "left3PriceGuide", "marker", "mconfidence", "mblockreason", "pcqscorecon", "mqscorecon", "pcrelativity", "mrelativity", "pclandpage", "mlandpage", "pclandpagereason", "mlandpagereason", "addfrom", "modfrom"], "pageNo": page, "pageSize": page_size, "startTime": today, "endTime": today, "levelCond": {"unitid": group_id}})
  result = {'data' => [], 'count' => data['totalCount']}
  data['listData'].each do |item|
    match = ''
    if item['wmatch'] == 15
      match = '广泛'
    elsif item['wmatch'] == 31
      match = '短语-'
      if item['wctrl'] == 3
        match += '核心包含'
      elsif item['wctrl'] == 0
        match += '同义包含'
      elsif item['wctrl'] == 1
        match += '精确包含'
      end
    elsif item['wmatch'] == 63
      match = '精确'
    end
    status = ''
    if item['wordstat'] == 13
      status = '计算机搜索无效'
    end
    result['data'] << {
        'id' => item['winfoid'],
        'keyword' => item['showword'],
        'status' => status,
        'mobile_status' => '',
        'pause' => item['pausestat'] == 1,
        'price' => item['bid'],
        'match' => match,
        'quality' => item['pcqscore'],
        'mobile_quality' => item['mqscore'],
        'url' => item['wurl'],
        'mobile_url' => item['mwurl']
    }
  end
  result
end

#keywords_enable(keyword_ids = []) ⇒ Object



110
111
112
# File 'lib/baidu/tuiguang.rb', line 110

def keywords_enable(keyword_ids = [])
  keywords_pause_and_enable(keyword_ids, false)
end

#keywords_pause(keyword_ids = []) ⇒ Object



106
107
108
# File 'lib/baidu/tuiguang.rb', line 106

def keywords_pause(keyword_ids = [])
  keywords_pause_and_enable(keyword_ids)
end

#keywords_pause_and_enable(keyword_ids = [], pause = true) ⇒ Object



118
119
120
# File 'lib/baidu/tuiguang.rb', line 118

def keywords_pause_and_enable(keyword_ids = [], pause = true)
  request("path": 'vega/MOD/adkeyword', "params": {"checkAll": false, "mtlCond": {"levelCond": {"userid": cookies_params['__cas__id__3']}}, "mtlIds": keyword_ids.map {|keyword_id| keyword_id.to_s}, "items": {"pausestat": pause}})
end

#plans(page = 1, page_size = 20) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/baidu/tuiguang.rb', line 25

def plans(page = 1, page_size = 20)
  today = Time.now.to_date
  data = request(path: 'vega/GET/mtllist/plan', params: {"fields": ["planid", "pausestat", "planname", "shows", "clks", "paysum", "trans", "avgprice", "plandynamicideastat", "acctdynamicideastat", "mPriceFactor", "planstat", "remarketingstat", "deviceprefer", "wregion", "qrstat1", "phonetrans", "allipblackcnt", "clkrate", "wbudget", "plancyc", "showprob", "allnegativecnt", "showpay", "adtype", "acctdynamictagsublinkstat", "acctdynamictitlestat", "acctdynamichotredirectstat", "plandynamictagsublinkstat", "plandynamictitlestat", "plandynamichotredirectstat", "plandynamictitlelist", "plandynamictitleabstract", "acctdynamictitlelist", "acctdynamictitleabstract", "acctscenarizedstatus", "planscenarizedphrasestatus", "atstatus", "atbid", "atbudget", "bidprefer", "pcpricefactor", "inpeople", "remarketingPauseStat", "robotAPlanState"], "levelCond": {"userid": cookies_params['__cas__id__3']}, "pageNo": page, "pageSize": page_size, "startTime": today, "endTime": today})
  result = {'data' => [], 'count' => data['totalCount']}
  data['listData'].each do |item|
    result['data'] << {
        'id' => item['planid'],
        'name' => item['planname'],
        'pause' => item['pausestat'] == 1,
        'status' => '',
        'mobile_price_ratio' => item['mPriceFactor'],
    }
  end
  result
end

#request(params = {}) ⇒ Object

Raises:



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/baidu/tuiguang.rb', line 122

def request(params = {})
  params.merge!(
      "userid": cookies_params['__cas__id__3'],
      "token": cookies_params['__cas__st__3']
  )
  @params = params
  params[:"params"] = params[:"params"].to_json
  url = URI('http://fengchao.baidu.com/nirvana/request.ajax')
  url.query = URI.encode_www_form(path: params[:"path"])
  @url = url.to_s
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url)
  request["accept"] = '*/*'
  request["accept-encoding"] = 'gzip, deflate'
  request["accept-language"] = 'zh-CN,zh;q=0.8'
  request["cache-control"] = 'no-cache'
  request["connection"] = 'keep-alive'
  request["content-type"] = 'application/x-www-form-urlencoded'
  request["cookie"] = @cookies
  request["host"] = 'fengchao.baidu.com'
  request["origin"] = 'http://fengchao.baidu.com'
  request["pragma"] = 'no-cache'
  request["referer"] = "http://fengchao.baidu.com/nirvana/main.html?userid=#{cookies_params['__cas__id__3']}"
  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'
  request.body = URI.encode_www_form(params)
  response = http.request(request)
  raise RequestError, "HTTP Response: #{response.code} #{response.message}" unless response.is_a?(Net::HTTPSuccess)
  @res = response.read_body
  if response['Content-Encoding'] && response['Content-Encoding'].include?('gzip')
    @res = Zlib::GzipReader.new(StringIO.new(@res)).read
  end
  result = JSON.parse(@res)
  raise LoginExpired if result['redirect'] && result['redirecturl'] && result['redirecturl'].include?('http://cas.baidu.com')
  unless result['status'].to_i == 200
    if params[:"path"] == 'vega/MOD/keyword'
      raise Error.new result['error'].map {|k, v| v['winfoid']['message']}.join('; '), result['status']
    else
      raise Error.new result['error'], result['status']
    end
  end
  result['data']
end