gzlib 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dc55f64d334633fe26b9037b12708d07c3756670
4
+ data.tar.gz: 1738b106bb987a27d9ccc2dda65bab349ec4289e
5
+ SHA512:
6
+ metadata.gz: 2baf0dea69dc9fc85bf776a36276a29825cc213a8390216b0f3b96a6cf47051b26cd1523078726cea8f917335a0cee7a015325dae46235408ceef419cdc158bf
7
+ data.tar.gz: eecc53b71b433195f3dc7708dd0d4b9d2a9e0118a5304af1e739f990cac48de2dff1e8a951e4b24ffb8eeae7c40bc4bba2a7425fe17ede35ea7255f0894379fd
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+ *.swp
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gzlib.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Gzlib
2
+
3
+ search books on http://opac.gzlib.gov.cn/
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gzlib'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install gzlib
20
+
21
+ ## Usage
22
+
23
+ Search for title on CLI:
24
+
25
+ $ gzlib metarogramming
26
+
27
+ More condition in ruby script:
28
+
29
+ ```ruby
30
+ Gzlib::Search.isbn 9787560974583
31
+ ```
32
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = 'test/**/*_test.rb'
7
+ end
8
+
9
+ desc 'Run tests'
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'gzlib'
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
data/bin/gzlib ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'gzlib'
4
+
5
+ puts Gzlib.list ARGV.shift.dup
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/gzlib.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gzlib/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'gzlib'
8
+ spec.version = Gzlib::VERSION
9
+ spec.authors = ['ken']
10
+ spec.email = ['block24block@gmail.com']
11
+
12
+ spec.summary = 'search books on http://opac.gzlib.gov.cn/'
13
+ spec.homepage = 'https://github.com/turnon/gzlib'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.executables = ['gzlib']
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.9'
21
+ spec.add_development_dependency 'rake', '~> 10.0'
22
+ spec.add_development_dependency 'minitest', '~> 5.0'
23
+
24
+ spec.add_dependency 'nokogiri', '~> 1.6'
25
+ end
data/lib/gzlib/book.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'gzlib/positions'
2
+
3
+ module Gzlib
4
+ class Book
5
+
6
+ Search = 'http://opac.gzlib.gov.cn/opac/book/holdingpreview/'
7
+
8
+ attr_reader :title, :id, :author, :publisher, :year
9
+
10
+ def initialize(node)
11
+ @title = node.at_css('.bookmetaTitle').text.strip
12
+ @id = node['bookrecno']
13
+ metas = node.css('div')
14
+ @author = metas[1].at_css('a').text.strip
15
+ @publisher = metas[2].at_css('a').text.strip
16
+ @year = metas[2].text.gsub(/[^\d]/,'')
17
+ end
18
+
19
+ def positions
20
+ @poss ||= Gzlib::Positions.new id
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,30 @@
1
+ require 'ostruct'
2
+
3
+ module Gzlib
4
+ class Holding < OpenStruct
5
+
6
+ attr_accessor :loan, :libcodeMap, :localMap
7
+ attr_reader :info
8
+
9
+ def loan?
10
+ !!@loan
11
+ end
12
+
13
+ def returnDate
14
+ Time.at loan['returnDate'] / 1000 if loan?
15
+ end
16
+
17
+ def curlibname
18
+ libcodeMap[curlib]
19
+ end
20
+
21
+ def curlocalname
22
+ localMap[curlocal]
23
+ end
24
+
25
+ def lib_callno
26
+ [curlibname, curlocalname, callno].join ' '
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,45 @@
1
+ require 'json'
2
+ require 'gzlib/holding'
3
+
4
+ module Gzlib
5
+ class Positions
6
+
7
+ Search = 'http://opac.gzlib.gov.cn/opac/api/holding/'
8
+
9
+ include Enumerable
10
+
11
+ def initialize(id)
12
+ fetch_json id
13
+ merge_holding_loan
14
+ end
15
+
16
+ def each &b
17
+ @holdings.each &b
18
+ end
19
+
20
+ private
21
+
22
+ def json
23
+ @json
24
+ end
25
+
26
+ def fetch_json id
27
+ json_file = open "#{Search}#{id}"
28
+ @json = JSON.parse json_file.read
29
+ end
30
+
31
+ def merge_holding_loan
32
+ libcodeMap = json['libcodeMap']
33
+ localMap = json['localMap']
34
+ loanWorkMap = json['loanWorkMap']
35
+
36
+ @holdings = json['holdingList'].map do |hold|
37
+ h = Gzlib::Holding.new hold
38
+ h.libcodeMap = libcodeMap
39
+ h.localMap = localMap
40
+ h.loan = loanWorkMap[h.barcode]
41
+ h
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,85 @@
1
+ require 'webrick/httputils'
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+ require 'gzlib/book'
5
+
6
+ module Gzlib
7
+ class Search
8
+ Search = 'http://opac.gzlib.gov.cn/opac/search?hasholding=1&'
9
+ Para = {page: 1, searchWay: 'title', sortWay: 'title200Weight', rows: 100, curlibcode: 'GT'}
10
+ AcceptableSearchWay = [:title, :marc, :isbn, :author, :subject, :class, :publish, :callno]
11
+
12
+ attr_reader :pages
13
+
14
+ include Enumerable
15
+
16
+ class << self
17
+ def method_missing search_way, *keywords, &blk
18
+ return new(keywords.join(' '), searchWay: search_way) if AcceptableSearchWay.include? search_way
19
+ super
20
+ end
21
+ end
22
+
23
+ def initialize(key, opt={})
24
+ @para = Para.merge({q: escape(key)}).merge opt
25
+ doc = getHtml
26
+ @books = getBooks(doc)
27
+ @pages = (@books.empty? ? 1 : getPages(doc))
28
+ end
29
+
30
+ def [](i)
31
+ @books[i]
32
+ end
33
+
34
+ def each(&b)
35
+ @books.each &b
36
+ while not lastPage?
37
+ nextPage
38
+ books = getBooks(getHtml)
39
+ @books.concat books
40
+ books.each &b
41
+ end
42
+ end
43
+
44
+ def total
45
+ reduce(0){ |sum, book| sum + 1 }
46
+ end
47
+
48
+ def curPage
49
+ @para[:page]
50
+ end
51
+
52
+ def lastPage?
53
+ curPage == pages
54
+ end
55
+
56
+ private
57
+
58
+ def getHtml
59
+ Nokogiri::HTML(open "#{Search}#{para}")
60
+ end
61
+
62
+ def escape(str)
63
+ WEBrick::HTTPUtils.escape str.force_encoding('utf-8')
64
+ end
65
+
66
+ def para
67
+ @para.map{ |k,v| "#{k}=#{v}" }.join('&')
68
+ end
69
+
70
+ def getBooks(doc)
71
+ doc.css(".bookmeta").map do |node|
72
+ Gzlib::Book.new(node)
73
+ end.to_a
74
+ end
75
+
76
+ def getPages(doc)
77
+ doc.at_css(".meneame .disabled").text.gsub(/[^\d]/,'').to_i
78
+ end
79
+
80
+ def nextPage
81
+ @para.merge!({page: curPage+1})
82
+ end
83
+
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ module Gzlib
2
+ VERSION = '0.2.0'
3
+ end
data/lib/gzlib.rb ADDED
@@ -0,0 +1,42 @@
1
+ require 'gzlib/version'
2
+ require 'gzlib/search'
3
+
4
+ module Gzlib
5
+ class << self
6
+ def search(*para)
7
+ Search.new(*para)
8
+ end
9
+
10
+ def list(*para)
11
+ style search(*para)
12
+ end
13
+
14
+ private
15
+
16
+ def style search
17
+ search.first(10).map do |book|
18
+ book_style book
19
+ end.join "\n\n"
20
+ end
21
+
22
+ def book_style book
23
+ "#{book.title} #{book.author}\n#{book.publisher} #{book.year}\n#{position_style book.positions}"
24
+ end
25
+
26
+ def position_style poss
27
+ free = poss.
28
+ reject(&:loan?).
29
+ group_by(&:lib_callno).
30
+ map{|callno_lib, poss| [callno_lib, "free x #{poss.count}"]}
31
+
32
+ loan = poss.
33
+ select(&:loan?).
34
+ sort_by(&:returnDate).
35
+ map{|pos| [pos.lib_callno, pos.returnDate]}
36
+
37
+ (free + loan).map{|status| status.join(' ')}.join("\n")
38
+ end
39
+
40
+ end
41
+
42
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gzlib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - ken
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ description:
70
+ email:
71
+ - block24block@gmail.com
72
+ executables:
73
+ - gzlib
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/gzlib
84
+ - bin/setup
85
+ - gzlib.gemspec
86
+ - lib/gzlib.rb
87
+ - lib/gzlib/book.rb
88
+ - lib/gzlib/holding.rb
89
+ - lib/gzlib/positions.rb
90
+ - lib/gzlib/search.rb
91
+ - lib/gzlib/version.rb
92
+ homepage: https://github.com/turnon/gzlib
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.8
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: search books on http://opac.gzlib.gov.cn/
116
+ test_files: []