accessible-books 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +20 -0
  3. data/README.md +7 -0
  4. data/lib/accessible-books.rb +69 -0
  5. metadata +132 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a98aff07c8b43c1ad10b260adf32b809e36d3e63
4
+ data.tar.gz: ddeb9a893b14533a8a53fccd8fb256c547e0e5f4
5
+ SHA512:
6
+ metadata.gz: f24e75596afc9f487f36360a3c534295f7ee82bc6720cabc44e428014d4777815914942d1e01a00b0035e580d07f30d0c9cc3ece3019f9687a7968ec70e95605
7
+ data.tar.gz: 764a75f3cb55c97372501672fa2ce1f48e38b95eb6d6c33e9757b10c4c01717f27df9691e0130005dfb819296571b3bbda82a058293055b3fc985fcdc11fbbd4
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 CoughDrop
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to use,
8
+ copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
9
+ Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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 LIABILITY,
19
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,7 @@
1
+ AccessibleBooks
2
+
3
+ Tarheel Reader is a great tool, but needs just a little help in
4
+ its programmatic access. Also this gem supports retrieving
5
+ accessible books from other sources with a similar formatting,
6
+ including checking the head attribute of an HTML response for directions
7
+ on where to find the JSON-formatted data for an online book.
@@ -0,0 +1,69 @@
1
+ require 'json'
2
+ require 'typhoeus'
3
+ require 'nokogiri'
4
+
5
+ module AccessibleBooks
6
+ TARHEEL_REGEX = /https?:\/\/tarheelreader.org\/.+\/.+\/.+\/(.+)\//
7
+
8
+ def self.tarheel_id(url)
9
+ return nil unless url
10
+ if url.match(TARHEEL_REGEX)
11
+ url.match(TARHEEL_REGEX)[1]
12
+ else
13
+ nil
14
+ end
15
+ end
16
+
17
+ def self.find_json(url)
18
+ return nil unless url
19
+ if url.match(/dropbox\.com/) && url.match(/\?dl=0$/)
20
+ url = url.sub(/\?dl=0$/, '?dl=1')
21
+ end
22
+ json = nil
23
+ if url.match(AccessibleBooks::TARHEEL_REGEX)
24
+ json = AccessibleBooks.tarheel_json(url)
25
+ else
26
+ req = Typhoeus.get(url, followlocation: true)
27
+ json = JSON.parse(req.body) rescue nil
28
+ if !json
29
+ elem = Nokogiri(req.body).css("head meta[property='book:url']")[0]
30
+ if elem && elem['content']
31
+ req = Typhoeus.get(elem['content'])
32
+ json = JSON.parse(req.body) rescue nil
33
+ end
34
+ end
35
+ end
36
+ if json && json['pages']
37
+ json['image_url'] ||= json['pages'][0]['image_url']
38
+ end
39
+ json
40
+ end
41
+
42
+ def self.tarheel_json_url(id)
43
+ return nil unless id
44
+ "https://tarheelreader.org/book-as-json/?slug=#{CGI.escape(id)}"
45
+ end
46
+
47
+ def self.tarheel_json(url)
48
+ return nil unless url
49
+ id = tarheel_id(url)
50
+ return nil unless id
51
+ url = self.tarheel_json_url(id)
52
+ return nil unless url
53
+ res = Typhoeus.get(url)
54
+ json = JSON.parse(res.body) rescue nil
55
+ if json && json['title'] && json['pages']
56
+ json['book_url'] = "https://tarheelreader.org#{json['link']}"
57
+ json['attribution_url'] = "https://tarheelreader.org/photo-credits/?id=#{id}"
58
+ json['pages'].each_with_index do |page, idx|
59
+ page['id'] ||= idx == 0 ? 'title_page' : "page_#{idx}"
60
+ page['image_url'] = page['url']
61
+ page['image_url'] = "https://tarheelreader.org#{page['image_url']}" unless page['image_url'].match(/^http/)
62
+ end
63
+ json['image_url'] = json['pages'][1]['image_url']
64
+ else
65
+ json = nil
66
+ end
67
+ json
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: accessible-books
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Whitmer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: typhoeus
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: json
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: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: ruby-debug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Utility for programmatic access to TarheelReader and other accessible
98
+ books
99
+ email: brian.whitmer@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files:
103
+ - LICENSE
104
+ files:
105
+ - LICENSE
106
+ - README.md
107
+ - lib/accessible-books.rb
108
+ homepage: http://github.com/CoughDrop/boy_band
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.5.2
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: AccessibleBooks
132
+ test_files: []