newsbing 1.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.
- checksums.yaml +7 -0
- data/README.md +42 -0
- data/lib/newsbing.rb +58 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9368470decd5c0e4bbe484e2f812ac51c9a28b34
|
4
|
+
data.tar.gz: 8396693d008b5461a03e7a6437c2f08df85be9a8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4b8106df333bc5fa0d06b78dea50a860a85b360a01764d566c470f1f829d08f1c3c8db51cd7f665fad52685108164f150895c4e0d817311e8d6d840e14e7fdf7
|
7
|
+
data.tar.gz: a923af428f963b17027cd4bf2b4cf50b51c4ecedf9caeb348c7feee7a701206d69e98f975e62f932ce98cbc61835e32d8058646c341f568207abc67e86f50ca2
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
A gem to sanely interact with the News portion of the Bing API. Based on the search bing gem.
|
2
|
+
-------------
|
3
|
+
find the [gem](https://rubygems.org/gems/newsbing) on rubygems.org
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
gem install newsbing
|
9
|
+
|
10
|
+
|
11
|
+
Configuration
|
12
|
+
-------------
|
13
|
+
An account key is needed to use the Bing Search API. You can create a new account for the Bing Search API and obtain account key [here](http://www.bing.com/developers/)
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
- this gem relies on the open-uri, net/http, and json gems.
|
18
|
+
|
19
|
+
- Bing News results are limited to 15 results at a time; this gem will use skip and top to get as many results as needed, note that for every 15 results your "top" parameter is, you Asure will "charge" you 1 API call.
|
20
|
+
|
21
|
+
Example: Interactive Ruby Shell
|
22
|
+
----------
|
23
|
+
require the gem in your shell session
|
24
|
+
|
25
|
+
require 'newsbing'
|
26
|
+
create a new search object, below 10 results are requested, you can retrieve up to 50 at a time
|
27
|
+
|
28
|
+
bing_image = Bing.new('your_account_key_goes_here', 10, 'Image')
|
29
|
+
retrieve the results for a given term
|
30
|
+
|
31
|
+
bing_results = bing_image.search("puffin")
|
32
|
+
|
33
|
+
or optionally specify an offset for your search, to start retrieving results from the starting point provided
|
34
|
+
|
35
|
+
bing_results = bing_image.search("puffin", 25)
|
36
|
+
|
37
|
+
parse the results
|
38
|
+
|
39
|
+
puts bing_results[0]["Thumbnail"]["MediaUrl"] # puts url of thumbnail
|
40
|
+
display the total number of rsults
|
41
|
+
|
42
|
+
puts bing_results[0]["ImageTotal"]
|
data/lib/newsbing.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'net/http'
|
4
|
+
# Before proceeding you will need an account key, which can be obtained by registering an accout at http://windows.microsoft.com/en-US/windows-live/sign-in-what-is-microsoft-account
|
5
|
+
class Newssearch
|
6
|
+
# Create a new object of the bing class
|
7
|
+
# >> bing = Bing.new('your_account_key_goes_here', 10)
|
8
|
+
# => #<Bing:0x9d9b9f4 @account_key="your_account_key", @num_results=10>
|
9
|
+
# Arguments:
|
10
|
+
# account_key: (String)
|
11
|
+
# num_results: (Integer)
|
12
|
+
|
13
|
+
def initialize(account_key, num_results)
|
14
|
+
@account_key = account_key
|
15
|
+
@num_results = num_results
|
16
|
+
end
|
17
|
+
|
18
|
+
attr_accessor :account_key, :num_results
|
19
|
+
|
20
|
+
def search(search_term )
|
21
|
+
results = []
|
22
|
+
offset = 15 # MS SPECIFIC CONSTANT
|
23
|
+
modulus = @num_results % offset
|
24
|
+
results = []
|
25
|
+
skip = 0
|
26
|
+
until @num_results < offset
|
27
|
+
results << hit_api(search_term, skip, offset)
|
28
|
+
@num_results = @num_results - offset
|
29
|
+
skip = skip + offset
|
30
|
+
end
|
31
|
+
results << hit_api(search_term, skip, modulus)
|
32
|
+
return results.flatten
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
def hit_api(search_term, skip, top)
|
40
|
+
user = ''
|
41
|
+
result_set = []
|
42
|
+
web_search_url = "https://api.datamarket.azure.com/Bing/Search/v1/News?"
|
43
|
+
query_string = '$format=json&Query='
|
44
|
+
query_portion = URI.encode_www_form_component('\'' + search_term + '\'')
|
45
|
+
params = "&$top=#{top.to_s}&$skip=#{skip}"
|
46
|
+
full_address = web_search_url + query_string + query_portion + params
|
47
|
+
uri = URI(full_address)
|
48
|
+
req = Net::HTTP::Get.new(uri.request_uri)
|
49
|
+
req.basic_auth user, account_key
|
50
|
+
|
51
|
+
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https'){|http|
|
52
|
+
http.request(req)
|
53
|
+
}
|
54
|
+
body = JSON.parse(res.body)
|
55
|
+
result_set = body["d"]["results"]
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: newsbing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Anthony
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: bing search api gem compatible with recent microsoft azure migration.
|
14
|
+
Find README.md with example usage at https://github.com/makerop/newsbing
|
15
|
+
email: anthony@makerops.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- README.md
|
21
|
+
- lib/newsbing.rb
|
22
|
+
homepage: https://github.com/makerops
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.0.3
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: compatible with the recent azure migration,use bing's search api in your
|
46
|
+
ruby app
|
47
|
+
test_files: []
|