-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Accept-Encoding header to a request for edict #101
base: master
Are you sure you want to change the base?
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>
How about just removing |
Because net/http has auto inflate feature.
OK, I replaced it. |
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. |
Sorry for late reply. |
Thanks! It looks good except niku@9777cef . |
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 `
irb(main):005:0> Zlib::GzipReader.open(open(url, { "Accept-Encoding" => "gzip, deflate" }))
=> #Zlib::GzipReader:0x007ff97ee5bf80