/adwords_api/examples/v201101/get_related_placements.rb

https://code.google.com/ · Ruby · 107 lines · 55 code · 14 blank · 38 comment · 4 complexity · 2bd58a18677b80f69314969f5ee06727 MD5 · raw file

  1. #!/usr/bin/ruby
  2. #
  3. # Author:: api.sgomes@gmail.com (SĂŠrgio Gomes)
  4. #
  5. # Copyright:: Copyright 2011, Google Inc. All Rights Reserved.
  6. #
  7. # License:: Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  16. # implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. #
  20. # This example retrieves URLs that have content keywords related to a given
  21. # website.
  22. #
  23. # Tags: TargetingIdeaService.get
  24. require 'rubygems'
  25. require 'adwords_api'
  26. require 'adwords_api/utils'
  27. API_VERSION = :v201101
  28. PAGE_SIZE = 100
  29. def get_related_placements()
  30. # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
  31. # when called without parameters.
  32. adwords = AdwordsApi::Api.new
  33. # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
  34. # the configuration file or provide your own logger:
  35. # adwords.logger = Logger.new('adwords_xml.log')
  36. targeting_idea_srv = adwords.service(:TargetingIdeaService, API_VERSION)
  37. url = 'INSERT_PLACEMENT_URL_HERE'
  38. # Construct selector.
  39. selector = {
  40. :idea_type => 'PLACEMENT',
  41. :request_type => 'IDEAS',
  42. :requested_attribute_types => ['CRITERION'],
  43. :search_parameters => [{
  44. # The 'xsi_type' field allows you to specify the xsi:type of the object
  45. # being created. It's only necessary when you must provide an explicit
  46. # type that the client library can't infer.
  47. :xsi_type => 'RelatedToUrlSearchParameter',
  48. :urls => [url],
  49. :include_sub_urls => false
  50. }],
  51. :paging => {
  52. :start_index => 0,
  53. :number_results => PAGE_SIZE
  54. }
  55. }
  56. # Define initial values.
  57. offset = 0
  58. results = []
  59. begin
  60. # Perform request.
  61. page = targeting_idea_srv.get(selector)
  62. results += page[:entries] if page and page[:entries]
  63. # Prepare next page request.
  64. offset += PAGE_SIZE
  65. selector[:paging][:start_index] = offset
  66. end while offset < page[:total_num_entries]
  67. # Display results.
  68. results.each do |result|
  69. data = AdwordsApi::Utils.map(result[:data])
  70. placement = data['CRITERION'][:value]
  71. puts "Related content keywords found at URL [%s]" % placement[:url]
  72. end
  73. puts "Total URLs found with keywords related to keywords at [%s]: %d." %
  74. [url, results.length]
  75. end
  76. if __FILE__ == $0
  77. begin
  78. get_related_placements()
  79. # HTTP errors.
  80. rescue AdsCommon::Errors::HttpError => e
  81. puts "HTTP Error: %s" % e
  82. # API errors.
  83. rescue AdwordsApi::Errors::ApiException => e
  84. puts "Message: %s" % e.message
  85. puts 'Errors:'
  86. e.errors.each_with_index do |error, index|
  87. puts "\tError [%d]:" % (index + 1)
  88. error.each do |field, value|
  89. puts "\t\t%s: %s" % [field, value]
  90. end
  91. end
  92. end
  93. end