book_finder 0.0.0 → 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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/bok.rb +129 -0
  3. data/lib/libgen.rb +72 -0
  4. metadata +3 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8d51b5fa354ae3959aa8c9166a99537672ea97481c185555fe144f0a984f976
4
- data.tar.gz: d6454818e1e2fa03e9146d754952366ca1952636dd7587e35ddeadae66046749
3
+ metadata.gz: e4108d73d455de5755c79af70dedf91eef3ee68c0756f6f2fbad79ecdad3de5e
4
+ data.tar.gz: 34dcc23d2026f0d5e9e8fe3e6a653577017a96889bb95d4f82f32bf456e48c73
5
5
  SHA512:
6
- metadata.gz: d0cac05529d2e864dabf246f85b57f6764648ab56821e48f8a6beed6479b056cb72c3fd8206e4a3ab922f63f32aeadd0b90dcd9839cead4e0bfb9a9259b8d948
7
- data.tar.gz: dcb3d4ee6dba3eca37cae82d1291d14ebdd8e67aa1fc17cb5d8c3a4b1f25840f84e68d38ecc95737ad9dcc8a5ee0226fe74c3e1ec9a24cc2e76254f2ff5ab4aa
6
+ metadata.gz: 41eccfff0f174e9f92349e23535f87b2fac324f17657a9b4c43d8df2dd075ed011e5ee7fb7958cef408a41825c3ad5796599fac6eaa9f8a981d8e70b2bc123e0
7
+ data.tar.gz: 88cb01fc6559665d221624bb180fd48143b179b83d4915cd276683374fdfffb5e5c3dd56a62e035e505a953e3dfae2ae85d439712e48b5f4b5467106b65cc785
@@ -0,0 +1,129 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nokogiri"
4
+ require "open-uri"
5
+
6
+ # b-ok api interface for simple use with web scrapping
7
+ class Bok
8
+ BASEURL = "https://b-ok.cc"
9
+
10
+ def search(params)
11
+ setup
12
+
13
+ query, page, from, to, language, order_by = params.values
14
+
15
+ return :QUERY_MISSING unless query
16
+
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}"
27
+ end
28
+ html = URI.parse(url).open
29
+ @doc = Nokogiri::HTML(html)
30
+ run
31
+ structure
32
+ end
33
+
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
45
+
46
+ private
47
+
48
+ def run
49
+ img_urls
50
+ book_titles
51
+ book_authors
52
+ book_year
53
+ book_language
54
+ book_file
55
+ end
56
+
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
+ )
78
+ end
79
+ books
80
+ end
81
+
82
+ def img_urls
83
+ @doc.search("img.cover.lazy").each do |node|
84
+ @img_by_book.push("https:" + node["data-src"].to_s)
85
+ end
86
+ end
87
+
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)
95
+ end
96
+ end
97
+ end
98
+
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)
104
+ end
105
+ @authors_by_book.push(authors)
106
+ end
107
+ end
108
+
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+:/, ""))
112
+ end
113
+ end
114
+
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+:/, ""))
118
+ end
119
+ end
120
+
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)
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,72 @@
1
+ require "nokogiri"
2
+ require "open-uri"
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
29
+
30
+ def books
31
+ @books = @rows.map { |r|
32
+ @columns.zip(vals(r)).to_h
33
+ }
34
+ end
35
+
36
+ private
37
+
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
+ }
43
+
44
+ @columns.compact!
45
+ @columns.shift
46
+ @columns.pop
47
+ end
48
+
49
+ def vals(row)
50
+ full_vals = []
51
+ vals = row.content.split("\r\n\t\t\t\t")
52
+ vals.pop
53
+ vals.shift
54
+
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
61
+
62
+ 7.times { full_vals << vals.shift }
63
+
64
+ full_vals << row.children[18..22].search("a").map { |a| a.attribute("href").to_s }
65
+ full_vals
66
+ end
67
+
68
+ def initialize_rows
69
+ @rows = [@doc.search('[@bgcolor="#C6DEFF"]'),
70
+ @doc.search('[@bgcolor=""]'),].flatten
71
+ end
72
+ 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.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Amezcua
@@ -19,7 +19,9 @@ executables: []
19
19
  extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
+ - lib/bok.rb
22
23
  - lib/book_finder.rb
24
+ - lib/libgen.rb
23
25
  homepage: ''
24
26
  licenses:
25
27
  - MIT