baidu 1.2.10 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ module Baidu
2
+ class Auth
3
+ attr_accessor :username,:password,:token
4
+ end
5
+ end
@@ -0,0 +1,66 @@
1
+ require 'httparty'
2
+ module Baidu
3
+ class Map
4
+ include HTTParty
5
+ def initialize(key)
6
+ @key = key
7
+ @segments = {:ak=>@key,:output=>'json',:radius=>1000}
8
+ end
9
+
10
+ def bus
11
+ @segments[:query]='公交站'
12
+ self
13
+ end
14
+
15
+ def bank
16
+ @segments[:query]='银行'
17
+ self
18
+ end
19
+
20
+ def cafe
21
+ @segments[:query]='餐馆'
22
+ self
23
+ end
24
+
25
+ def spot
26
+ @segments[:query]='景点'
27
+ self
28
+ end
29
+
30
+ def around(lat,lng)
31
+ @segments[:location] = "#{lat},#{lng}"
32
+ self
33
+ end
34
+
35
+ def within(radius)
36
+ @segments[:radius] = radius
37
+ self
38
+ end
39
+
40
+ #得到百度地图城市id
41
+ def self.get_cityid(cityname)
42
+ body = get "http://api.map.baidu.com/?qt=cur&wd=#{URI.encode(cityname)}&ie=utf-8&res=api"
43
+ return body['content']['code']
44
+ end
45
+
46
+ #从每条结果中解析x,y坐标
47
+ def self.coordinate(poiname,cityid)
48
+ # puts "http://api.map.baidu.com/geocoder?address=#{URI.encode(poiname)}&output=json&key=#{@key}&city=#{URI.encode(cityid)}"
49
+ url = "http://api.map.baidu.com/geocoder?address=#{URI.encode(poiname)}&output=json&key=#{@key}&city=#{cityid}"
50
+ result = get url
51
+ location = result['result']
52
+ return nil if location.nil? or location.empty?
53
+ location = location['location']
54
+ return location['lng'],location['lat'] if result['status'] == 'OK'
55
+ end
56
+
57
+ def info
58
+ uri = URI::HTTP.build(
59
+ :host => 'api.map.baidu.com',
60
+ :query=> URI.encode(@segments.map{|k,v|"#{k}=#{v}"}.join('&')),
61
+ :path => '/place/v2/search'
62
+ )
63
+ self.class.get(uri.to_s)
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,5 @@
1
+ module Baidu
2
+ module Rank
3
+
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ require "baidu/sem/enum"
2
+ require "baidu/sem/base"
3
+ require "baidu/sem/report"
4
+ require "baidu/sem/account"
5
+ require "baidu/sem/adgroup"
6
+ require "baidu/sem/bulk"
7
+ require "baidu/sem/campaign"
8
+ require "baidu/sem/creative"
9
+ require "baidu/sem/kr"
10
+ require "baidu/sem/keyword"
11
+ require "baidu/sem/new_creative"
12
+ require "baidu/sem/search"
13
+ # require "baidu/ext"
14
+ require "savon"
@@ -0,0 +1,31 @@
1
+ module Baidu
2
+ module SEM
3
+ class AccountService < Base
4
+ # def getAccountInfo
5
+ # operation = make_operation('getAccountInfo')
6
+ # operation.header = operation_header
7
+ # operation.body = {
8
+ # :getAccountInfoRequest=>{
9
+ # :requestData=>[]
10
+ # }
11
+ # }
12
+
13
+ # response = operation.call
14
+ # if @debug
15
+ # puts operation.build
16
+ # end
17
+ # response.body
18
+ # end
19
+
20
+ # def updateAccountInfo(accountInfoType)
21
+ # operation = make_operation('updateAccountInfo')
22
+ # operation.header = operation_header
23
+ # operation.body = {
24
+ # :updateAccountInfoRequest =>{
25
+ # :accountInfoType => accountInfoType
26
+ # }
27
+ # }
28
+ # end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,55 @@
1
+ module Baidu
2
+ module SEM
3
+ class AdgroupService < Base
4
+ # def getAllAdgroupId
5
+ # operation = make_operation('getAllAdgroupId')
6
+ # operation.header = operation_header
7
+ # operation.body = {
8
+ # :getAllAdgroupIdRequest=>{}
9
+ # }
10
+ # if @debug
11
+ # puts operation.build
12
+ # end
13
+ # end
14
+ # def getAdgroupIdByCampaignId(id)
15
+ # operation = make_operation('getAdgroupIdByCampaignId')
16
+ # operation.header = operation_header
17
+ # operation.body ={
18
+ # :getAdgroupIdByCampaignIdRequest=>{
19
+ # :campaignIds=>id.to_i
20
+ # }
21
+ # }
22
+ # if @debug
23
+ # puts operation.build
24
+ # end
25
+ # end
26
+ # def getAdgroupByCampaignId(id)
27
+ # operation = make_operation('getAdgroupByCampaignId')
28
+ # operation.header = operation_header
29
+ # operation.body ={
30
+ # :getAdgroupByCampaignIdRequest=>{
31
+ # :campaignIds=>id.to_i
32
+ # }
33
+ # }
34
+ # if @debug
35
+ # puts operation.build
36
+ # end
37
+ # end
38
+ # def getAdgroupByAdgroupId
39
+ # operation = make_operation('getAdgroupByAdgroupId')
40
+ # operation.header = operation_header
41
+ # operation.body = {
42
+ # :getAdgroupByAdgroupIdRequest=>{
43
+ # :adgroupIds=>["long"]
44
+ # }
45
+ # }
46
+ # end
47
+ # def addAdgroup
48
+ # end
49
+ # def updateAdgroup
50
+ # end
51
+ # def deleteAdgroup
52
+ # end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,96 @@
1
+ module Baidu
2
+ module SEM
3
+ class Base
4
+ attr_accessor :username,:password,:token
5
+ attr_accessor :debug
6
+
7
+ def initialize(auth)
8
+ classname = self.class.name.split('::').last
9
+ @service_name = classname
10
+ @port_name = classname
11
+ @username = auth.username
12
+ @password = auth.password
13
+ @token = auth.token
14
+ @client = Savon.new("https://api.baidu.com/sem/sms/v3/#{classname}?wsdl")
15
+ # Savon.new(@base_uri+service_name+'?wsdl')
16
+ end
17
+ def method_missing(name, *args, &block)
18
+ options,debug = args[0],args[1]
19
+ options = {} if options.nil?
20
+ name = name.to_s
21
+ name_snake = name.snake_case
22
+ # p name
23
+ name_tmp =
24
+ case name
25
+ when 'getAllCampaign'
26
+ 'GetAllCampaign'
27
+ when 'getCampaignByCampaignId'
28
+ 'GetCampaignByCampaignId'
29
+ when 'addCampaign'
30
+ 'AddCampaign'
31
+ when 'updateCampaign'
32
+ 'UpdateCampaign'
33
+ when 'deleteCampaign'
34
+ 'DeleteCampaign'
35
+ when 'getCampaignByCampaignId'
36
+ 'GetCampaignByCampaignId'
37
+ else
38
+ name
39
+ end
40
+
41
+ name_request_sym = (name_tmp+'Request').to_sym #if %w(getCampaignByCampaignId getAllCampaign addCampaign updateCampaign deleteCampaign).include?name
42
+ # puts name_request_sym
43
+ name_response_sym = (name+'Response').snake_case.to_sym
44
+ operation = make_operation(name)
45
+ operation.header = operation_header
46
+ operation.body = {
47
+ name_request_sym => options
48
+ }
49
+ ap operation.body if debug
50
+ puts operation.build if debug
51
+ response = operation.call
52
+ ap response if debug
53
+ # ap response.failures if debug
54
+ if response.failures
55
+ raise response.failures.to_s
56
+ else
57
+ Baidu::Response.new(response,name_response_sym)
58
+ end
59
+ end
60
+ def operations
61
+ @client.operations(@service_name,@port_name)
62
+ end
63
+
64
+ def make_operation(operation_name)
65
+ @client.operation(@service_name,@port_name,operation_name)
66
+ end
67
+
68
+ def operation_header
69
+ {
70
+ :AuthHeader=>
71
+ {
72
+ :username=>@username,
73
+ :password=>@password,
74
+ :token=>@token
75
+ }
76
+ }
77
+ end
78
+
79
+ def example(operation,with_header=false)
80
+ operation = make_operation(operation)
81
+ if with_header
82
+ {
83
+ :example_header => operation.example_header,
84
+ :example_body => operation.example_body
85
+ }
86
+ else
87
+ operation.example_body
88
+ end
89
+ end
90
+
91
+ # def invalid_options?(options,necessary_options)
92
+ # return true if necessary_options.any?{|necessary_option|!options.has_key?necessary_option}
93
+ # end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,75 @@
1
+ module Baidu
2
+ module SEM
3
+ class BulkJobService < Base
4
+ # class getChangedCampaignIdResponse
5
+ # attr_accessor :endTime, :changedCampaignIds
6
+ # end
7
+ # class getChangedIdResponse
8
+ # attr_accessor :endTime, :changedCampaignIds,:changedAdgroupIds,:changedKeywordIds,:changedCreativeIds,:changedNewCreativeIds
9
+ # end
10
+ # def method_missing(name, *args, &block)
11
+ # name = name.to_s
12
+ # name_snake = name.snake_case
13
+ # name_request_sym = (name+'Request').to_sym
14
+ # name_response_sym = (name+'Response').snake_case.to_sym
15
+ # operation = make_operation(name)
16
+ # operation.header = operation_header
17
+ # operation.body = {
18
+ # name_request_sym => args[0]
19
+ # }
20
+ # puts operation.build if @debug
21
+ # response = operation.call
22
+ # response = response.body[name_response_sym]
23
+ # response
24
+ # end
25
+ # def getChangedCampaignId(options)
26
+ # operation = make_operation('getChangedCampaignId')
27
+ # operation.header = operation_header
28
+ # operation.body = {
29
+ # :getChangedCampaignIdRequest=>options
30
+ # }
31
+ # if @debug
32
+ # puts operation.build
33
+ # end
34
+ # response = operation.call
35
+ # response = response.body[:get_changed_campaign_id_response]
36
+ # r = getChangedCampaignIdResponse.new
37
+ # r.endTime = response[:endTime]
38
+ # r.changedCampaignIds = response[:changedCampaignIds]
39
+ # r
40
+ # end
41
+ # def getChangedId(startTime)
42
+ # operation = make_operation('getChangedId')
43
+ # operation.header = operation_header
44
+ # operation.body = {:getChangedIdRequest=>{:startTime=>startTime}}
45
+ # response = operation.call
46
+ # r = getChangedIdResponse.new
47
+ # r.endTime = response[:endTime]
48
+ # r.changedCampaignIds = response[:changedCampaignIds]
49
+ # r.changedAdgroupIds = response[:changedAdgroupIds]
50
+ # r.changedKeywordIds = response[:changedKeywordIds]
51
+ # r.changedCreativeIds = response[:changedCreativeIds]
52
+ # r.changedNewCreativeIds = response[:changedNewCreativeIds]
53
+ # r
54
+ # end
55
+ # def getChangedNewCreativeId
56
+ # end
57
+ # def getChangedScale
58
+ # end
59
+ # def getAllObjects
60
+ # end
61
+ # def getAllChangedObjects
62
+ # end
63
+ # def getFileState
64
+ # end
65
+ # def getFilePath
66
+ # end
67
+ # def getChangedAdgroupId
68
+ # end
69
+ # def getChangedItemId
70
+ # end
71
+ # def getSelectedObjects
72
+ # end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,96 @@
1
+ module Baidu
2
+ module SEM
3
+ class CampaignService < Base
4
+ # def addCampaign(options)
5
+ # operation = make_operation('addCampaign')
6
+ # operation.header = operation_header
7
+ # operation.body = {
8
+ # :AddCampaignRequest=>{
9
+ # :campaignTypes=> options
10
+ # }
11
+ # }
12
+ # if @debug
13
+ # puts operation.build
14
+ # end
15
+ # end
16
+
17
+ # def updateCampaign(options)
18
+ # operation = make_operation('updateCampaign')
19
+ # operation.header = operation_header
20
+ # operation.body = {
21
+ # :UpdateCampaignRequest=> {
22
+ # :campaignTypes => options
23
+ # }
24
+ # }
25
+ # if @debug
26
+ # puts operation.build
27
+ # end
28
+ # end
29
+
30
+ # def getCampaignByCampaignId(id)
31
+ # operation = make_operation('getCampaignByCampaignId')
32
+ # operation.header = operation_header
33
+ # operation.body = {
34
+ # :GetCampaignByCampaignIdRequest=>{:campaignIds=>id.to_i}
35
+ # }
36
+ # if @debug
37
+ # puts operation.build
38
+ # end
39
+ # response = operation.call
40
+ # if @debug
41
+ # p response.hash
42
+ # end
43
+ # end
44
+
45
+ # def getAllCampaign
46
+ # operation = make_operation('getAllCampaign')
47
+ # p operation_header
48
+ # operation.header = operation_header
49
+ # operation.body = {
50
+ # :GetAllCampaignRequest => {}
51
+ # }
52
+
53
+ # puts operation.build if @debug
54
+
55
+ # response = operation.call
56
+
57
+ # p response.hash if @debug
58
+
59
+ # response = response.body[:GetAllCampaignResponse]
60
+ # puts response.class
61
+ # end
62
+
63
+ # def getAllCampaignId
64
+ # warn 'this API may not be ready yet'
65
+ # operation = make_operation('getAllCampaignId')
66
+ # operation.header = operation_header
67
+ # operation.body = {
68
+ # :getAllCampaignIdRequest => {}
69
+ # }
70
+ # if @debug
71
+ # puts operation.build
72
+ # end
73
+ # response = operation.call
74
+ # if @debug
75
+ # p response.hash
76
+ # end
77
+ # response = response.body[:addCampaignResponse]
78
+ # end
79
+
80
+ # def deleteCampaign(id)
81
+ # operation = make_operation('deleteCampaign')
82
+ # operation.header = operation_header
83
+ # operation.body = {
84
+ # :DeleteCampaignRequest=>{:campaignIds=>id.to_i}
85
+ # }
86
+ # if @debug
87
+ # puts operation.build
88
+ # end
89
+ # response = operation.call
90
+ # if @debug
91
+ # p response.hash
92
+ # end
93
+ # end
94
+ end
95
+ end
96
+ end