Skip to content

Commit

Permalink
Support for http_proxy and all_proxy environment variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwnall committed Jul 17, 2011
1 parent 6ba7229 commit 8e7aa6b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 deletions.
19 changes: 13 additions & 6 deletions Gemfile.lock
Expand Up @@ -3,17 +3,22 @@ GEM
specs:
diff-lcs (1.1.2)
git (1.2.5)
jeweler (1.6.2)
jeweler (1.6.4)
bundler (~> 1.0)
git (>= 1.2.5)
rake
json (1.5.2)
mechanize (1.0.0)
nokogiri (>= 1.2.1)
nokogiri (1.4.5)
json (1.5.3)
mechanize (2.0.1)
net-http-digest_auth (~> 1.1, >= 1.1.1)
net-http-persistent (~> 1.8)
nokogiri (~> 1.4)
webrobots (~> 0.0, >= 0.0.9)
net-http-digest_auth (1.1.1)
net-http-persistent (1.8)
nokogiri (1.5.0)
rake (0.9.2)
rcov (0.9.9)
rdoc (3.6.1)
rdoc (3.8)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
Expand All @@ -22,6 +27,8 @@ GEM
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
webrobots (0.0.10)
nokogiri (>= 1.4.4)

PLATFORMS
ruby
Expand Down
18 changes: 18 additions & 0 deletions README.rdoc
Expand Up @@ -20,8 +20,26 @@ Or, if you're feeling undecided, grab the entire library.

zhangmen all

Feeling lazy? Download the entire catalog.

zhangmen all

The songs will be downloaded in the current directory. One directory will be made for each artist, and all that artist's songs will be saved there.

If you're not in Mainland China, you can use a proxy to get your music fix.

http_proxy=google.for.a.proxy:1234 zhangmen fetch 602

== Testing

The tests are written in RSpec. Run them like this.

rake spec

If you're not in Mainland China, set http_proxy so the downloading tests pass.

http_proxy=google.for.a.proxy:1234 rake spec

== Known Issues

Some songs might not download. The Flash player skips over those songs too, so it seems to be a server issue.
Expand Down
6 changes: 5 additions & 1 deletion lib/zhangmen/cli.rb
Expand Up @@ -103,7 +103,11 @@ def save_client_cache

# Runs a command.
def run(args)
@client = Zhangmen::Client.new
options = {}
if proxy_server = ENV['http_proxy'] || ENV['all_proxy']
options[:proxy] = proxy_server
end
@client = Zhangmen::Client.new options
@client.cache = client_cache

case args[0]
Expand Down
15 changes: 12 additions & 3 deletions lib/zhangmen/client.rb
Expand Up @@ -8,8 +8,12 @@ module Zhangmen

# Wraps a client session for accessing Baidu's streaming music service.
class Client
def initialize
@mech = mechanizer
# New client session.
#
# The options hash accepts the following keys:
# :proxy:: "host:port" string
def initialize(options = {})
@mech = mechanizer options
@cache = {}
end

Expand Down Expand Up @@ -144,9 +148,14 @@ def hostname
end

# Mechanize instance customized to maximize fetch success.
def mechanizer
def mechanizer(options = {})
mech = Mechanize.new
mech.user_agent_alias = 'Linux Firefox'
if options[:proxy]
host, _, port_str = *options[:proxy].rpartition(':')
port_str ||= 80
mech.set_proxy host, port_str.to_i
end
mech
end
end # class Zhangmen::Client
Expand Down
24 changes: 23 additions & 1 deletion spec/zhangmen/client_spec.rb
Expand Up @@ -2,7 +2,29 @@
require File.expand_path('../../spec_helper', __FILE__)

describe Zhangmen::Client do
let(:client) { Zhangmen::Client.new }
describe 'mechanizer' do
let(:empty_client) { Zhangmen::Client.new }

it 'returns a Mechanize instance' do
empty_client.mechanizer.should be_kind_of(Mechanize)
end

describe 'with a proxy option' do
let(:mech) do
empty_client.mechanizer :proxy => '127.0.0.1:3306'
end

it 'parses the address correctly' do
mech.proxy_addr.should == '127.0.0.1'
end

it 'parses the port correctly' do
mech.proxy_port.should == 3306
end
end
end

let(:client) { Zhangmen::Client.new :proxy => ENV['http_proxy'] }

describe 'op_url' do
it 'encodes everything correctly' do
Expand Down

0 comments on commit 8e7aa6b

Please sign in to comment.