google_translate_scraper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
+ test
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in google_translate_scraper.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Sebastian Glazebrook
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # GoogleTranslateScraper
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'google_translate_scraper'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install google_translate_scraper
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
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/google_translate_scraper/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Seb Glazebrook"]
6
+ gem.email = ["me@sebglazebrook.com"]
7
+ gem.description = %q{" add details description of gem here."}
8
+ gem.summary = %q{"This gem scrapes Google Translate and their results."}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "google_translate_scraper"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = GoogleTranslateScraper::VERSION
17
+ gem.add_dependency "nokogiri"
18
+ end
@@ -0,0 +1,62 @@
1
+ module GoogleTranslateScraper
2
+ require 'net/http'
3
+ require 'nokogiri'
4
+
5
+ class Translator
6
+
7
+ @@input_language = nil
8
+ @@target_language = nil
9
+ @@text = nil
10
+
11
+ def translate(input_language, target_language, text )
12
+ @@input_language = input_language
13
+ @@target_language = target_language
14
+ @@text = text
15
+ get_translation
16
+ end
17
+
18
+ def get_translation
19
+ # set POST variables
20
+ js = "n"
21
+ prev = "_t"
22
+ hv = "en" # i think this is the language of the website
23
+ ie = "UTF-8"
24
+ layout = "2"
25
+ eotf = "1"
26
+ file = ""
27
+
28
+ # download the webpage
29
+ http_response = Net::HTTP.post_form(URI.parse('http://translate.google.com/'), {"sl" => @@input_language, "tl" => @@target_language, "js" => "n", "prev" => "_t", "hv" => "en", "ie" =>
30
+ "UTF-8" , "layout" => "2", "eotf" => "1", "text" => @@text, "file" => ""})
31
+ # get the string
32
+ html_string = http_response.body
33
+ # convert string to a Nokogiri document file
34
+ html_doc = Nokogiri::HTML(html_string)
35
+ # find the translation
36
+ translation = Array.new
37
+ translation = get_dictionary_translation(html_doc)
38
+ # check for non-dictionary translations
39
+ if translation.empty?
40
+ translation[0] = get_non_dictionary_translation(html_doc).text
41
+ end
42
+ return translation
43
+
44
+ end
45
+
46
+ # when there are multiple translations
47
+ def get_dictionary_translation html_doc
48
+ translations = Array.new
49
+ translation = html_doc.xpath('//div[@id="gt-res-dict"]/ol/li/div/div')
50
+ translation.each do |t|
51
+ translations.push t.text
52
+ end
53
+ translations
54
+ end
55
+
56
+ # when there is a phrase with no dictionary translation i.e multiple possible translations
57
+ def get_non_dictionary_translation html_doc
58
+ translation = html_doc.xpath('//html/body/div[2]/div[2]/form/div[2]/div[2]/div/div/div[2]/div/div[2]/span/span')
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module GoogleTranslateScraper
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "google_translate_scraper/version"
2
+ require "google_translate_scraper/translator"
3
+
4
+ module GoogleTranslateScraper
5
+ # Your code goes here...
6
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_translate_scraper
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Seb Glazebrook
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-12 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: nokogiri
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: "\" add details description of gem here.\""
35
+ email:
36
+ - me@sebglazebrook.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - google_translate_scraper.gemspec
50
+ - lib/google_translate_scraper.rb
51
+ - lib/google_translate_scraper/translator.rb
52
+ - lib/google_translate_scraper/version.rb
53
+ homepage: ""
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.9
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: "\"This gem scrapes Google Translate and their results.\""
86
+ test_files: []
87
+