newznab-api 0.1.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
- checksums.yaml.gz.sig +3 -0
- data.tar.gz.sig +0 -0
- data/.env.example +9 -0
- data/.gitignore +10 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +61 -0
- data/Rakefile +38 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/certs/homans.pem +21 -0
- data/lib/newznab/api.rb +311 -0
- data/lib/newznab/api/item.rb +91 -0
- data/lib/newznab/api/list.rb +139 -0
- data/lib/newznab/api/search.rb +193 -0
- data/lib/newznab/api/version.rb +6 -0
- data/newznab-api.gemspec +42 -0
- metadata +288 -0
- metadata.gz.sig +1 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 604369344c3d1cda367e67905cadc85a82068c10
|
4
|
+
data.tar.gz: 35ee7aea3ce8261d4c42b59ffe001166e4acc719
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c01556b2eb6f41e1e6cdaccf8e76731d536832b0357322dfbc2220e14a52bd8cdcd944bbd8902bbc8043706083f4023cba4907a75de8440eeac4ad1d161bd56e
|
7
|
+
data.tar.gz: 4dd02ab5407e28bf36a8325d019d650442c4df70dbe59be868e2ac4bdf800cef2dd552beed219bb7df616dedd2a395f6a02310e83a5602938acc63dfc944c416
|
checksums.yaml.gz.sig
ADDED
data.tar.gz.sig
ADDED
Binary file
|
data/.env.example
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# Add account credentials and API keys here.
|
2
|
+
# This file should be listed in .gitignore to keep your settings secret!
|
3
|
+
# Each entry sets a local environment variable.
|
4
|
+
# For example, setting:
|
5
|
+
# GMAIL_USERNAME=Your_Gmail_Username
|
6
|
+
# makes 'Your_Gmail_Username' available as ENV["GMAIL_USERNAME"]
|
7
|
+
|
8
|
+
NEWZNAB_URI=Your_Newznab_URI
|
9
|
+
NEWZNAB_API_KEY=Your_Newznab_API_Key
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at holden.omans@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Holden Omans
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# Newznab::Api
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/newznab/api`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'newznab-api'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install newznab-api
|
22
|
+
|
23
|
+
Then set a environmental variables named `NEWZNAB_API_KEY` and `NEWZNAB_URI` to auto connect **(optional)**
|
24
|
+
|
25
|
+
$ export NEWZNAB_URI=https://api.newznab.com
|
26
|
+
$ export NEWZNAB_API_KEY=7e8896464895c1cde33759b8307f5cf8
|
27
|
+
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
If you did not set the env variables `NEWZNAB_API_KEY` or `NEWZNAB_URI` then you need to set the class `api_key` and `uri` before using. If you did set the env variable you do not need to set this.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
Newznab::Api.new(uri: 'https://api.newznab.com', key: '7e8896464895c1cde33759b8307f5cf8')
|
35
|
+
```
|
36
|
+
|
37
|
+
|
38
|
+
Perform a search:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
resp = newznab.search(query: 'This Old House', limit: 50)
|
42
|
+
```
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
## Development
|
48
|
+
|
49
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
50
|
+
|
51
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kalinon/newznab-api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
56
|
+
|
57
|
+
|
58
|
+
## License
|
59
|
+
|
60
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
61
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'yard'
|
4
|
+
require 'dotenv/load'
|
5
|
+
|
6
|
+
# Test tasks
|
7
|
+
Rake::TestTask.new(:test) do |t|
|
8
|
+
t.libs << 'test'
|
9
|
+
t.libs << 'lib'
|
10
|
+
t.test_files = FileList['test/**/*_test.rb']
|
11
|
+
t.warning = false
|
12
|
+
end
|
13
|
+
|
14
|
+
YARD::Rake::YardocTask.new do |t|
|
15
|
+
t.files = ['lib/**/*.rb'] # optional
|
16
|
+
t.options = %w{--private} # optional
|
17
|
+
t.stats_options = ['--list-undoc'] # optional
|
18
|
+
end
|
19
|
+
|
20
|
+
# Default task
|
21
|
+
task :default => :test
|
22
|
+
|
23
|
+
Rake::Task['build'].enhance do
|
24
|
+
require 'digest/sha2'
|
25
|
+
built_gem_path = 'pkg/newznab-api-'+Newznab::Api::VERSION+'.gem'
|
26
|
+
checksum = Digest::SHA256.new.hexdigest(File.read(built_gem_path))
|
27
|
+
checksum_path = 'checksum/newznab-api-'+Newznab::Api::VERSION+'.gem.sha256'
|
28
|
+
File.open(checksum_path, 'w') { |f| f.write(checksum) }
|
29
|
+
end
|
30
|
+
|
31
|
+
task :console do
|
32
|
+
require 'irb'
|
33
|
+
require 'irb/completion'
|
34
|
+
require 'newznab/api'
|
35
|
+
|
36
|
+
ARGV.clear
|
37
|
+
IRB.start
|
38
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'newznab/api'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/certs/homans.pem
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
-----BEGIN CERTIFICATE-----
|
2
|
+
MIIDgDCCAmigAwIBAgIBATANBgkqhkiG9w0BAQUFADBDMRUwEwYDVQQDDAxob2xk
|
3
|
+
ZW4ub21hbnMxFTATBgoJkiaJk/IsZAEZFgVnbWFpbDETMBEGCgmSJomT8ixkARkW
|
4
|
+
A2NvbTAeFw0xNzA0MDkyMTAwMTJaFw0xODA0MDkyMTAwMTJaMEMxFTATBgNVBAMM
|
5
|
+
DGhvbGRlbi5vbWFuczEVMBMGCgmSJomT8ixkARkWBWdtYWlsMRMwEQYKCZImiZPy
|
6
|
+
LGQBGRYDY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyNPYv6sC
|
7
|
+
h/gLJ4XG0QdPJUEtvg8AwPLHz/BHLHKJPvq6Nyh2UDiWy+ZyP+uioBAxieHrMA4m
|
8
|
+
2v60BLIYWQyl2jPK2Rc2kchrciskFNy7wQMvi59a6+FFBMwdrl55stgJQOGorKSA
|
9
|
+
B/w/WYOnTWLzA6NuoRoA0JU1mWzKPCYn8HZLX88y5LzKZ6011K2zeh19MJDngaGU
|
10
|
+
EjOavcgNsNVGdlTToyz4P0v8kyXbRdKsBM4On7bN59ujHSOVdUr//Ze1IJj3dtew
|
11
|
+
bcefgVhmX+TtxjtKo1+V5Nt1YzQ3omiEbVt6I8yIuY87CDk8KJiXjzCR1iiNu8YF
|
12
|
+
zBm0vYm6tuRCKQIDAQABo38wfTAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNV
|
13
|
+
HQ4EFgQUKuIxpvKKtDKrTwoV2OTimRBnu1YwIQYDVR0RBBowGIEWaG9sZGVuLm9t
|
14
|
+
YW5zQGdtYWlsLmNvbTAhBgNVHRIEGjAYgRZob2xkZW4ub21hbnNAZ21haWwuY29t
|
15
|
+
MA0GCSqGSIb3DQEBBQUAA4IBAQADMSGgLnyWg3FkyFsf0p5Y/yeN3giPIMjc4pK8
|
16
|
+
im1nsfZRotJvkWgobP17tkSK4YPYSlIqf9O5hCZlHHG8aGfy+L4ax9w23QOa9oJm
|
17
|
+
90OnzyR7G7CCXBOhjYXX5YsQbxBSbNwQQxLjjuOXe1HvgAKNCwMEI2Gj+5ByMMn3
|
18
|
+
uq/WPwQKccVm0Ado75I+1IiIpdJjWfOMt5KBxS/JCpE8hRxc3viXdTvN3vSUZ7q7
|
19
|
+
yFGnn1/BkVPEZOvDj9bIANBVtWP0hebPDM/v7B7Lz+SSfqSaWGAwi+XoDe2kN0Ii
|
20
|
+
LHXORkT9cmA6WykguTkGcDw9YiV/I7VMHzYyurD2x+nOXmla
|
21
|
+
-----END CERTIFICATE-----
|
data/lib/newznab/api.rb
ADDED
@@ -0,0 +1,311 @@
|
|
1
|
+
require 'newznab/api/version'
|
2
|
+
require 'newznab/api/list'
|
3
|
+
require 'newznab/api/item'
|
4
|
+
require 'newznab/api/search'
|
5
|
+
require 'rest-client'
|
6
|
+
require 'cgi'
|
7
|
+
require 'json'
|
8
|
+
require 'mono_logger'
|
9
|
+
require 'open-uri'
|
10
|
+
require 'nokogiri'
|
11
|
+
|
12
|
+
## Yard Doc generation stuff
|
13
|
+
# @!macro [new] raise.FunctionNotSupportedError
|
14
|
+
# @raise [FunctionNotSupportedError] indicating the resource requested is not supported
|
15
|
+
# @!macro [new] raise.FunctionDisabledError
|
16
|
+
# @raise [FunctionDisabledError] indicating the server api function is disabled
|
17
|
+
# @!macro [new] raise.NewznabAPIError
|
18
|
+
# @raise [NewznabAPIError] indicating the api request code received
|
19
|
+
|
20
|
+
##
|
21
|
+
# Base Newznab module
|
22
|
+
module Newznab
|
23
|
+
|
24
|
+
##
|
25
|
+
# Class to interact and query the Newznab API
|
26
|
+
module Api
|
27
|
+
|
28
|
+
# Response format from Newznab api
|
29
|
+
# @since 0.1.0
|
30
|
+
API_FORMAT = 'json'
|
31
|
+
|
32
|
+
# Supported Newznab api functions
|
33
|
+
# @since 0.1.0
|
34
|
+
API_FUNCTIONS = [:caps, :search, :tvsearch, :movie, :music, :book, :details, :getnfo]
|
35
|
+
|
36
|
+
|
37
|
+
##
|
38
|
+
# Raised when a Newznab API error is encountered
|
39
|
+
#
|
40
|
+
# @since 0.1.0
|
41
|
+
class NewznabAPIError < ScriptError
|
42
|
+
end
|
43
|
+
|
44
|
+
##
|
45
|
+
# Raised when a function is not implemented on the current API
|
46
|
+
#
|
47
|
+
# Must be included in {Newznab::Api::API_FUNCTIONS}
|
48
|
+
#
|
49
|
+
# @since 0.1.0
|
50
|
+
class FunctionNotSupportedError < NewznabAPIError
|
51
|
+
end
|
52
|
+
|
53
|
+
##
|
54
|
+
# Raised when a Newznab API function is disabled
|
55
|
+
#
|
56
|
+
# @since 0.1.0
|
57
|
+
class FunctionDisabledError < FunctionNotSupportedError
|
58
|
+
end
|
59
|
+
|
60
|
+
class << self
|
61
|
+
include Newznab::Api::Search
|
62
|
+
|
63
|
+
attr_accessor :api_uri, :api_key, :api_timeout, :api_rate_limit, :logger
|
64
|
+
|
65
|
+
##
|
66
|
+
# @example
|
67
|
+
# Newznab::Api.new(uri: 'https://api.newznab.com', key: '7e8896464895c1cde33759b8307f5cf8')
|
68
|
+
# @return [Newznab::API]
|
69
|
+
# @param uri [String] Newznab API Uri
|
70
|
+
# @param key [String] Newznab API Key
|
71
|
+
# @since 0.1.0
|
72
|
+
def new(uri: nil, key: nil)
|
73
|
+
|
74
|
+
@api_rate_limit = 0
|
75
|
+
|
76
|
+
@logger = MonoLogger.new(STDOUT)
|
77
|
+
@logger.level = MonoLogger::WARN
|
78
|
+
|
79
|
+
# Newznab API Key. Set to the environmental variable NEWZNAB_API_KEY by default if present
|
80
|
+
@api_key = ENV['NEWZNAB_API_KEY'] || nil
|
81
|
+
# Newznab API Uri. Set to the environmental variable NEWZNAB_URI by default if present
|
82
|
+
@api_uri = ENV['NEWZNAB_URI'] || nil
|
83
|
+
# Api response timeout in seconds
|
84
|
+
@api_timeout = 10
|
85
|
+
|
86
|
+
|
87
|
+
# Set passed uri
|
88
|
+
unless uri.nil?
|
89
|
+
@api_uri=uri
|
90
|
+
end
|
91
|
+
|
92
|
+
# Set passed key
|
93
|
+
unless key.nil?
|
94
|
+
@api_key=key
|
95
|
+
end
|
96
|
+
|
97
|
+
self
|
98
|
+
end
|
99
|
+
|
100
|
+
##
|
101
|
+
# Return the server's supported features and the protocol version and other metadata
|
102
|
+
# Will perform a request to server if not set
|
103
|
+
# @return [Hash]
|
104
|
+
# @since 0.1.0
|
105
|
+
# @macro raise.NewznabAPIError
|
106
|
+
def caps
|
107
|
+
@caps ||= _make_request(:caps)
|
108
|
+
end
|
109
|
+
|
110
|
+
##
|
111
|
+
# @param api_function [Symbol] Newznab function
|
112
|
+
# @param params [Hash] The named key value pairs of query parameters
|
113
|
+
# @macro raise.NewznabAPIError
|
114
|
+
# @macro raise.FunctionNotSupportedError
|
115
|
+
# @since 0.1.0
|
116
|
+
def get(api_function:, **params)
|
117
|
+
_make_request(api_function, **params)
|
118
|
+
end
|
119
|
+
|
120
|
+
##
|
121
|
+
# Get item based on guid
|
122
|
+
# @param guid [String] The GUID of the item being queried.
|
123
|
+
# @since 0.1.0
|
124
|
+
def get_details(guid)
|
125
|
+
resp = _make_request(:details, id: guid)
|
126
|
+
if resp['channel']['item']
|
127
|
+
Newznab::Api::Item.new(resp['channel']['item'])
|
128
|
+
else
|
129
|
+
nil
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
##
|
134
|
+
# Get item's NFO based on guid
|
135
|
+
# @param guid [String] The GUID of the item being queried.
|
136
|
+
# @since 0.1.0
|
137
|
+
def get_nfo(guid)
|
138
|
+
resp = _make_request(:getnfo, id: guid)
|
139
|
+
if resp['channel']['item']['description']
|
140
|
+
resp['channel']['item']['description']
|
141
|
+
else
|
142
|
+
nil
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
private
|
147
|
+
|
148
|
+
##
|
149
|
+
# Will attempt to parse the {api_uri} and append '/api' to the end if needed
|
150
|
+
# @return [String]
|
151
|
+
# @since 0.1.0
|
152
|
+
def _build_base_url
|
153
|
+
if self.api_uri.to_s.match(/\/api$/)
|
154
|
+
self.api_uri
|
155
|
+
else
|
156
|
+
self.api_uri + '/api'
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
##
|
161
|
+
# Executes api request based on provided +function+ and +params+
|
162
|
+
#
|
163
|
+
# @example Return 5 results from the +:characters+ resource
|
164
|
+
# _make_request(:search, limit: 5)
|
165
|
+
#
|
166
|
+
# @param function [Symbol] Newznab function
|
167
|
+
# @param params [Hash] The named key value pairs of query parameters
|
168
|
+
# @return [Hash]
|
169
|
+
# @since 0.1.0
|
170
|
+
# @macro raise.NewznabAPIError
|
171
|
+
# @macro raise.FunctionNotSupportedError
|
172
|
+
def _make_request(function, **params)
|
173
|
+
|
174
|
+
unless API_FUNCTIONS.include?(function)
|
175
|
+
logger.error("Function #{function.to_s} not supported")
|
176
|
+
raise FunctionNotSupportedError, "Function #{function.to_s} not supported"
|
177
|
+
end
|
178
|
+
|
179
|
+
_make_url_request(_build_base_url, function, params)
|
180
|
+
end
|
181
|
+
|
182
|
+
##
|
183
|
+
# Executes api request based on provided +resource+ and +params+
|
184
|
+
#
|
185
|
+
# @example Make a simple request with +limit: 1+
|
186
|
+
# _make_url_request('http://newznabserver.com/api', t: :caps)
|
187
|
+
#
|
188
|
+
# @param url [String] Request url
|
189
|
+
# @param function [Symbol] Newznab function
|
190
|
+
# @param params [Hash] optional request parameters
|
191
|
+
# @return [Hash]
|
192
|
+
# @since 0.1.0
|
193
|
+
# @macro raise.NewznabAPIError
|
194
|
+
def _make_url_request(url, function, **params)
|
195
|
+
|
196
|
+
# If we have a rate_limit set, wait that long before sending a request
|
197
|
+
if api_rate_limit > 0
|
198
|
+
sleep api_rate_limit
|
199
|
+
end
|
200
|
+
|
201
|
+
# Default options hash
|
202
|
+
options = {
|
203
|
+
accept: :json,
|
204
|
+
content_type: :json,
|
205
|
+
params: {
|
206
|
+
apikey: self.api_key,
|
207
|
+
o: Newznab::Api::API_FORMAT,
|
208
|
+
t: function.to_s,
|
209
|
+
}
|
210
|
+
}
|
211
|
+
|
212
|
+
options[:params].merge! params
|
213
|
+
|
214
|
+
begin
|
215
|
+
logger.debug("Request URL: #{url}")
|
216
|
+
logger.debug("Request headers: #{options.to_json}")
|
217
|
+
|
218
|
+
# Perform request
|
219
|
+
request = RestClient::Request.execute(
|
220
|
+
method: :get,
|
221
|
+
url: url,
|
222
|
+
timeout: self.api_timeout,
|
223
|
+
headers: options,
|
224
|
+
)
|
225
|
+
|
226
|
+
rescue RestClient::NotFound, RestClient::Exceptions::ReadTimeout => e
|
227
|
+
logger.error(e.message)
|
228
|
+
raise NewznabAPIError, e.message
|
229
|
+
end
|
230
|
+
|
231
|
+
case request.code
|
232
|
+
when 200
|
233
|
+
if request.headers[:content_type].eql? 'text/xml'
|
234
|
+
doc = Nokogiri.XML(request.body, nil)
|
235
|
+
if doc.xpath('//error')
|
236
|
+
code = doc.xpath('//error')[0]['code']
|
237
|
+
msg = doc.xpath('//error')[0]['description']
|
238
|
+
_newznab_error(code: code, msg: msg)
|
239
|
+
else
|
240
|
+
logger.error('XML responses are not supported')
|
241
|
+
raise NewznabAPIError, 'XML responses are not supported'
|
242
|
+
end
|
243
|
+
elsif request.headers[:content_type].eql? 'application/json'
|
244
|
+
req = JSON.parse(request.body)
|
245
|
+
if req
|
246
|
+
req
|
247
|
+
else
|
248
|
+
logger.error(req['error'])
|
249
|
+
raise NewznabAPIError, req['error']
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
else
|
254
|
+
logger.error('Recived a '+request.code+' http response')
|
255
|
+
raise NewznabAPIError, 'Recived a '+request.code+' http response'
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
##
|
260
|
+
# @param code [Integer] Error code from the Newznab server
|
261
|
+
# @param msg [String] Error message from the Newznab server
|
262
|
+
# @macro raise.NewznabAPIError
|
263
|
+
# @macro raise.FunctionNotSupportedError
|
264
|
+
# @since 0.1.0
|
265
|
+
def _newznab_error(code:, msg: nil)
|
266
|
+
if msg.nil?
|
267
|
+
msg = case code.to_i
|
268
|
+
when 100
|
269
|
+
'Incorrect user credentials'
|
270
|
+
when 101
|
271
|
+
'Account suspended'
|
272
|
+
when 102
|
273
|
+
'Insufficient privileges/not authorized'
|
274
|
+
when 103
|
275
|
+
'Registration denied'
|
276
|
+
when 104
|
277
|
+
'Registrations are closed'
|
278
|
+
when 105
|
279
|
+
'Invalid registration (Email Address Taken)'
|
280
|
+
when 106
|
281
|
+
'Invalid registration (Email Address Bad Format)'
|
282
|
+
when 107
|
283
|
+
'Registration Failed (Data error)'
|
284
|
+
when 200
|
285
|
+
'Missing parameter'
|
286
|
+
when 201
|
287
|
+
'Incorrect parameter'
|
288
|
+
when 202
|
289
|
+
'No such function. (Function not defined in this specification).'
|
290
|
+
when 203
|
291
|
+
'Function not available. (Optional function is not implemented).'
|
292
|
+
when 300
|
293
|
+
'No such item.'
|
294
|
+
when 300
|
295
|
+
'Item already exists.'
|
296
|
+
when 900
|
297
|
+
'Unknown error'
|
298
|
+
when 910
|
299
|
+
'API Disabled'
|
300
|
+
end
|
301
|
+
end
|
302
|
+
logger.error("NewznabAPIError - Code: #{code} Message: #{msg}")
|
303
|
+
if msg.eql? 'API Disabled'
|
304
|
+
raise FunctionDisabledError, "Code: #{code} Message: #{msg}"
|
305
|
+
else
|
306
|
+
raise NewznabAPIError, "Code: #{code} Message: #{msg}"
|
307
|
+
end
|
308
|
+
end
|
309
|
+
end
|
310
|
+
end
|
311
|
+
end
|