Back to Parent

get "/search" do

  array_of_lines = IO.readlines("cloth_type.txt")
  $clothtype = array_of_lines.sample

  uri = URI('https://api.cognitive.microsoft.com/bing/v7.0/images/search')
  uri.query = URI.encode_www_form({
      # Request parameters
      'q' => $color1+$clothtype,
      'count' => '1',
      'offset' => '0',
      'mkt' => 'en-us',
      'safeSearch' => 'Moderate'
  })

  request = Net::HTTP::Get.new(uri.request_uri)
  # Request headers
  request['Ocp-Apim-Subscription-Key'] = '8e0a0e073d134d30ae956e78aebaf880'
  # Request body
  request.body = "{body}"

  response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
      http.request(request)
  end

  
  
  data = JSON.parse(response.body)
  
  $thumbnail1 = data["value"][0]["thumbnailUrl"]
  puts data["value"][0]["thumbnailUrl"]
  
  uri = URI('https://api.cognitive.microsoft.com/bing/v7.0/images/search')
  uri.query = URI.encode_www_form({
      # Request parameters
      'q' => $color2+$clothtype,
      'count' => '1',
      'offset' => '0',
      'mkt' => 'en-us',
      'safeSearch' => 'Moderate'
  })

  request = Net::HTTP::Get.new(uri.request_uri)
  # Request headers
  request['Ocp-Apim-Subscription-Key'] = '<key>'
  # Request body
  request.body = "{body}"

  response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
      http.request(request)
  end

  #puts response.body
  #response["page"].to_json
  #   homepage = response["homepage"]
  
  data = JSON.parse(response.body)
  
  $thumbnail2 = data["value"][0]["thumbnailUrl"]
  puts data["value"][0]["thumbnailUrl"]
  
  
end
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0