book_finder 0.0.2 → 0.0.3

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.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -0
  3. data/lib/bok.rb +112 -98
  4. data/lib/book_finder.rb +19 -13
  5. data/lib/libgen.rb +60 -58
  6. metadata +17 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c23d488c111de34c691e18f6e92eae31d8763c0f1fb870ee2a6c5a1ae887b404
4
- data.tar.gz: 2740e853fa292cda25908a5b022091d02cd516dfa795327e6e6b75524ba69a54
3
+ metadata.gz: 7f7bb75d03f76832c6248ecbd129e7b93708a472e8e1b77c0e829389f3acf382
4
+ data.tar.gz: 737464685e8856f55de5d0028a8b31834a5a18a838f03be7f1e9e28332e56c6f
5
5
  SHA512:
6
- metadata.gz: f149d3ca7c45e937e1a6f81140dd3c563f46832af00af271fb9e1e8f5982b4d9b79c9032fe46d5db40141f2028790bbbb9b1ac8ed453324f67ce2bb94246690b
7
- data.tar.gz: d1273a3a3475347e7298a13031b864d3a601971ae3172ae0a7b8b45543c0ea13421ba8b5056f4d064544ede5cbcf723a4749d0920016e846fb93cd67a03c15e1
6
+ metadata.gz: 19e0e595c6aff091c9fff46037bb846d06768086a59e5fec1b43aad42dd8230055cbfea1ecfa8b372af26b85c9f992ad81614d2c189c9dd994986ce044e96409
7
+ data.tar.gz: abeaaf270a9f4e3d3acdaaa1234f9955ac62c82e16c6d9ee315d91798244f7dbef3b2636048fb6d2ccc6bdb6fe2d168b7fd445002d93f2cb7c4ea5ca6f2b25aa
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "nokogiri"
data/lib/bok.rb CHANGED
@@ -3,127 +3,141 @@
3
3
  require "nokogiri"
4
4
  require "open-uri"
5
5
 
6
- # b-ok api interface for simple use with web scrapping
7
- class Bok
8
- BASEURL = "https://b-ok.cc"
6
+ class Hash
9
7
 
10
- def search(params)
11
- setup
8
+ def stringify_keys
9
+ inject({}) do |hash, (key, value)|
10
+ value = value.stringify_keys if value.is_a?(Hash)
11
+ hash[key.to_s] = value
12
+ hash
13
+ end
14
+ end
12
15
 
13
- query, page, from, to, language, order_by = params.values
16
+ end
14
17
 
15
- return :QUERY_MISSING unless query
18
+ module BookFinder
19
+ # b-ok api interface for simple use with web scrapping
20
+ class Bok
21
+ BASEURL = "https://b-ok.cc"
16
22
 
17
- @page = page || 1
18
- url = BASEURL + "/s/?q=#{query}&page=#{page}"
19
- if from && to
20
- url += "&yearFrom=#{from}&yearTo=#{to}"
21
- end
22
- if language
23
- url += "&language=#{language}"
24
- end
25
- if order_by
26
- url += "&order=#{order_by}"
23
+ def search(params)
24
+ setup
25
+
26
+ query, page, from, to, language, order_by = params.values
27
+
28
+ return :QUERY_MISSING unless query
29
+
30
+ @page = page || 1
31
+ url = BASEURL + "/s/?q=#{query}&page=#{page}"
32
+ if from && to
33
+ url += "&yearFrom=#{from}&yearTo=#{to}"
34
+ end
35
+ if language
36
+ url += "&language=#{language}"
37
+ end
38
+ if order_by
39
+ url += "&order=#{order_by}"
40
+ end
41
+ html = URI.parse(url).open
42
+ @doc = Nokogiri::HTML(html)
43
+ run
44
+ structure
27
45
  end
28
- html = URI.parse(url).open
29
- @doc = Nokogiri::HTML(html)
30
- run
31
- structure
32
- end
33
46
 
34
- def setup
35
- @page = nil
36
- @img_by_book = []
37
- @title_by_book = []
38
- @links_by_book = []
39
- @authors_by_book = []
40
- @year_by_book = []
41
- @language_by_book = []
42
- @extension_by_book = []
43
- @size_by_book = []
44
- end
47
+ def setup
48
+ @page = nil
49
+ @img_by_book = []
50
+ @title_by_book = []
51
+ @mirrors_by_book = []
52
+ @authors_by_book = []
53
+ @year_by_book = []
54
+ @language_by_book = []
55
+ @extension_by_book = []
56
+ @size_by_book = []
57
+ end
45
58
 
46
- private
59
+ private
47
60
 
48
- def run
49
- img_urls
50
- book_titles
51
- book_authors
52
- book_year
53
- book_language
54
- book_file
55
- end
61
+ def run
62
+ img_urls
63
+ book_titles
64
+ book_authors
65
+ book_year
66
+ book_language
67
+ book_file
68
+ end
56
69
 
57
- def structure
58
- books = []
59
- @img_by_book.zip(
60
- @title_by_book,
61
- @links_by_book,
62
- @authors_by_book,
63
- @year_by_book,
64
- @language_by_book,
65
- @extension_by_book,
66
- @size_by_book
67
- ).each do |book|
68
- books.push(
69
- img: book[0],
70
- title: book[1],
71
- link: book[2],
72
- authors: book[3],
73
- year: book[4],
74
- language: book[5],
75
- extension: book[6],
76
- size: book[7]
77
- )
70
+ def structure
71
+ books = []
72
+ @img_by_book.zip(
73
+ @title_by_book,
74
+ @mirrors_by_book,
75
+ @authors_by_book,
76
+ @year_by_book,
77
+ @language_by_book,
78
+ @extension_by_book,
79
+ @size_by_book
80
+ ).each do |book|
81
+ books.push({
82
+ img: book[0],
83
+ title: book[1],
84
+ mirrors: [book[2]],
85
+ authors: book[3],
86
+ year: book[4],
87
+ language: book[5],
88
+ extension: book[6],
89
+ size: book[7]
90
+ }.stringify_keys)
91
+ end
92
+ books
78
93
  end
79
- books
80
- end
81
94
 
82
- def img_urls
83
- @doc.search("img.cover.lazy").each do |node|
84
- @img_by_book.push("https:" + node["data-src"].to_s)
95
+ def img_urls
96
+ @doc.search("img.cover.lazy").each do |node|
97
+ @img_by_book.push("https:" + node["data-src"].to_s)
98
+ end
85
99
  end
86
- end
87
100
 
88
- def book_titles
89
- @doc.search('[@itemprop="name"] a').each do |node|
90
- @title_by_book.push(node.text.to_s)
91
- url = BASEURL.to_s + node["href"].to_s
92
- doc = Nokogiri::HTML(URI.parse(url).open)
93
- doc.search("a.dlButton").each do |nodes|
94
- @links_by_book.push(BASEURL.to_s + nodes["href"].to_s)
101
+ def book_titles
102
+ @doc.search('[@itemprop="name"] a').each do |node|
103
+ @title_by_book.push(node.text.to_s)
104
+ url = BASEURL.to_s + node["href"].to_s
105
+ doc = Nokogiri::HTML(URI.parse(url).open)
106
+ doc.search("a.dlButton").each do |nodes|
107
+ @mirrors_by_book.push(BASEURL.to_s + nodes["href"].to_s)
108
+ end
95
109
  end
96
110
  end
97
- end
98
111
 
99
- def book_authors
100
- @doc.search('[@class="authors"]').each do |node|
101
- authors = {}
102
- node.search("a").each do |author|
103
- authors[author.text.to_s] = (BASEURL.to_s + author["href"].to_s)
112
+ def book_authors
113
+ @doc.search('[@class="authors"]').each do |node|
114
+ authors = {}
115
+ node.search("a").each do |author|
116
+ authors[author.text.to_s] = (BASEURL.to_s + author["href"].to_s)
117
+ end
118
+ @authors_by_book.push(authors)
104
119
  end
105
- @authors_by_book.push(authors)
106
120
  end
107
- end
108
121
 
109
- def book_year
110
- @doc.search("div.bookProperty.property_year").each do |node|
111
- @year_by_book.push(node.text.to_s.gsub!(/\s+/, "").gsub!(/\w+:/, ""))
122
+ def book_year
123
+ @doc.search("div.bookProperty.property_year").each do |node|
124
+ @year_by_book.push(node.text.to_s.gsub!(/\s+/, "").gsub!(/\w+:/, ""))
125
+ end
112
126
  end
113
- end
114
127
 
115
- def book_language
116
- @doc.search("div.bookProperty.property_language").each do |node|
117
- @language_by_book.push(node.text.to_s.gsub!(/\s+/, "").gsub!(/\w+:/, ""))
128
+ def book_language
129
+ @doc.search("div.bookProperty.property_language").each do |node|
130
+ @language_by_book.push(node.text.to_s.gsub!(/\s+/, "").gsub!(/\w+:/, ""))
131
+ end
118
132
  end
119
- end
120
133
 
121
- def book_file
122
- @doc.search("div.bookProperty.property__file").each do |node|
123
- extension, size = node.text.to_s.gsub!(/\s+/, "")
124
- .gsub!(/\w+:/, "").split(",")
125
- @extension_by_book.push(extension)
126
- @size_by_book.push(size)
134
+ def book_file
135
+ @doc.search("div.bookProperty.property__file").each do |node|
136
+ extension, size = node.text.to_s.gsub!(/\s+/, "")
137
+ .gsub!(/\w+:/, "").split(",")
138
+ @extension_by_book.push(extension)
139
+ @size_by_book.push(size)
140
+ end
127
141
  end
128
142
  end
129
143
  end
@@ -1,30 +1,36 @@
1
1
  require_relative "bok"
2
2
  require_relative "libgen"
3
3
 
4
- class BookFinder
5
-
6
- def initialize
7
- @libgen = Libgen.new
8
- @bok = Bok.new
9
- @result = []
10
- end
11
-
12
- def search(api: nil, params: {})
13
-
4
+ module BookFinder
5
+ @libgen = BookFinder::Libgen.new
6
+ @bok = BookFinder::Bok.new
7
+ @result = []
8
+
9
+ def self.search(api: nil, params: {})
14
10
  @result = []
15
11
 
16
12
  return :PARAMS_MISSING if params.empty?
17
13
 
18
- if api == nil || api.downcase == "libgen"
14
+ if api.nil? || api.downcase == "libgen"
19
15
  @result += @libgen.search(params)
20
16
  end
21
17
 
22
- if api == nil || api.downcase == "bok"
18
+ if api.nil? || api.downcase == "bok"
23
19
  @result += @bok.search(params)
24
20
  end
25
21
 
26
- @result
22
+ @result = @result.map do |book|
23
+ book.map { |key, value| [key.to_s.downcase.chomp, value.to_s.downcase.chomp] }.to_h
24
+ end
25
+
26
+ @result.uniq { |book| {authors: book["authors"], title: book["title"], year: book["year"], language: book["language"], size: book["size"], extension: book["extension"]} }
27
27
 
28
28
  end
29
29
 
30
30
  end
31
+
32
+ # books = BookFinder.search(params:{query:"dog"})
33
+ # puts books.length
34
+ # puts books.shift, books.pop
35
+ # puts "------"
36
+ # p books
@@ -1,72 +1,74 @@
1
1
  require "nokogiri"
2
2
  require "open-uri"
3
3
 
4
- class Libgen
5
- attr_accessor :columns, :row
6
- attr_writer :books
7
-
8
- BASE_URL = "http://gen.lib.rus.ec/search.php".freeze
9
-
10
- def search(params)
11
-
12
- query, by, ordered_by, order_mode, page = params.values
13
-
14
- return :QUERY_MISSING unless query
15
-
16
- url = BASE_URL + "?req=#{query}"
17
- url << "&sort=#{ordered_by}" if ordered_by
18
- url << "&sortmode=#{order_mode}" if order_mode
19
- url << "&column=#{by}" if by
20
- url << "&page=#{page}" if page
21
-
22
- html = URI.parse(url).open
23
- @doc = Nokogiri::HTML(html)
24
-
25
- initialize_columns
26
- initialize_rows
27
- books
28
- end
4
+ module BookFinder
5
+ class Libgen
6
+ attr_accessor :columns, :row
7
+ attr_writer :books
29
8
 
30
- def books
31
- @books = @rows.map { |r|
32
- @columns.zip(vals(r)).to_h
33
- }
34
- end
9
+ BASE_URL = "http://gen.lib.rus.ec/search.php".freeze
35
10
 
36
- private
11
+ def search(params)
37
12
 
38
- def initialize_columns
39
- dom_cols = @doc.search('[@bgcolor="#C0C0C0"]').first.children
40
- @columns = dom_cols.map { |col|
41
- col.content.strip.downcase.tr("()", "") unless col.content.strip.empty?
42
- }
13
+ query, by, ordered_by, order_mode, page = params.values
43
14
 
44
- @columns.compact!
45
- @columns.shift
46
- @columns.pop
47
- end
15
+ return :QUERY_MISSING unless query
48
16
 
49
- def vals(row)
50
- full_vals = []
51
- vals = row.content.split("\r\n\t\t\t\t")
52
- vals.pop
53
- vals.shift
17
+ url = BASE_URL + "?req=#{query}"
18
+ url << "&sort=#{ordered_by}" if ordered_by
19
+ url << "&sortmode=#{order_mode}" if order_mode
20
+ url << "&column=#{by}" if by
21
+ url << "&page=#{page}" if page
54
22
 
55
- authors = vals.shift.split(",")
56
- a_authors = row
57
- .children[2]
58
- .search("a")
59
- .map { |a| a.attribute("href").to_s }
60
- full_vals << authors.zip(a_authors).to_h
23
+ html = URI.parse(url).open
24
+ @doc = Nokogiri::HTML(html)
61
25
 
62
- 7.times { full_vals << vals.shift }
26
+ initialize_columns
27
+ initialize_rows
28
+ books
29
+ end
63
30
 
64
- full_vals << row.children[18..22].search("a").map { |a| a.attribute("href").to_s }
65
- full_vals
66
- end
31
+ def books
32
+ @books = @rows.map { |r|
33
+ @columns.zip(vals(r)).to_h
34
+ }
35
+ end
36
+
37
+ private
38
+
39
+ def initialize_columns
40
+ dom_cols = @doc.search('[@bgcolor="#C0C0C0"]').first.children
41
+ @columns = dom_cols.map { |col|
42
+ col.content.strip.downcase.tr("()", "") unless col.content.strip.empty?
43
+ }
44
+
45
+ @columns.compact!
46
+ @columns.shift
47
+ @columns.pop
48
+ end
49
+
50
+ def vals(row)
51
+ full_vals = []
52
+ vals = row.content.split("\r\n\t\t\t\t")
53
+ vals.pop
54
+ vals.shift
55
+
56
+ authors = vals.shift.split(",")
57
+ a_authors = row
58
+ .children[2]
59
+ .search("a")
60
+ .map { |a| a.attribute("href").to_s }
61
+ full_vals << authors.zip(a_authors).to_h
62
+
63
+ 7.times { full_vals << vals.shift }
64
+
65
+ full_vals << row.children[18..22].search("a").map { |a| a.attribute("href").to_s }
66
+ full_vals
67
+ end
67
68
 
68
- def initialize_rows
69
- @rows = [@doc.search('[@bgcolor="#C6DEFF"]'),
70
- @doc.search('[@bgcolor=""]'),].flatten
69
+ def initialize_rows
70
+ @rows = [@doc.search('[@bgcolor="#C6DEFF"]'),
71
+ @doc.search('[@bgcolor=""]'),].flatten
72
+ end
71
73
  end
72
74
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: book_finder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Amezcua
@@ -10,7 +10,21 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2019-10-30 00:00:00.000000000 Z
13
- dependencies: []
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.6'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.6'
14
28
  description:
15
29
  email:
16
30
  - gerald.amezcua@michelada.io
@@ -19,6 +33,7 @@ executables: []
19
33
  extensions: []
20
34
  extra_rdoc_files: []
21
35
  files:
36
+ - Gemfile
22
37
  - lib/bok.rb
23
38
  - lib/book_finder.rb
24
39
  - lib/libgen.rb