xiami 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d642a22309e30720cbf1fc99ae7a359be008ba6e
4
+ data.tar.gz: 18bbd20112a9cb47b3afeda855c8e43a414433e9
5
+ SHA512:
6
+ metadata.gz: a32e3ffc7d37ec7b1acfbe06deb32c21884dca7b956c2a79e6506edd50ae7f2bb3442651a0658a92037fb4fb2b7d3922afa59f17ccf3936495e83b595c619445
7
+ data.tar.gz: 2f4e4bc91fdb5794cee6a98f64d8c57947866c7bc70c3b38c55d1c7d9ee1c27fee894ca7642c2dab870ce1e897cbcd51a2dcb1be6da02723530386a98ff61c46
@@ -0,0 +1,3 @@
1
+ .env
2
+ pkg
3
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'http://ruby.taobao.org'
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'rake'
7
+ gem 'nokogiri'
8
+ gem 'rspec', '~> 2.12.0'
9
+ gem 'webmock'
10
+ end
@@ -0,0 +1,3 @@
1
+ # xiami gem
2
+
3
+ retrieve songs from xiami.com
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,4 @@
1
+ require "xiami/version"
2
+ require "xiami/song"
3
+ require "xiami/album"
4
+ require "xiami/artist"
@@ -0,0 +1,5 @@
1
+ module Xiami
2
+ class Album
3
+ attr_accessor :name, :id, :cover_url
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Xiami
2
+ class Artist
3
+ attr_accessor :name, :id
4
+ end
5
+ end
@@ -0,0 +1,76 @@
1
+ # originally from xiami_sauce gem
2
+ require "cgi"
3
+ require 'net/http'
4
+ require 'nokogiri'
5
+
6
+ module Xiami
7
+ class Song
8
+ attr_accessor :id, :name, :temporary_url
9
+ attr_accessor :artist, :album
10
+
11
+ attr_accessor :local_file_path
12
+
13
+ def initialize(song_url = nil)
14
+ if song_url
15
+ @id = song_url.match(/song\/([0-9]+)/)[1] rescue song_url
16
+
17
+ parse_info!
18
+ end
19
+ end
20
+
21
+ def ==(another)
22
+ return false if another.nil?
23
+
24
+ self.id == another.id
25
+ end
26
+
27
+ def parse_info!
28
+ doc = Nokogiri::XML(info_xml)
29
+
30
+ @id = doc.at_css('track song_id').content
31
+ @name = doc.at_css('track song_name').content
32
+
33
+ @album = Album.new.tap do |album|
34
+ album.id = doc.at_css('track album_id').content
35
+ album.name = doc.at_css('track album_name').content
36
+ album.cover_url = doc.at_css('track album_cover').content
37
+ end
38
+
39
+ @artist = Artist.new.tap do |artist|
40
+ artist.id = doc.at_css('track artist_id').content
41
+ artist.name = doc.at_css('track artist_name').content
42
+ end
43
+
44
+ @temporary_url = sospa(doc.at_css('track location').content)
45
+ end
46
+
47
+ private
48
+
49
+ def info_xml
50
+ uri = URI.parse("http://www.xiami.com/widget/xml-single/uid/0/sid/#{id}")
51
+
52
+ headers = { 'accept' => 'text/html', 'user-agent' => 'Mozilla/5.0' }
53
+
54
+ Net::HTTP.new(uri.host, uri.port).get2(uri.path, headers).body
55
+ end
56
+
57
+ def sospa(location)
58
+ string = location[1..-1]
59
+ col = location[0].to_i
60
+ row = (string.length.to_f / col).floor
61
+ remainder = string.length % col
62
+ address = [[nil] * col] * (row + 1)
63
+
64
+ sizes = [row+1] * remainder + [row] * (col - remainder)
65
+ pos = 0
66
+ sizes.each_with_index do |size, i|
67
+ size.times { |index| address[col * index + i] = string[pos + index] }
68
+ pos += size
69
+ end
70
+
71
+ address = CGI::unescape(address.join).gsub('^', '0')
72
+ rescue
73
+ raise location
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,3 @@
1
+ module Xiami
2
+ VERSION = '0.0.3'
3
+ end
@@ -0,0 +1,21 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <trackList>
3
+ <config>
4
+ <copyright>1</copyright>
5
+ </config>
6
+ <track>
7
+ <width>257</width>
8
+ <height>33</height>
9
+ <song_name><![CDATA[J에게]]></song_name>
10
+ <song_id>3495768</song_id>
11
+ <user_id>0</user_id>
12
+ <album_id>315098</album_id>
13
+ <album_cover><![CDATA[http://img.xiami.net/images/album/img4/5104/3150981231826033_3.jpg]]></album_cover>
14
+ <album_name><![CDATA[My Life + Best]]></album_name>
15
+ <artist_id>5104</artist_id>
16
+ <artist_name><![CDATA[이선희]]></artist_name>
17
+ <location><![CDATA[4h%2Ff.moFEF5259F5_E3l3a_%b5543d8aEa14E--lt3Fmixim145EF%837174.%uk34E254a%9b-44%%ntA%5li.%%%1435%46%21m3teD7ac7eb5%814%55up%2.eac252%%1E29859_pFhy4%6429bE5b455EEl]]></location>
18
+
19
+ <widgetCode><![CDATA[<embed src="http://www.xiami.com/widget/0_3495768/singlePlayer.swf" type="application/x-shockwave-flash" width="257" height="33" wmode="transparent"></embed>]]></widgetCode>
20
+ </track>
21
+ </trackList>
@@ -0,0 +1,18 @@
1
+ require "xiami"
2
+
3
+ require "webmock"
4
+ WebMock.disable_net_connect!
5
+
6
+ def fixture(path)
7
+ File.new(File.expand_path("../fixtures/#{path}.xml", __FILE__))
8
+ end
9
+
10
+ include WebMock::API
11
+
12
+ pattern = /http:\/\/www\.xiami\.com\/widget\/xml-single\/uid\/0\/sid\/(\d+)/
13
+
14
+ result = lambda do |request|
15
+ { body: fixture(request.uri.path.match(/(\d+)$/)[0]) }
16
+ end
17
+
18
+ stub_request(:any, pattern).to_return(result)
@@ -0,0 +1,8 @@
1
+ require "spec_helper"
2
+
3
+ module Xiami
4
+ describe Album do
5
+ it { should respond_to :name }
6
+ it { should respond_to :id }
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require "spec_helper"
2
+
3
+ module Xiami
4
+ describe Artist do
5
+ it { should respond_to :name }
6
+ it { should respond_to :id }
7
+ end
8
+ end
@@ -0,0 +1,47 @@
1
+ # coding: utf-8
2
+ require "spec_helper"
3
+
4
+ module Xiami
5
+ describe Song do
6
+ it { should respond_to :name }
7
+ it { should respond_to :artist }
8
+ it { should respond_to :album }
9
+ it { should respond_to :local_file_path }
10
+ it { should respond_to :local_file_path= }
11
+
12
+ describe '#new' do
13
+ it 'parses data' do
14
+ song = Song.new('http://www.xiami.com/song/3495768')
15
+
16
+ song.temporary_url.should == 'http://m5.file.xiami.com/104/5104/315098/3495768_10729341_l.mp3?auth_key=4b470a652c4457234e9dabb80a90b8ba-1414454400-0-null'
17
+
18
+ song.album.id.should == '315098'
19
+ song.album.name.should == 'My Life + Best'
20
+ song.album.cover_url.should == 'http://img.xiami.net/images/album/img4/5104/3150981231826033_3.jpg'
21
+
22
+ song.artist.id.should == '5104'
23
+ song.artist.name.should == '이선희'
24
+ end
25
+
26
+ it 'accepts song id' do
27
+ song = Song.new('3495768')
28
+
29
+ song.album.id.should == '315098'
30
+ end
31
+ end
32
+
33
+ describe '#==' do
34
+ let(:song1) { Song.new.tap { |song| song.id = '123' } }
35
+ let(:song2) { Song.new.tap { |song| song.id = '123' } }
36
+ let(:song3) { Song.new.tap { |song| song.id = '1234' } }
37
+ let(:song4) { Song.new.tap { |song| song.id = nil } }
38
+
39
+ it 'test equality base on id' do
40
+ song1.should == song2
41
+ song2.should_not == song3
42
+ song1.should_not == nil
43
+ song1.should_not == song4
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'xiami/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "xiami"
8
+ spec.version = Xiami::VERSION
9
+ spec.authors = ["Forrest Ye"]
10
+ spec.email = ["afu@forresty.com"]
11
+ spec.summary = %q{ retrieve songs from http://xiami.com }
12
+ spec.homepage = "https://github.com/forresty/xiami"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xiami
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Forrest Ye
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - afu@forresty.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - lib/xiami.rb
53
+ - lib/xiami/album.rb
54
+ - lib/xiami/artist.rb
55
+ - lib/xiami/song.rb
56
+ - lib/xiami/version.rb
57
+ - spec/fixtures/3495768.xml
58
+ - spec/spec_helper.rb
59
+ - spec/xiami/album_spec.rb
60
+ - spec/xiami/artist_spec.rb
61
+ - spec/xiami/song_spec.rb
62
+ - xiami.gemspec
63
+ homepage: https://github.com/forresty/xiami
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.0.2
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: retrieve songs from http://xiami.com
87
+ test_files:
88
+ - spec/fixtures/3495768.xml
89
+ - spec/spec_helper.rb
90
+ - spec/xiami/album_spec.rb
91
+ - spec/xiami/artist_spec.rb
92
+ - spec/xiami/song_spec.rb