bing_images 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/bing_images.rb +88 -0
  2. metadata +45 -0
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #-----------------------------------------------------------------------------#
4
+ # bing_images.rb #
5
+ # #
6
+ # Copyright (c) 2012, Rajiv Bakulesh Shah, original author. #
7
+ # #
8
+ # This file is free software; you can redistribute it and/or modify #
9
+ # it under the terms of the GNU General Public License as published #
10
+ # by the Free Software Foundation, either version 3 of the License, #
11
+ # or (at your option) any later version. #
12
+ # #
13
+ # This file is distributed in the hope that it will be useful, but #
14
+ # WITHOUT ANY WARRANTY; without even the implied warranty of #
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
16
+ # General Public License for more details. #
17
+ # #
18
+ # You should have received a copy of the GNU General Public License #
19
+ # along with this file. If not, see: #
20
+ # <http://www.gnu.org/licenses/>. #
21
+ #-----------------------------------------------------------------------------#
22
+
23
+
24
+ require 'addressable/uri'
25
+ require 'net/http'
26
+ require 'net/https'
27
+ require 'rexml/document'
28
+ require 'URI'
29
+
30
+
31
+ module BingImages
32
+ ACCOUNT_KEY = 'vwg49S0132p4mwVBpBL4p4GXTAhQyXU9PJoLnzpsXnE='
33
+ URL = 'https://api.datamarket.azure.com/Bing/Search/Image'
34
+
35
+ NUM_PAGES = 20
36
+ RESULTS_PER_PAGE = 50
37
+
38
+ def self.search(query, safe, offset)
39
+ query = build_query(query, safe, offset)
40
+ url = URL + '?' + query
41
+ xml = issue_request(url)
42
+ results = parse_xml(xml)
43
+ results
44
+ end
45
+
46
+ private
47
+
48
+ def self.build_query(query, safe, offset)
49
+ uri = Addressable::URI.new
50
+ uri.query_values = {
51
+ Query: "'" + query + "'",
52
+ Adult: "'" + (safe ? 'Moderate' : 'Off') + "'",
53
+ '$skip' => offset,
54
+ }
55
+ uri.query
56
+ end
57
+
58
+ def self.issue_request(url)
59
+ uri = URI(url)
60
+ http = Net::HTTP.new(uri.host, uri.port)
61
+ http.use_ssl = uri.scheme == 'https'
62
+ query = uri.query.nil? ? '' : ('?' + uri.query)
63
+ request = Net::HTTP::Get.new(uri.path + query)
64
+ request.basic_auth('', ACCOUNT_KEY)
65
+ response = http.request(request)
66
+ response.body
67
+ end
68
+
69
+ def self.parse_xml(xml)
70
+ doc = REXML::Document.new(xml)
71
+ results = []
72
+ doc.elements.each('feed/entry/content/m:properties') do |element|
73
+ result = {
74
+ thumbnail: (element.elements.each('d:Thumbnail/d:MediaUrl') {})[0].get_text,
75
+ full_size: (element.elements.each('d:MediaUrl') {})[0].get_text,
76
+ }
77
+ results << result
78
+ end
79
+ results
80
+ end
81
+ end
82
+
83
+
84
+ if __FILE__ == $0
85
+ query = ARGV.join(' ')
86
+ photos = BingImages.search(query, false, 0)
87
+ puts photos
88
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bing_images
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rajiv Bakulesh Shah
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-21 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A Ruby gem wrapper around Bing's image search.
15
+ email: brainix@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/bing_images.rb
21
+ homepage: http://rubygems.org/gems/bing_images
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.21
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Wrapper for Bing's Image Search
45
+ test_files: []