scrapinghub 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+
2
+ test.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in scrapinghub.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Emil Ahlbäck
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Scrapinghub
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'scrapinghub'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install scrapinghub
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b feature/my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin feature/my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ require 'net/http'
2
+
3
+ require_relative 'scrapinghub/api_method'
4
+ require_relative 'scrapinghub/scrapinghub'
5
+
6
+ module Scrapinghub
7
+ end
8
+
@@ -0,0 +1,31 @@
1
+ module Scrapinghub
2
+ class ApiMethod
3
+ def initialize(location, requires={}, format = :json)
4
+ @location = "#{location}.#{format.to_s}"
5
+ @requires = requires
6
+ end
7
+
8
+ def build(base_url, parameters = {})
9
+ @parameters = parameters
10
+
11
+ build_params!
12
+ build_uri!(base_url)
13
+
14
+ @uri
15
+ end
16
+
17
+ def build_params!
18
+ @requires.each do |required_parameter|
19
+ unless @parameters[required_parameter]
20
+ raise ArgumentError.new "#{required_parameter} is required to access #{@location}"
21
+ end
22
+ end
23
+ end
24
+
25
+ def build_uri!(base_url)
26
+ @uri = URI( File.join(base_url, @location) )
27
+ @uri.query = URI.encode_www_form(@parameters)
28
+ end
29
+ end
30
+ end
31
+
@@ -0,0 +1,80 @@
1
+ require_relative 'api_method'
2
+
3
+ module Scrapinghub
4
+ class Scrapinghub
5
+ METHODS = {
6
+ projects: ApiMethod.new('scrapyd/listprojects', []),
7
+ spiders: ApiMethod.new('spiders/list', [:project]),
8
+ jobs: ApiMethod.new('jobs/list', [:project]),
9
+ job: ApiMethod.new('jobs/list', [:project, :job]),
10
+ job_items: ApiMethod.new('items', [:project, :job]),
11
+ spider_items: ApiMethod.new('items', [:project, :spider])
12
+ }
13
+
14
+ attr_reader :api_key
15
+
16
+ def initialize(api_key, url='http://panel.scrapinghub.com/api/')
17
+ @api_key = api_key
18
+ @base_url = url
19
+ end
20
+
21
+ def get(method, parameters = {})
22
+ if method.is_a? Symbol
23
+ uri = get_method_url(method, parameters)
24
+ else
25
+ uri = build_url(method, parameters)
26
+ end
27
+
28
+ fetch(uri)
29
+ end
30
+
31
+ def fetch(uri, redirect_limit = 5)
32
+ if redirect_limit <= 0
33
+ raise "Request redirected too many times."
34
+ end
35
+
36
+ Net::HTTP.start(uri.host, uri.port) do |http|
37
+ request = Net::HTTP::Get.new(uri.request_uri)
38
+ request.basic_auth @api_key, ''
39
+
40
+ response = http.request(request)
41
+
42
+ case response
43
+ when Net::HTTPFound
44
+ new_location = URI(response['location'])
45
+
46
+ debug("<- #{uri.request_uri} redirects to")
47
+ debug("-> #{new_location.request_uri}")
48
+
49
+ fetch(new_location, redirect_limit-1)
50
+ else
51
+ response
52
+ end
53
+ end
54
+ end
55
+
56
+ def method_missing(method, *args, &block)
57
+ if METHODS[method]
58
+ get(method, args)
59
+ else
60
+ super
61
+ end
62
+ end
63
+
64
+ private
65
+ def build_url(api_method, parameters)
66
+ api_method.build(@base_url, parameters)
67
+ end
68
+
69
+ def get_method_url(method, parameters)
70
+ api_method = METHODS[method]
71
+
72
+ return build_url(api_method, parameters)
73
+ end
74
+
75
+ def debug(message)
76
+ puts message
77
+ end
78
+ end
79
+ end
80
+
@@ -0,0 +1,3 @@
1
+ module Scrapinghub
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'scrapinghub/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "scrapinghub"
8
+ gem.version = Scrapinghub::VERSION
9
+ gem.authors = ["Emil Ahlback"]
10
+ gem.email = ["e.ahlback@gmail.com"]
11
+ gem.description = %q{Simple interface to Scrapinghub's API}
12
+ gem.summary = %q{Just a Ruby wrapper for the Scrapinghub API, see docs at: http://help.scrapinghub.com/api.html}
13
+ gem.homepage = "https://github.com/mull/scrapinghub"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scrapinghub
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Emil Ahlback
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Simple interface to Scrapinghub's API
15
+ email:
16
+ - e.ahlback@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/scrapinghub.rb
27
+ - lib/scrapinghub/api_method.rb
28
+ - lib/scrapinghub/scrapinghub.rb
29
+ - lib/scrapinghub/version.rb
30
+ - scrapinghub.gemspec
31
+ homepage: https://github.com/mull/scrapinghub
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.24
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: ! 'Just a Ruby wrapper for the Scrapinghub API, see docs at: http://help.scrapinghub.com/api.html'
55
+ test_files: []