libgen_api 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 44501aa543f10a08cf2824a7874d7aa58afd2c4bcdc0443179e1a099de8ec9e8
4
+ data.tar.gz: 4d24e8293a18129b95976c4ea6acdb73ef502bcf2329d29ff3d50f1ba0287afd
5
+ SHA512:
6
+ metadata.gz: da763aedbf5abdc50ae6488396a9c69704729f745cd7091d031826cdadaf08efaf4a4cd711c7bcc283d3a7b0296db6e1e9e4a2e1680f6ebaeb8a7266f7e163e9
7
+ data.tar.gz: baea8a346012e5db6e399fb00abec2ed89fb557506e36cbb7dbdbaeac065e4b857f1a42c74cec3935dc90ae75e3b3773ee563988f73f1256474b281ef0623b60
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+ .DS_Store
data/CHANGELOG.md ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {"https://github.com/Sag0Sag0/libgen_api"}
4
+
5
+ # Specify your gem's dependencies in libgen_api.gemspec
6
+ gemspec
7
+
8
+ gem 'httparty'
9
+ gem 'nokogiri'
10
+ gem "minitest"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Sag0Sag0
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,91 @@
1
+ # LibgenApi
2
+
3
+ Welcome to the `libgen_api` gem!
4
+
5
+ This gem allows you to search library genesis easily and relatively quicky.
6
+
7
+ ## Advice
8
+
9
+ The Library Genesis maintainers run the library for free through donations.
10
+ Please don't overload their servers.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'libgen_api'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install libgen_api
27
+
28
+ ## Usage
29
+ ### Searching the site
30
+ The main search method is called `search()` and has three options.
31
+
32
+ The first is `query` and is set to the value of the search you want to make.
33
+ The second is `res` and is set to the amount of results you want to recieve per page. The values you can give here are 10, 25 or 100.
34
+ The final option is `column` and is which column you want to search. The values this can have are `def`(if unsure use this one), `title`, `author`, `series`, `publisher`, `year`, `identifier`, `language`, `md5`, `tags` and `extension`.
35
+
36
+ This method will return in order the id's of the books shown in your search results.
37
+
38
+ ### Retrieving books
39
+ Books can be retrieved using their ids.
40
+
41
+ The method `get_book()` can be used to retrieve an individual books information where as the method `get_books()` can be used to retrieve multiple books.
42
+ `get_book()` has two parameters, `id` and `fields`. `id` is any books id number whilst `fields` contains an array of which fields you are interested in looking at. A list of fields can be found [here](http://garbage.world/posts/libgen/). By default the `title`, `author` and `md5` fields are requested. It returns a hash with each selected field being a key that can be used to access its value.
43
+
44
+ `get_books()` works basically the same excepts its first argument is `ids` which accepts an array of ids. It returns an array of hashes for each book requested.
45
+
46
+ Both methods only make one http request, so don't be shy about requesting multiple books at once.
47
+
48
+ ### Changing site
49
+ By default requests go to `libgen.is`. To change this call the method `change_mirror()` with the string of the new mirror site.
50
+ To check what mirror is currently being used call `get_mirror()`
51
+
52
+ ### Example
53
+ ```ruby
54
+ require 'libgen_api'
55
+
56
+ # Change the mirror, this is not necessary.
57
+ LibgenApi.change_mirror("gen.lib.rus.ec")
58
+
59
+ # Search for books using the search term "dodo" with results per page 100 and default column settings.
60
+ # This is the potentially time consuming part, each page needs to be downloaded and parsed.
61
+ # If your search term has a lot of results this can take a while.
62
+ ids = LibgenApi.search("dodo", 100, "def")
63
+
64
+ # Now we grab the books information, specifically their title and author/s.
65
+ # This is quick, it only takes one request from the JSON api.
66
+ books = LibgenApi.get_books(ids, ["title", "author"])
67
+
68
+ # Now print their title and author/s!
69
+ books.each do |book|
70
+ puts "Title: #{book['title']}\nAuthor: #{book['author']}\n\n"
71
+ end
72
+
73
+ ```
74
+
75
+ ## Development
76
+
77
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
78
+
79
+ 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).
80
+
81
+ ## Contributing
82
+
83
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Sag0Sag0/libgen_api.
84
+
85
+ ## License
86
+
87
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
88
+
89
+ ## Thanks
90
+
91
+ I'd like to thank the creators of [libgen.js](https://github.com/dunn/libgen.js/) and the author of [this](http://garbage.world/posts/libgen/) and of course the creators and maintainters of Library Genesis. They were all very helpfull in creating this gem.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "libgen_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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/libgen_api.rb ADDED
@@ -0,0 +1,101 @@
1
+ require "libgen_api/version"
2
+
3
+ require "json"
4
+
5
+ require 'httparty'
6
+ require 'nokogiri'
7
+
8
+ module LibgenApi
9
+ class Error < StandardError; end
10
+
11
+ @mirror = 'libgen.is'
12
+
13
+ # Results are in the same order as search results.
14
+ def self.get_ids(page)
15
+ books = []
16
+
17
+ table = page.search('table')[2]
18
+ table.search('tbody').each do |tb|
19
+ tb.search('tr').each do |tr|
20
+
21
+ # Here we check to see if the name of th current text element is "ID:"
22
+ # and then move onto to the next element in the loop which contains the id.
23
+ found_id = false
24
+ tr.search('td').each do |td|
25
+ if found_id
26
+ books.push(td.text)
27
+ break
28
+ end
29
+
30
+ if td.text == "ID:"
31
+ found_id = true
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ books
38
+ end
39
+
40
+ def self.get_mirror
41
+ @mirror
42
+ end
43
+
44
+ def self.change_mirror(new_mirror)
45
+ @mirror = new_mirror
46
+ end
47
+
48
+ def self.search(query, res, column)
49
+ raise "query must be at least four characters long!" if query.length() < 4
50
+
51
+ if res != 25 && res != 50 && res != 100
52
+ raise "res must equal either 25, 50 or 100"
53
+ end
54
+
55
+ id_collection = []
56
+ page = 1
57
+ loop do
58
+ url = "http://#{@mirror}/search.php?req=#{query}&lg_topic=libgen&open=0&view=detailed&res=#{res}&column=#{column}&page=#{page}"
59
+ response = HTTParty.get(url)
60
+
61
+ doc = Nokogiri::HTML(response.body)
62
+ page_ids = get_ids(doc)
63
+
64
+ id_collection = id_collection + page_ids
65
+
66
+ if page_ids.length() != res
67
+ break
68
+ end
69
+
70
+ page += 1
71
+ end
72
+
73
+ id_collection
74
+ end
75
+
76
+ def self.get_book(id, fields=["Title", "Author", "MD5"])
77
+ fields = fields.join(',')
78
+
79
+ res = HTTParty.get("http://#{@mirror}/json.php?ids=#{id}&fields=#{fields}")
80
+
81
+ begin
82
+ JSON.parse(res.body)[0]
83
+ rescue JSON::ParserError, TypeError => e
84
+ raise "An error has occured when parsing the JSON recieved. The error is:\n#{e}"
85
+ end
86
+ end
87
+
88
+ def self.get_books(ids, fields=["Title", "Author", "MD5"])
89
+ ids = ids.map(&:to_s).join(',')
90
+ fields = fields.join(',')
91
+
92
+ res = HTTParty.get("http://#{@mirror}/json.php?ids=#{ids}&fields=#{fields}")
93
+
94
+ begin
95
+ JSON.parse(res.body)
96
+ rescue JSON::ParserError, TypeError => e
97
+ raise "An error has occured when parsing the JSON recieved. The error is:\n#{e}"
98
+ end
99
+ end
100
+
101
+ end
@@ -0,0 +1,3 @@
1
+ module LibgenApi
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,33 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "libgen_api/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "libgen_api"
8
+ spec.version = LibgenApi::VERSION
9
+ spec.authors = ["Sag0Sag0"]
10
+ spec.email = ["Sag0Sag0@outlook.com"]
11
+
12
+ spec.summary = "An API that can be used to search Library Genesis"
13
+ spec.description = "An API that can be used to search Library Genesis. In the early stages."
14
+ spec.homepage = "https://github.com/Sag0Sag0/libgen_api"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency "httparty"
27
+ spec.add_dependency "nokogiri"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.17"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "minitest", "~> 5.0"
32
+ spec.add_development_dependency "minitest-reporters", "~> 1.3"
33
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: libgen_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Sag0Sag0
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.17'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.17'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-reporters
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.3'
97
+ description: An API that can be used to search Library Genesis. In the early stages.
98
+ email:
99
+ - Sag0Sag0@outlook.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - CHANGELOG.md
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - lib/libgen_api.rb
114
+ - lib/libgen_api/version.rb
115
+ - libgen_api.gemspec
116
+ homepage: https://github.com/Sag0Sag0/libgen_api
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubygems_version: 3.0.6
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: An API that can be used to search Library Genesis
139
+ test_files: []