Class: PPC::API::Sogou::Bulk

Inherits:
PPC::API::Sogou show all
Defined in:
lib/ppc/api/sogou/bulk.rb

Constant Summary collapse

Service =
'AccountDownload'

Class Method Summary collapse

Methods inherited from PPC::API::Sogou

debug_print, process, request

Methods included from PPC::API

#make_type, #process, #request, #request_http_body, #request_http_header, #request_uri, #reverse_type

Class Method Details

.download(auth, ids = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/ppc/api/sogou/bulk.rb', line 30

def self.download( auth, ids = nil)
  result = get_all_object(auth, ids)
  field_id = result[:result]
  loop do 
    status = get_file_state(auth, field_id)
    return get_file_path(auth, field_id) if status[:result] == '1'
    sleep 15
  end
end

.download_cpc_rank(auth, device = 0) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ppc/api/sogou/bulk.rb', line 58

def self.download_cpc_rank(auth, device = 0)
  result = get_cpc_rank(auth, device)
  rank_id = result[:result]
  times = 0
  loop do 
    raise "The speed of get_status is too slow." if (times += 1) > 10
    status = get_cpc_rank_status(auth, rank_id)
    return get_cpc_rank_path(auth, rank_id) if status[:result] == '1'
    sleep 10
  end
end

.get_all_object(auth, ids) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/ppc/api/sogou/bulk.rb', line 10

def self.get_all_object( auth, ids )
  #文档上面写的输入类型是String?
  body = nil
  body = { 'cpcPlanIds' => ids } if ids
  response = request( auth, Service, 'getAccountFile', body )
  process( response, 'accountFileId' ){ |x| x }
end

.get_cpc_rank(auth, device) ⇒ Object



40
41
42
43
44
# File 'lib/ppc/api/sogou/bulk.rb', line 40

def self.get_cpc_rank(auth, device)
  body = {'deviceType' => device}
  response = request(auth, 'CpcRank', 'getCpcRankId', body)
  process(response, 'rankId'){|x| x}
end

.get_cpc_rank_path(auth, rank_id) ⇒ Object



52
53
54
55
56
# File 'lib/ppc/api/sogou/bulk.rb', line 52

def self.get_cpc_rank_path(auth, rank_id)
  body = {'rankId' => rank_id}
  response = request(auth, 'CpcRank', 'getCpcRankPath', body)
  process(response, 'rankPath'){|x| x}
end

.get_cpc_rank_status(auth, rank_id) ⇒ Object



46
47
48
49
50
# File 'lib/ppc/api/sogou/bulk.rb', line 46

def self.get_cpc_rank_status(auth, rank_id)
  body = {'rankId' => rank_id}
  response = request(auth, 'CpcRank', 'getCpcRankStatus', body)
  process(response, 'isGenerated' ){|x| x }
end

.get_file_path(auth, id) ⇒ Object



24
25
26
27
28
# File 'lib/ppc/api/sogou/bulk.rb', line 24

def self.get_file_path(auth, id)
  body = {'accountFileId' => id}
  response = request(auth, Service, 'getAccountFilePath', body)
  process(response, 'accountFilePath'){|x| x}
end

.get_file_state(auth, id) ⇒ Object



18
19
20
21
22
# File 'lib/ppc/api/sogou/bulk.rb', line 18

def self.get_file_state( auth, id )
  body = { 'accountFileId' => id }
  response = request( auth, Service, 'getAccountFileStatus' , body )
  process( response, 'isGenerated' ){ |x| x }
end

.rank_report(auth, device = 0) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ppc/api/sogou/bulk.rb', line 70

def self.rank_report(auth, device = 0 )
  url = download_cpc_rank(auth, device)[:result]
  file = "sogou_rank_#{Time.now.to_i}.zip"
  File.open(file, "w"){|f| f.puts open(url).read} 
  content = Zip::File.open(file) do |zip_file|
    entry = zip_file.glob('*.csv').first
    content = entry.get_input_stream.read.force_encoding('gb18030').encode('utf-8')
  end
  File.delete(file)
  content
end