shanbay 0.1.1 → 0.1.2
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 +5 -5
- data/README.md +1 -1
- data/exe/shanbay +54 -7
- data/lib/shanbay/api.rb +6 -6
- data/lib/shanbay/data.rb +92 -67
- data/lib/shanbay/version.rb +1 -1
- data/shanbay.gemspec +17 -16
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e53444d1ed90aaca742d26f6009af1c01024f73ef89db6d3247c569538c09f44
|
4
|
+
data.tar.gz: 52b73f3b76ab87e4541da6f2b8cb878bb5d27cfd11f18e471acfa707f3b0e3f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53fcde8658aec4269592047a8fe2181a6f5252bb6650c319bf150a788c244662a420657c85ec8015e9e12cb5eb24f39c192997e7c321c0ab6d2e70585f45ab42
|
7
|
+
data.tar.gz: a96b5029f2b3c9c4d83ab8b928064adf3fc046b6269f3c1018d83bf638d42e8280dfbf1ddebd3be50bd367e82a3bd8540ee9daf2ecb72386b8fc95c79486d975
|
data/README.md
CHANGED
data/exe/shanbay
CHANGED
@@ -1,10 +1,57 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
require
|
3
|
-
|
2
|
+
require "rainbow/ext/string"
|
3
|
+
|
4
|
+
require "shanbay"
|
5
|
+
# require_relative "../lib/shanbay"
|
6
|
+
|
7
|
+
def puts_shanbay_data(word)
|
8
|
+
return if word.nil?
|
4
9
|
api = Shanbay::Api.new
|
5
|
-
data = api.search
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
+
data = api.search word.chomp
|
11
|
+
if !data.content.nil?
|
12
|
+
lines = []
|
13
|
+
lines << "#{data.content}".color(:yellow) + " [#{data.pron}]".color(:magenta)
|
14
|
+
puts lines
|
15
|
+
end
|
16
|
+
if !data.cn_definition.nil?
|
17
|
+
cn_defn = data.cn_definition["defn"]
|
18
|
+
return if cn_defn.nil?
|
19
|
+
cn_defns = cn_defn.lines.map do |line|
|
20
|
+
type, defn = line.chomp.scan(/(\w*\.)([^\.]*)/)[0]
|
21
|
+
[type.color(:white), defn.color(:cyan)]
|
22
|
+
end
|
23
|
+
puts cn_defns.flatten
|
24
|
+
end
|
25
|
+
|
26
|
+
if !data.us_audio.nil?
|
27
|
+
data.pre_download_us_audio
|
28
|
+
data.play_us_audio
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
if ARGV.empty?
|
33
|
+
begin
|
34
|
+
loop do
|
35
|
+
print "> "
|
36
|
+
input = gets
|
37
|
+
return if input.nil?
|
38
|
+
input.chomp!
|
39
|
+
# 输入为空时继续循环
|
40
|
+
next if input == ""
|
41
|
+
puts_shanbay_data(input)
|
42
|
+
end
|
43
|
+
rescue SystemExit, Interrupt
|
44
|
+
puts "bye~"
|
45
|
+
end
|
46
|
+
elsif %w{-h --help}.any? { |c| ARGV.include? c }
|
47
|
+
help = <<HELP
|
48
|
+
shanbay: Translate tools in the command line
|
49
|
+
$ shanbay word
|
50
|
+
$ shanbay world peace
|
51
|
+
$ shanbay
|
52
|
+
> enter the loop mode, ctrl+c to exit
|
53
|
+
HELP
|
54
|
+
puts help
|
55
|
+
else
|
56
|
+
puts_shanbay_data(ARGV.join(" "))
|
10
57
|
end
|
data/lib/shanbay/api.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
+
require "uri"
|
1
2
|
require "open-uri"
|
2
3
|
require "json"
|
3
|
-
|
4
|
+
|
4
5
|
module Shanbay
|
5
6
|
class Api
|
6
7
|
def search(word)
|
7
|
-
getwordui =
|
8
|
-
|
9
|
-
|
8
|
+
getwordui = "https://api.shanbay.com/bdc/search/?word=#{URI.escape(word)}"
|
9
|
+
|
10
|
+
open(URI(getwordui)) do |io|
|
11
|
+
jsonstr = io.read
|
10
12
|
json = JSON.parse(jsonstr)
|
11
13
|
data = json["data"]
|
12
14
|
return parse_data data
|
@@ -26,7 +28,5 @@ module Shanbay
|
|
26
28
|
#data.pre_download_us_audio
|
27
29
|
data
|
28
30
|
end
|
29
|
-
|
30
|
-
|
31
31
|
end
|
32
32
|
end
|
data/lib/shanbay/data.rb
CHANGED
@@ -1,65 +1,66 @@
|
|
1
1
|
#{
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
#}
|
2
|
+
#"msg": "SUCCESS",
|
3
|
+
#"status_code": 0,
|
4
|
+
#"data": {
|
5
|
+
#"pronunciations": {
|
6
|
+
#"uk": "'saɪləns",
|
7
|
+
#"us": "'saɪləns"
|
8
|
+
#},
|
9
|
+
#"en_definitions": {
|
10
|
+
#"v": [
|
11
|
+
#"cause to be quiet or not talk",
|
12
|
+
#"keep from expression, for example by threats or pressure"
|
13
|
+
#],
|
14
|
+
#"n": [
|
15
|
+
#"the state of being silent (as when no one is speaking)",
|
16
|
+
#"the absence of sound",
|
17
|
+
#"a refusal to speak when expected"
|
18
|
+
#]
|
19
|
+
#},
|
20
|
+
#"content_id": 5499,
|
21
|
+
#"audio_addresses": {
|
22
|
+
#"uk": [
|
23
|
+
#"https://words-audio.oss.aliyuncs.com/uk%2Fs%2Fsi%2Fsilence.mp3",
|
24
|
+
#"http://words-audio.cdn.shanbay.com/uk/s/si/silence.mp3"
|
25
|
+
#],
|
26
|
+
#"us": [
|
27
|
+
#"https://words-audio.oss.aliyuncs.com/us%2Fs%2Fsi%2Fsilence.mp3",
|
28
|
+
#"http://words-audio.cdn.shanbay.com/us/s/si/silence.mp3"
|
29
|
+
#]
|
30
|
+
#},
|
31
|
+
#"en_definition": {
|
32
|
+
#"pos": "v",
|
33
|
+
#"defn": "cause to be quiet or not talk; keep from expression, for example by threats or pressure"
|
34
|
+
#},
|
35
|
+
#"uk_audio": "http://media.shanbay.com/audio/uk/silence.mp3",
|
36
|
+
#"has_audio": true,
|
37
|
+
#"conent_id": 5499,
|
38
|
+
#"pronunciation": "'saɪləns",
|
39
|
+
#"audio_name": "silence",
|
40
|
+
#"content": "silence",
|
41
|
+
#"pron": "'saɪləns",
|
42
|
+
#"num_sense": 1,
|
43
|
+
#"object_id": 5499,
|
44
|
+
#"content_type": "vocabulary",
|
45
|
+
#"definition": " n. 沉默,寂静\nv. 使沉默,使安静\nvt. 使安静,使沉默",
|
46
|
+
#"sense_id": 0,
|
47
|
+
#"audio": "http://media.shanbay.com/audio/us/silence.mp3",
|
48
|
+
#"id": 5499,
|
49
|
+
#"cn_definition": {
|
50
|
+
#"pos": "",
|
51
|
+
#"defn": "n. 沉默,寂静\nv. 使沉默,使安静\nvt. 使安静,使沉默"
|
52
|
+
#},
|
53
|
+
#"us_audio": "http://media.shanbay.com/audio/us/silence.mp3"
|
55
54
|
#}
|
56
|
-
|
57
|
-
require
|
58
|
-
require
|
55
|
+
#}
|
56
|
+
require "tempfile"
|
57
|
+
require "em-http-request"
|
58
|
+
require "time"
|
59
|
+
|
59
60
|
module Shanbay
|
60
61
|
class Data
|
61
62
|
attr_accessor :pronunciations #{uk: "saɪləns", us: "saɪləns"}
|
62
|
-
attr_accessor :uk_pronunciation
|
63
|
+
attr_accessor :uk_pronunciation
|
63
64
|
attr_accessor :us_pronunciation
|
64
65
|
attr_accessor :en_definitions #{"v": ["cause to be quiet or not talk", "keep from expression, for example by threats or pressure"], "n": ["the state of being silent (as when no one is speaking)", "the absence of sound", "a refusal to speak when expected"]}
|
65
66
|
attr_accessor :audio_address
|
@@ -79,13 +80,13 @@ module Shanbay
|
|
79
80
|
|
80
81
|
attr_accessor :local_us_audio
|
81
82
|
attr_accessor :raw
|
82
|
-
|
83
|
+
|
83
84
|
def initialize
|
84
85
|
end
|
85
86
|
|
86
87
|
def content=(word)
|
87
88
|
@content = word
|
88
|
-
@local_us_audio = ::Tempfile.new([content.to_s,
|
89
|
+
@local_us_audio = ::Tempfile.new([content.to_s, ".mp3"])
|
89
90
|
end
|
90
91
|
|
91
92
|
def pre_download_us_audio
|
@@ -97,21 +98,47 @@ module Shanbay
|
|
97
98
|
http.callback {
|
98
99
|
local_us_audio.write(http.response)
|
99
100
|
local_us_audio.rewind
|
100
|
-
::EM.stop
|
101
|
+
::EM.stop
|
101
102
|
}
|
102
103
|
http.errback {
|
103
|
-
::EM.stop
|
104
|
+
::EM.stop
|
104
105
|
}
|
105
106
|
end
|
107
|
+
end
|
106
108
|
|
109
|
+
def which(cmd)
|
110
|
+
exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [""]
|
111
|
+
ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
|
112
|
+
exts.each { |ext|
|
113
|
+
exe = File.join(path, "#{cmd}#{ext}")
|
114
|
+
return exe if File.executable?(exe) && !File.directory?(exe)
|
115
|
+
}
|
116
|
+
end
|
117
|
+
return nil
|
107
118
|
end
|
108
119
|
|
109
120
|
def play_us_audio
|
110
|
-
if local_us_audio.size > 0
|
111
|
-
|
112
|
-
|
121
|
+
# if local_us_audio.size > 0
|
122
|
+
# # if ENV["_system_name"] == "OSX"
|
123
|
+
# if which("afplay")
|
124
|
+
# fork { exec "afplay", local_us_audio.path }
|
125
|
+
# elsif which("ffplay")
|
126
|
+
# # fork { exec "ffplay", " -nodisp", " -autoexit ", local_us_audio.path, " >/dev/null 2>&1" }
|
127
|
+
# fork {
|
128
|
+
# system("ffplay -nodisp -autoexit #{local_us_audio.path} >/dev/null 2>&1 ")
|
129
|
+
# }
|
130
|
+
# end
|
131
|
+
# end
|
132
|
+
if us_audio
|
133
|
+
if which("afplay")
|
134
|
+
fork { exec "afplay", us_audio }
|
135
|
+
elsif which("ffplay")
|
136
|
+
# fork { exec "ffplay", " -nodisp", " -autoexit ", us_audio, " >/dev/null 2>&1" }
|
137
|
+
fork {
|
138
|
+
system("ffplay -nodisp -autoexit #{us_audio} >/dev/null 2>&1 ")
|
139
|
+
}
|
113
140
|
end
|
114
|
-
|
141
|
+
|
115
142
|
pre_download_us_audio
|
116
143
|
end
|
117
144
|
end
|
@@ -123,7 +150,5 @@ module Shanbay
|
|
123
150
|
def to_json
|
124
151
|
@raw.to_json
|
125
152
|
end
|
126
|
-
|
127
153
|
end
|
128
154
|
end
|
129
|
-
|
data/lib/shanbay/version.rb
CHANGED
data/shanbay.gemspec
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
lib = File.expand_path(
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
4
|
+
require "shanbay/version"
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name
|
8
|
-
spec.version
|
9
|
-
spec.authors
|
10
|
-
spec.email
|
7
|
+
spec.name = "shanbay"
|
8
|
+
spec.version = Shanbay::VERSION
|
9
|
+
spec.authors = ["Ji-Yuhang"]
|
10
|
+
spec.email = ["yuhang.silence@gmail.com"]
|
11
11
|
|
12
|
-
spec.summary
|
13
|
-
spec.description
|
14
|
-
spec.homepage
|
15
|
-
spec.license
|
12
|
+
spec.summary = %q{an unofficial shanbay gem. https://www.shanbay.com/}
|
13
|
+
spec.description = %q{an unofficial shanbay gem. https://www.shanbay.com/}
|
14
|
+
spec.homepage = "https://github.com/Ji-Yuhang/gem-shanbay"
|
15
|
+
spec.license = "MIT"
|
16
16
|
|
17
17
|
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
18
|
# delete this section to allow pushing this gem to any host.
|
@@ -22,14 +22,15 @@ Gem::Specification.new do |spec|
|
|
22
22
|
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
23
|
end
|
24
24
|
|
25
|
-
spec.files
|
26
|
-
spec.bindir
|
27
|
-
spec.executables
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
28
|
spec.require_paths = ["lib"]
|
29
29
|
|
30
|
-
spec.add_dependency
|
31
|
-
spec.add_dependency
|
32
|
-
|
30
|
+
spec.add_dependency "em-http-request", "~> 1.1"
|
31
|
+
spec.add_dependency "awesome_print", "~> 1.7"
|
32
|
+
spec.add_dependency "rainbow", "~>3.0"
|
33
|
+
|
33
34
|
spec.add_development_dependency "bundler", "~> 1.11"
|
34
35
|
spec.add_development_dependency "rake", "~> 10.0"
|
35
36
|
spec.add_development_dependency "rspec", "~> 3.0"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shanbay
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ji-Yuhang
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-01-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: em-http-request
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rainbow
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: bundler
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -126,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
126
140
|
version: '0'
|
127
141
|
requirements: []
|
128
142
|
rubyforge_project:
|
129
|
-
rubygems_version: 2.
|
143
|
+
rubygems_version: 2.7.7
|
130
144
|
signing_key:
|
131
145
|
specification_version: 4
|
132
146
|
summary: an unofficial shanbay gem. https://www.shanbay.com/
|