baidu 1.2.10 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+ describe Baidu::SEM::KeywordService do
3
+ subject{Baidu::SEM::KeywordService.new($auth)}
4
+ let(:keyword_sample){'XJK测试关键词LZJ'}
5
+ it "#addKeyword #updateKeyword #getKeywordByKeywordId #deleteKeyword " do
6
+ #addKeyword
7
+ # puts $adgroup_id
8
+ keywordType = {:adgroupId => $adgroup_id,:keyword=>keyword_sample,:matchType => 1,:price => 1}
9
+ response = subject.addKeyword({:keywordTypes => [keywordType]})
10
+ response.status.should == 0
11
+ response.desc.should == 'success'
12
+ response.quota.should == 2
13
+ response.rquota.should > 0
14
+ keyword_id = response.body[:keyword_types][:keyword_id]
15
+
16
+ #updateKeyword
17
+ keywordType = {:keywordId=> keyword_id,:price => 2}
18
+ response = subject.updateKeyword({:keywordTypes=>[keywordType]})
19
+ response.status.should == 0
20
+ response.desc.should == 'success'
21
+ response.quota.should == 2
22
+ response.rquota.should > 0
23
+ # ap response.body
24
+
25
+ #getKeywordByKeywordId
26
+ sleep 3
27
+ response = subject.getKeywordByKeywordId({:keywordIds => [keyword_id]})
28
+ response.status.should == 0
29
+ response.desc.should == 'success'
30
+ response.quota.should == 2
31
+ response.rquota.should > 0
32
+ # ap response.body
33
+ keyword = response.body[:keyword_types]
34
+ (keyword.is_a?Hash).should == true
35
+ keyword[:keyword_id].should == keyword_id
36
+ keyword[:adgroup_id].should == $adgroup_id
37
+ keyword[:keyword].should == keyword_sample
38
+ keyword[:price].should == "2.0"
39
+
40
+ #deleteKeyword
41
+ response = subject.deleteKeyword({:keywordIds => [keyword_id]})
42
+ response.status.should == 0
43
+ response.desc.should == 'success'
44
+ response.quota.should == 2
45
+ response.rquota.should > 0
46
+ response.body[:result].should == '1'
47
+ end
48
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+ describe Baidu::SEM::ReportService do
3
+ subject{Baidu::SEM::ReportService.new($auth)}
4
+ before :each do
5
+ @options = {}
6
+ @options[:performanceData] = [Baidu::SEM::PerformanceData::IMPRESSION, Baidu::SEM::PerformanceData::CLICK, Baidu::SEM::PerformanceData::CPC]
7
+ @options[:startDate] = (Time.now - 24*3600).utc.iso8601
8
+ @options[:endDate] = Time.now.utc.iso8601
9
+ @options[:statIds] = $campaign_ids
10
+ @options[:levelOfDetails] = Baidu::SEM::LevelOfDetails::CAMPAIGN
11
+ @options[:reportType] = Baidu::SEM::ReportType::CAMPAIGN
12
+ end
13
+
14
+ # describe "ReportService#getProfessionalReportId" do
15
+ # @report_id = nil
16
+ it "返回的id必须是32位数字和小写字母组合" do
17
+ pending('need to rewrite')
18
+ response = subject.getProfessionalReportId({:reportRequestType => @options}).body
19
+ expect{ApiResponse.verify(response)}.not_to raise_error
20
+ # report_id.class.should == String
21
+ # report_id.should =~ /^[a-z0-9]{32}$/
22
+ end
23
+
24
+ it "处理未过期有效id(1小时内产生的)应返回ReportState中的枚举值" do
25
+ pending('need to rewrite')
26
+ response = subject.getProfessionalReportId({:reportRequestType => @options}).body
27
+ report_id = response[:report_id]
28
+ response = subject.getReportState({:reportId=>report_id}).body
29
+ expect{ApiResponse.verify(response)}.not_to raise_error
30
+ # report_state = response[:report_state]
31
+ # [Baidu::SEM::ReportState::PENDING,ReportState::DOING,ReportState::DONE].should include report_state
32
+ end
33
+
34
+ it "处理过期/无效id,应返回false" do
35
+ expect{subject.getReportState({:reportId=>'7da8438bc705c9e6e747959fafce91f2'}).body}.to raise_error
36
+ expect{subject.getReportState({:reportId=>'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'}).body}.to raise_error
37
+ end
38
+ end
39
+
40
+
41
+ # describe "ReportService#getReportState" do
42
+ #
43
+
44
+ # end
45
+ # describe "" do
46
+ # end
47
+ # describe "ReportService#getRealTimeQueryData" do
48
+ # end
49
+ # describe "ReportService#getRealTimePairData" do
50
+ # end
51
+ # describe "ReportService#getRealTimeData" do
52
+ # end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+ describe Baidu::SEM::SearchService do
3
+ subject{Baidu::SEM::SearchService.new($auth)}
4
+ describe '#getKeywordBySearch' do
5
+ it "cannot get any results when search for something that does't exists" do
6
+ respsonse = subject.getKeywordBySearch({:searchWord=>'things_that_not_exist!@$@%',:searchType=>0})
7
+ respsonse.status.should == 0
8
+ expect{ApiResponse.verify(respsonse.body)}.not_to raise_error
9
+ #@TODO
10
+ # ap respsonse
11
+ # res.more.should == 0
12
+ # res.results.should == nil
13
+
14
+ respsonse = subject.getKeywordBySearch({:searchWord=>'things_that_not_exist!@$@%',:searchType=>1})
15
+ #@TODO
16
+ # res.more.should == 0
17
+ # res.results.should == nil
18
+ expect{ApiResponse.verify(respsonse.body)}.not_to raise_error
19
+ # ap respsonse
20
+ end
21
+
22
+ it "should get some results when search for #{$searchWord}" do
23
+ respsonse = subject.getKeywordBySearch({:searchWord=>$searchWord,:searchType=>0})
24
+ expect{ApiResponse.verify(respsonse.body)}.not_to raise_error
25
+
26
+ # [0,1].should include res.more
27
+ # res.results.class.should == Array
28
+ # res.results.first.class.should == Hash
29
+ # res.results.first.keys.sort.should == [:keyword_id, :keyword, :adgroup_id,:adgroup_name, :campaign_id, :campaign_name].sort
30
+ end
31
+ end
32
+
33
+ describe '#getAdgroupBySearch' do
34
+ it "cannot get any adgroups when search for something that does't exists" do
35
+ options = {:adgroupName => 'things_that_not_exist!@#$',:searchType => 0}
36
+ respsonse = subject.getAdgroupBySearch(options)
37
+ # ap respsonse.body
38
+ expect{ApiResponse.verify(respsonse.body)}.not_to raise_error
39
+ # res.more.should == 0
40
+
41
+ options = {:adgroupName => 'things_that_not_exist!@#$',:searchType => 1}
42
+ respsonse = subject.getAdgroupBySearch(options)
43
+ expect{ApiResponse.verify(respsonse.body)}.not_to raise_error
44
+
45
+ # res.more.should == 0
46
+ end
47
+
48
+ it "should get some results when search for #{$searchAdgroup}" do
49
+ options = {:adgroupName => 'brand_e_air$',:searchType => 0}
50
+ respsonse = subject.getAdgroupBySearch(options)
51
+ expect{ApiResponse.verify(respsonse.body)}.not_to raise_error
52
+ # [0,1].should include res.more
53
+ # res.results.class.should == Hash
54
+ # res.results.keys.sort.should == [:adgroup_id, :adgroup_name, :campaign_id, :campaign_name].sort
55
+ end
56
+ end
57
+
58
+ describe '#getCampaignBySearch' do
59
+ it "cannot get any campaign when search for something that does't exists" do
60
+ options = {:campaignName => 'things_that_not_exist!@#$',:searchType => 0}
61
+ respsonse = subject.getCampaignBySearch(options)
62
+ expect{ApiResponse.verify(respsonse.body)}.not_to raise_error
63
+ # res.more.should == 0
64
+
65
+ options = {:campaignName => 'things_that_not_exist!@#$',:searchType => 1}
66
+ respsonse = subject.getCampaignBySearch(options)
67
+ expect{ApiResponse.verify(respsonse.body)}.not_to raise_error
68
+ # res.more.should == 0
69
+ end
70
+
71
+ it "should get some results when search for #{$searchCampaign}" do
72
+ options = {:campaignName => $searchCampaignName,:searchType => 0}
73
+ respsonse = subject.getCampaignBySearch(options)
74
+ expect{ApiResponse.verify(respsonse.body)}.not_to raise_error
75
+ # [0,1].should include res.more
76
+ # res.results.class.should == Hash
77
+ # res.results.keys.sort.should == [:campaign_id, :campaign_name].sort
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,356 @@
1
+ require 'baidu'
2
+ require 'time'
3
+ #请根据自己实际情况填写
4
+ BAIDU_MAP_KEY = '' #百度地图TOKEN
5
+ $searchWord = '' #根据Word查已投放关键词
6
+ $searchAdgroupName = '' #根据Adgroup查已投放的单元
7
+ $searchCampaignName = '' #根据Campaign查已投放的计划
8
+ $username = '' #账户 id=13
9
+ $password = '' #密码
10
+ $token = '' #token
11
+ $auth = Baidu::Auth.new
12
+ $auth.username = $username
13
+ $auth.password = $password
14
+ $auth.token = $token
15
+ $startDate = (Time.now - 24*3600).utc.iso8601
16
+ $endDate = Time.now.utc.iso8601
17
+ $pcDestinationUrl = 'http://www.elong.com/'
18
+ $mobileDestinationUrl = 'http://www.elong.com/'
19
+
20
+
21
+ cs = Baidu::SEM::CampaignService.new($auth)
22
+ campaigns = cs.getAllCampaign
23
+ campaign = campaigns.body[:campaign_types].first
24
+ $campaign_id = campaign[:campaign_id]
25
+ $campaign_name = campaign[:campaign_name]
26
+
27
+ ac = Baidu::SEM::AdgroupService.new($auth)
28
+ adgroups = ac.getAdgroupIdByCampaignId({:campaignIds=>[$campaign_id]})
29
+ $adgroup_id = adgroups.body[:campaign_adgroup_ids][:adgroup_ids].first
30
+
31
+ class ApiResponse
32
+ class << self
33
+ def verify(hash)
34
+ hash.each do |k,v|
35
+ k = k.to_s
36
+ next if k.start_with?'@xmlns'
37
+ send(k,v)
38
+ end
39
+ end
40
+ def changed_creative_ids(changed_creative_ids)
41
+ end
42
+ def creative_types(creative_types)
43
+ end
44
+ def result(result)
45
+ end
46
+
47
+ def campaign_types(campaign_types)
48
+ end
49
+ def money(money)
50
+ raise "money:#{money}" unless money =~ /\d+\.\d{2}/
51
+ end
52
+ alias :balance :money
53
+ alias :cost :money
54
+ alias :payment :money
55
+
56
+ def budget(budget)
57
+ end
58
+
59
+ alias :budget_type :budget
60
+
61
+ def region_target(region_target)
62
+ end
63
+
64
+ def open_domains(open_domains)
65
+ end
66
+
67
+ def reg_domain(reg_domain)
68
+ end
69
+
70
+ def account_info_type(account_info_type)
71
+ raise "account_info_type:#{account_info_type}" if %w(userid balance cost payment budget_type budget region_target open_domains reg_domain).any?{|key|!account_info_type.has_key?key.to_sym}
72
+ verify(account_info_type)
73
+ end
74
+
75
+ def scale(scale)
76
+ raise "scale:#{scale}" unless scale.size == 2 and scale.is_a?Array
77
+ end
78
+ alias :changed_campaign_scale :scale
79
+ alias :changed_adgroup_scale :scale
80
+ alias :changed_keyword_scale :scale
81
+ alias :changed_creative_scale :scale
82
+
83
+ def campaign_infos(campaign_infos)
84
+ if campaign_infos.is_a?Hash
85
+ raise "campaign_infos#{campaign_infos}" if %w(campaign_id campaign_name).any?{|key|!campaign_infos.has_key?key.to_sym}
86
+ verify(campaign_infos)
87
+ else
88
+ raise "campaign_infos#{campaign_infos}"
89
+ end
90
+ end
91
+
92
+ def campaign_adgroups(campaign_adgroups)
93
+ if campaign_adgroups.is_a?Array
94
+ campaign_adgroups.each do |campaign_adgroup|
95
+ campaign_adgroup(campaign_adgroup)
96
+ end
97
+ elsif campaign_adgroups.is_a?Hash
98
+ verify(campaign_adgroups)
99
+ else
100
+ raise "campaign_adgroups:#{campaign_adgroups}"
101
+ end
102
+ end
103
+
104
+ def campaign_adgroup(campaign_adgroup)
105
+ raise 'campaign_adgroup' if %w(campaign_id adgroup_types).any?{|key|!campaign_adgroup.has_key?key.to_sym}
106
+ verify(campaign_adgroup)
107
+ end
108
+
109
+ def exact_negative_words(exact_negative_words)
110
+ raise "exact_negative_words:#{exact_negative_words}" unless exact_negative_words.is_a?Array or exact_negative_words.is_a?String
111
+ end
112
+
113
+ def negative_words(negative_words)
114
+ raise "negative_words:#{negative_words}" unless negative_words.is_a?Array or negative_words.is_a?String
115
+ end
116
+
117
+ def adgroup_types(adgroup_types)
118
+ if adgroup_types.is_a?Array #multiple adgroup_types
119
+ adgroup_types.each do |adgroup_type|
120
+ adgroup_type(adgroup_type)
121
+ end
122
+ elsif adgroup_types.is_a?Hash #single adgroup_type
123
+ adgroup_type(adgroup_types)
124
+ else
125
+ raise "adgroup_types:#{adgroup_types}"
126
+ end
127
+ end
128
+
129
+ def campaign_adgroup_ids(campaign_adgroup_ids)
130
+ if campaign_adgroup_ids.is_a?Array
131
+ campaign_adgroup_ids.each do |campaign_adgroup_id|
132
+ campaign_adgroup_id(campaign_adgroup_id)
133
+ end
134
+ elsif campaign_adgroup_ids.is_a?Hash
135
+ campaign_adgroup_id(campaign_adgroup_ids)
136
+ else
137
+ raise "campaign_adgroup_ids:#{campaign_adgroup_ids}"
138
+ end
139
+ end
140
+
141
+ def campaign_adgroup_id(campaign_adgroup_id)
142
+ raise "campaign_adgroup_id:#{campaign_adgroup_id}" if %w(campaign_id adgroup_ids).any?{|key|!campaign_adgroup_id.has_key?key.to_sym}
143
+ verify(campaign_adgroup_id)
144
+ end
145
+
146
+ def adgroup_type(adgroup_type)
147
+ raise "adgroup_type:#{adgroup_type}" if %w(adgroup_id campaign_id adgroup_name max_price pause status).any?{|key|!adgroup_type.has_key?key.to_sym}
148
+ verify(adgroup_type)
149
+ end
150
+
151
+ def adgroup_ids(adgroup_ids)
152
+ if adgroup_ids.is_a? String
153
+ adgroup_id(adgroup_ids)
154
+ elsif adgroup_ids.is_a?Array
155
+ adgroup_ids.each do |adgroup_id|
156
+ adgroup_id(adgroup_id)
157
+ end
158
+ else
159
+ raise "adgroup_ids:#{adgroup_ids}"
160
+ end
161
+ end
162
+
163
+ def adgroup_infos(adgroup_infos)
164
+ if adgroup_infos.is_a?Array
165
+ adgroup_infos.each do |adgroup_info|
166
+ adgroup_info(adgroup_info)
167
+ end
168
+ elsif adgroup_infos.is_a?Hash
169
+ adgroup_info(adgroup_infos)
170
+ else
171
+ raise "adgroup_infos:#{adgroup_infos}"
172
+ end
173
+ end
174
+
175
+ def adgroup_info(adgroup_info)
176
+ raise "adgroup_info:#{adgroup_info}" if %w(adgroup_id adgroup_name campaign_id campaign_name).any?{|key|!adgroup_info.has_key?key.to_sym}
177
+ verify(adgroup_info)
178
+ end
179
+
180
+ def keyword_infos(keyword_infos)
181
+ if keyword_infos.is_a?Array
182
+ keyword_infos.each do |keyword_info|
183
+ keyword_info(keyword_info)
184
+ end
185
+ elsif keyword_infos.is_a?Hash
186
+ keyword_info(keyword_info)
187
+ else
188
+ raise "keyword_infos:#{keyword_infos}"
189
+ end
190
+ end
191
+
192
+ def keyword_info(keyword_info)
193
+ raise "keyword_info:#{keyword_info}" if %w(keyword_id keyword adgroup_id adgroup_name campaign_id campaign_name).any?{|key|!keyword_info.has_key?key.to_sym}
194
+ verify(keyword_info)
195
+ end
196
+
197
+ def id(id)
198
+ raise "id:#{id}" unless id =~ /\d+/
199
+ end
200
+ alias :campaign_id :id
201
+ alias :adgroup_id :id
202
+ alias :keyword_id :id
203
+ alias :item_id :id
204
+ alias :userid :id
205
+
206
+ def name(name)
207
+ raise "name:#{name}" unless name =~ /.+/
208
+ end
209
+
210
+ alias :campaign_name :name
211
+ alias :adgroup_name :name
212
+
213
+
214
+ def keyword(keyword)
215
+ raise "keyword:#{keyword}" unless keyword =~ /.+/
216
+ end
217
+
218
+ def more(more)
219
+ raise "more:#{more}" unless more == '1' or more == '0'
220
+ end
221
+
222
+ alias :more_word :more
223
+ alias :more_campaign :more
224
+ alias :more_adgroup :more
225
+
226
+ def max_price(max_price)
227
+ raise "max_price:#{max_price}" unless max_price.to_f.to_s == max_price
228
+ end
229
+
230
+ def pause(pause)
231
+ raise "pause:#{pause}" unless !!pause == pause
232
+ end
233
+
234
+ def status(status)
235
+ raise "status:#{status}" unless status =~ /\d+/
236
+ end
237
+
238
+ def file_id(file_id)
239
+ raise "file_id:#{file_id}" unless file_id =~ /[0-9a-z]{32}/
240
+ end
241
+ alias :report_id :file_id
242
+
243
+ def file_paths(file_paths)
244
+ if file_paths.is_a?Array
245
+ file_paths.each do |file_path|
246
+ file_path(file_path)
247
+ end
248
+ else
249
+ raise "file_paths:#{file_paths}"
250
+ end
251
+ end
252
+
253
+ def file_path(file_path)
254
+ raise "file_path:#{file_path}" unless URI.parse(file_path)
255
+ end
256
+
257
+ alias :account_file_path :file_path
258
+ alias :campaign_file_path :file_path
259
+ alias :adgroup_file_path :file_path
260
+ alias :keyword_file_path :file_path
261
+ alias :creative_file_path :file_path
262
+
263
+ def file_md5(file_md5)
264
+ raise "file_md5:#{file_md5}" unless file_md5 =~ /[0-9a-z]{32}/
265
+ end
266
+ alias :account_file_md5 :file_md5
267
+ alias :campaign_file_md5 :file_md5
268
+ alias :adgroup_file_md5 :file_md5
269
+ alias :keyword_file_md5 :file_md5
270
+ alias :creative_file_md5 :file_md5
271
+
272
+ def is_generated(is_generated)
273
+ raise "is_generated:#{is_generated}" unless is_generated =~ /\d+/
274
+ end
275
+
276
+ def end_time(end_time)
277
+ end_time.is_a?DateTime
278
+ end
279
+
280
+
281
+ def changed_campaign_ids(changed_campaign_ids)
282
+ if changed_campaign_ids.is_a?Array
283
+ changed_campaign_ids.each do |changed_campaign_id|
284
+ changed_campaign_id(changed_campaign_id)
285
+ end
286
+ elsif changed_campaign_ids.is_a?Hash
287
+ changed_campaign_id(changed_campaign_ids)
288
+ else
289
+ raise "changed_campaign_ids:#{changed_campaign_ids}"
290
+ end
291
+ end
292
+
293
+ def changed_campaign_id(changed_campaign_id)
294
+ raise "changed_campaign_id:#{changed_campaign_id}" unless changed_campaign_id.has_key?(:campaign_id) and changed_campaign_id.has_key?(:operator)
295
+ verify(changed_campaign_id)
296
+ end
297
+
298
+ def changed_adgroup_ids(changed_adgroup_ids)
299
+ if changed_adgroup_ids.is_a?Array
300
+ changed_adgroup_ids.each do |changed_adgroup_id|
301
+ changed_adgroup_id(changed_adgroup_id)
302
+ end
303
+ elsif changed_adgroup_ids.is_a?Hash
304
+ changed_adgroup_id(changed_adgroup_ids)
305
+ else
306
+ raise "changed_adgroup_ids:#{changed_adgroup_ids}"
307
+ end
308
+ end
309
+
310
+ def changed_adgroup_id(changed_adgroup_id)
311
+ raise "changed_adgroup_id:#{changed_adgroup_id}" unless changed_adgroup_id.has_key?(:adgroup_id) and changed_adgroup_id.has_key?(:operator)
312
+ verify(changed_adgroup_id)
313
+ end
314
+
315
+ def changed_keyword_ids(changed_keyword_ids)
316
+ if changed_keyword_ids.is_a?Array
317
+ changed_keyword_ids.each do |changed_keyword_id|
318
+ changed_keyword_id(changed_keyword_id)
319
+ end
320
+ elsif changed_keyword_ids.is_a?Hash
321
+ # p changed_keyword_ids
322
+ changed_keyword_id(changed_keyword_ids)
323
+ else
324
+ raise "changed_keyword_ids:#{changed_keyword_ids}"
325
+ end
326
+ end
327
+
328
+ def changed_keyword_id(changed_keyword_id)
329
+ raise "changed_keyword_id:#{changed_keyword_id}" if [:operator, :item_id, :campaign_id, :adgroup_id].any?{|key|!changed_keyword_id.has_key?key}
330
+ verify(changed_keyword_id)
331
+ end
332
+
333
+ def operator(operator)
334
+ raise "operator:#{operator}" unless operator =~ /\d+/
335
+ end
336
+
337
+ def response(response)
338
+ raise "response:#{response}" unless response == 'placeholder'
339
+ end
340
+ end
341
+ # def account_file_md5(file_md5)
342
+ # file_md5(file_md5)
343
+ # end
344
+ # def campaign_file_md5(file_md5)
345
+ # file_md5(file_md5)
346
+ # end
347
+ # def adgroup_file_md5(file_md5)
348
+ # file_md5(file_md5)
349
+ # end
350
+ # def keyword_file_md5(file_md5)
351
+ # file_md5(file_md5)
352
+ # end
353
+ # def creative_file_md5(file_md5)
354
+ # file_md5(file_md5)
355
+ # end
356
+ end