Add Accept-Encoding header to a request for edict#101
Open
niku wants to merge 5 commits intologaling:masterfrom
Open
Add Accept-Encoding header to a request for edict#101niku wants to merge 5 commits intologaling:masterfrom
niku wants to merge 5 commits intologaling:masterfrom
Conversation
Because, Server returns raw content when a client requests without header. $ irb irb(main):001:0> require 'zlib' => true irb(main):002:0> require 'open-uri' => true irb(main):003:0> url = 'http://ftp.monash.edu.au/pub/nihongo/edict.gz' => "http://ftp.monash.edu.au/pub/nihongo/edict.gz" irb(main):004:0> Zlib::GzipReader.open(open(url)) Zlib::GzipFile::Error: not in gzip format from (irb):4:in `initialize' from (irb):4:in `open' from (irb):4 from /usr/local/bin/irb:11:in `<main>' irb(main):005:0> Zlib::GzipReader.open(open(url, { "Accept-Encoding" => "gzip, deflate" })) => #<Zlib::GzipReader:0x007ff97ee5bf80>
Member
|
How about just removing |
Because net/http has auto inflate feature.
Contributor
Author
|
OK, I replaced it. |
Member
|
Ah, I like more simple code like the following: diff --git a/lib/logaling/external_glossaries/edict.rb b/lib/logaling/external_glossaries/edict.rb
index a269679..8ce5511 100644
--- a/lib/logaling/external_glossaries/edict.rb
+++ b/lib/logaling/external_glossaries/edict.rb
@@ -14,8 +14,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require 'open-uri'
-require 'zlib'
-require 'stringio'
module Logaling
class Edict < ExternalGlossary
@@ -29,18 +27,12 @@ module Logaling
def convert_to_csv(csv)
puts "downloading edict file..."
url = 'http://ftp.monash.edu.au/pub/nihongo/edict.gz'
- Zlib::GzipReader.open(open(url)) do |gz|
+ open(url) do |edict|
puts "importing edict file..."
- lines = StringIO.new(gz.read).each_line
-
- lines.next # skip header
-
- preprocessed_lines = lines.map do |line|
- line.encode("UTF-8", "EUC-JP").chomp
- end
-
- preprocessed_lines.each do |line|
+ edict.gets # skip header
+ edict.each_line do |raw_line|
+ line = raw_line.encode("UTF-8", "EUC-JP").chomp
source, target = line.split('/', 2)
source = source.strip
csv << [source, target]
I like rebase. |
Contributor
Author
|
Sorry for late reply. |
Member
|
Thanks! It looks good except niku@9777cef . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Because, Server returns raw content when a client requests without header.
$ irb
'irb(main):001:0> require 'zlib'
=> true
irb(main):002:0> require 'open-uri'
=> true
irb(main):003:0> url = 'http://ftp.monash.edu.au/pub/nihongo/edict.gz'
=> "http://ftp.monash.edu.au/pub/nihongo/edict.gz"
irb(main):004:0> Zlib::GzipReader.open(open(url))
Zlib::GzipFile::Error: not in gzip format
from (irb):4:in
initialize' from (irb):4:inopen'from (irb):4
from /usr/local/bin/irb:11:in `
irb(main):005:0> Zlib::GzipReader.open(open(url, { "Accept-Encoding" => "gzip, deflate" }))
=> #Zlib::GzipReader:0x007ff97ee5bf80