iciba 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in iciba.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Scott Ballantyne
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Iciba
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'iciba'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install iciba
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/bin/fy ADDED
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'iciba'
4
+ require 'optparse'
5
+ require 'readability'
6
+ require 'fileutils'
7
+ $links = false
8
+
9
+ OptionParser.new do |opts|
10
+ opts.banner = "Usage: #{File.basename($0)} ('words'|word)"
11
+ opts.on("-l", "--links", "Download links?") { |b| $links = true }
12
+ opts.on("-i", "--interactive", "Download links?") { |b| $interactive = true }
13
+ opts.on("-h", "--help", "go to fy.iciba.com") do
14
+ puts opts
15
+ exit 0
16
+ end
17
+
18
+ begin
19
+ opts.parse!(ARGV)
20
+ rescue OptionParser::ParseError => e
21
+ warn e.message
22
+ puts opts
23
+ exit 1
24
+ end
25
+ end
26
+
27
+
28
+ def wrap_text(txt, col = 80)
29
+ txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/,
30
+ "\\1\\3\n")
31
+ end
32
+
33
+ if ARGV.empty?
34
+ abort "Enter some words to lookup!"
35
+ end
36
+
37
+ regex = /https?:\/\/[\S]+/
38
+ links = ARGV.first.scan(regex)
39
+
40
+ def loop_through_links(links)
41
+ links.each do |l|
42
+ text = Readability::Document.new(Iciba::Tools.download(l)).content.gsub('<p>', '').gsub('</p>', "\r\n").gsub('</div></div>', '').gsub('<div><div>', '')
43
+ puts wrap_text(text) unless text == ''
44
+ puts '------------------------------------------------------------------------------------' unless text == ''
45
+ end
46
+ end
47
+
48
+ Iciba::Fanyi.new(ARGV.first, 'auto', true)
49
+ loop_through_links(links) if ($links && links.size > 0)
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/iciba/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Scott Ballantyne"]
6
+ gem.email = ["ussballantyne@gmail.com"]
7
+ gem.description = %q{iciba wrapper}
8
+ gem.summary = %q{iciba wrapper}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "iciba"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Iciba::VERSION
17
+ gem.add_dependency(%q<curb>, [">= 0"])
18
+ gem.add_dependency(%q<hashie>, [">= 0"])
19
+ gem.add_dependency(%q<yajl-ruby>, [">= 0"])
20
+ gem.add_dependency(%q<nokogiri>, [">= 0"])
21
+ # gem.add_dependency(%q<pinyin>, [">= 0"])
22
+ gem.add_dependency(%q<ruby-readability>, [">= 0"])
23
+ end
@@ -0,0 +1,4 @@
1
+ # coding: utf-8
2
+ require "iciba/version"
3
+ require 'iciba/tools'
4
+ require 'iciba/fanyi'
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ module Iciba
3
+ class Fanyi
4
+ attr_accessor :response, :html, :dir, :result
5
+
6
+
7
+
8
+ def initialize(words, dir='auto', bin=false)
9
+ puts '------------------------------------------------------------------------------------' if bin
10
+ # puts if bin
11
+ puts wrap_text(words, 40)
12
+ puts '------------------------------------------------------------------------------------' if bin
13
+ download_and_parse(words, dir)
14
+ puts wrap_text(self.result) if bin
15
+ puts '------------------------------------------------------------------------------------' if bin
16
+ return self.result
17
+ end
18
+
19
+ def wrap_text(txt, col = 80)
20
+ txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/,
21
+ "\\1\\3\n")
22
+ end
23
+
24
+
25
+ def download_and_parse(words, dir)
26
+ self.response = Iciba::Tools.parse(Iciba::Tools.post('http://fy.iciba.com/api.php', {:q => words, :type => dir}).body_str)
27
+ self.html = self.response.ret
28
+ self.result = (Iciba::Tools.doc(self.html)/'span.dd').text.strip
29
+ self.result = self.html if self.result == ""
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,74 @@
1
+ # coding: utf-8
2
+ require 'yajl'
3
+ require 'hashie'
4
+ require 'curb'
5
+ require 'nokogiri'
6
+ module Iciba
7
+ module Tools
8
+
9
+ def self.download(url)
10
+ c = self.curb(url)
11
+ c.body_str
12
+ end
13
+
14
+ def self.post(url, options)
15
+ Curl.post(url, options)
16
+ end
17
+
18
+ def self.last_effective_url(url)
19
+ c = self.curb(url)
20
+ c.last_effective_url
21
+ end
22
+
23
+ def self.curb(url)
24
+ c = Curl::Easy.new(url) do |curl|
25
+ curl.headers["User-Agent"] = user_agent
26
+ # curl.verbose = true
27
+ curl.timeout = 120
28
+ curl.follow_location = true
29
+ curl.max_redirects = 20
30
+ end
31
+ c.perform
32
+ c
33
+ end
34
+
35
+ def self.parse(data)
36
+ Hashie::Mash.new Yajl::Parser.new.parse(data)
37
+ end
38
+
39
+ def self.encode(data)
40
+ Yajl::Encoder.encode(data)
41
+ end
42
+
43
+
44
+ def self.sleep_time
45
+ array_of_numbers = [1.2, 2.35, 2.45, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.1, 1.4]
46
+ random_number = array_of_numbers[rand(array_of_numbers.size)]
47
+ random_number
48
+ end
49
+
50
+ def self.hashify(source)
51
+ Digest::SHA1.hexdigest(source)
52
+ end
53
+
54
+ def self.user_agent
55
+ array_of_agents = ["Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
56
+ "Mozilla/5.0 (Windows; U; Windows NT 6.1; ja; rv:1.9.2a1pre) Gecko/20090403 Firefox/3.6a1pre",
57
+ "Opera/9.80 (Windows NT 6.0; U; fi) Presto/2.2.0 Version/10.00",
58
+ "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_1; zh-CN) AppleWebKit/530.19.2 (KHTML, like Gecko) Version/4.0.2 Safari/530.19",
59
+ "ia_archiver (+http://www.alexa.com/site/help/webmasters; crawler@alexa.com)",
60
+ "Baiduspider+(+http://www.baidu.com/search/spider.htm)",
61
+ 'msnbot-webmaster/1.0 (+http://search.msn.com/msnbot.htm)',
62
+ 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_4_11; ar) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.1 Safari/525.18',
63
+ 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; Tablet PC 2.0; OfficeLiveConnector.1.3; OfficeLivePatch.1.3; MS-RTC LM 8; InfoPath.3)',
64
+ 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)',
65
+ 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 1.1.4322; InfoPath.2; .NET CLR 3.5.21022)']
66
+ random_number = array_of_agents[rand(array_of_agents.size)]
67
+ random_number
68
+ end
69
+
70
+ def self.doc(html)
71
+ Nokogiri::HTML(html)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module Iciba
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iciba
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Scott Ballantyne
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: curb
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: hashie
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: yajl-ruby
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: nokogiri
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: ruby-readability
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: iciba wrapper
95
+ email:
96
+ - ussballantyne@gmail.com
97
+ executables:
98
+ - fy
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - .gitignore
103
+ - Gemfile
104
+ - LICENSE
105
+ - README.md
106
+ - Rakefile
107
+ - bin/fy
108
+ - iciba.gemspec
109
+ - lib/iciba.rb
110
+ - lib/iciba/fanyi.rb
111
+ - lib/iciba/tools.rb
112
+ - lib/iciba/version.rb
113
+ homepage: ''
114
+ licenses: []
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 1.8.21
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: iciba wrapper
137
+ test_files: []