topdmc-client 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +44 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/topdmc/resources/album.rb +19 -0
- data/lib/topdmc/resources/artist.rb +27 -0
- data/lib/topdmc/resources/base.rb +80 -0
- data/lib/topdmc/resources/client.rb +34 -0
- data/lib/topdmc/resources/search.rb +17 -0
- data/lib/topdmc/resources/track.rb +35 -0
- data/lib/topdmc/resources/utils.rb +20 -0
- data/lib/topdmc/resources/version.rb +5 -0
- data/lib/topdmc/topdmc.rb +2 -0
- data/topdmc-client.gemspec +32 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4f68b09ef7b59d1381229933e3f80a23b828181f
|
4
|
+
data.tar.gz: 461446b46919ec3b6415dc2d8e93f5d20bc20b48
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5571b4958e4d061efce87755b053f1bb59a8196e4431b37affc46cda0daccbe830f4746f81a860456e3c84f5ca7746b973667e601296e4d13e33c5f0b34835e9
|
7
|
+
data.tar.gz: f4a592b116f4d07a2693f36d221f8134fbaaf0853241be7552b02a9835001d97b2a10da85342c815c9f66bfbe04332796dc92bc25b28701394dd03ee2861ecfc
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 liangyali
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Topdmc::Client
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/topdmc/client`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'topdmc-client'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install topdmc-client
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
require 'topdmc/resources/client'
|
26
|
+
|
27
|
+
client = TopDMC::Client.new :client_id => '',:client_secure=>''
|
28
|
+
albums = client.artist.get('eEkZ3o')
|
29
|
+
|
30
|
+
## Development
|
31
|
+
|
32
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
33
|
+
|
34
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/topdmc-client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
39
|
+
|
40
|
+
|
41
|
+
## License
|
42
|
+
|
43
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
44
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "topdmc/topdmc"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'topdmc/resources/base'
|
2
|
+
|
3
|
+
module TopDMC
|
4
|
+
module Resources
|
5
|
+
class Album < Base
|
6
|
+
def initialize(client)
|
7
|
+
super(client)
|
8
|
+
end
|
9
|
+
|
10
|
+
def find(id)
|
11
|
+
perform_request('GET', "/v1/albums/#{id}")
|
12
|
+
end
|
13
|
+
|
14
|
+
def find_all_by_ids(ids)
|
15
|
+
perform_request('GET', "/v1/albums", {ids: ids.join(',')})
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'topdmc/resources/base'
|
2
|
+
|
3
|
+
module TopDMC
|
4
|
+
module Resources
|
5
|
+
class Artist < Base
|
6
|
+
def initialize(client)
|
7
|
+
super(client)
|
8
|
+
end
|
9
|
+
|
10
|
+
def find(id)
|
11
|
+
perform_request('GET', "/v1/artists/#{id}")
|
12
|
+
end
|
13
|
+
|
14
|
+
def find_all_by_ids(ids)
|
15
|
+
perform_request('GET', "/v1/artists", {ids: ids.join(',')})
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_albums(id)
|
19
|
+
perform_request('GET', "/v1/artists/#{id}/albums")
|
20
|
+
end
|
21
|
+
|
22
|
+
def et_tracks(id)
|
23
|
+
perform_request('GET', "/v1/artists/#{id}/tracks")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'json'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
require 'topdmc/resources/utils'
|
6
|
+
|
7
|
+
module TopDMC
|
8
|
+
module Resources
|
9
|
+
|
10
|
+
class Base
|
11
|
+
include TopDMC::Resources::Utils
|
12
|
+
|
13
|
+
def initialize(client)
|
14
|
+
@client = client
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def perform_request(request_method, path, params={})
|
19
|
+
|
20
|
+
# 处理请求参数
|
21
|
+
base_params ={
|
22
|
+
timestamp: Time.now.to_i,
|
23
|
+
nonce: nonce,
|
24
|
+
client_id: @client.client_id
|
25
|
+
}
|
26
|
+
|
27
|
+
# get请求合并参数
|
28
|
+
if request_method=='GET' then
|
29
|
+
base_params.merge! params
|
30
|
+
end
|
31
|
+
|
32
|
+
# 对参数名称和参数值进行URL编码
|
33
|
+
encoded_params = base_params.map do |key, value|
|
34
|
+
[key, URI.encode_www_form_component(value.to_s)]
|
35
|
+
end
|
36
|
+
|
37
|
+
# 按参数名进行升序排列,如果key值相同之后按照value进行排序
|
38
|
+
sorted_params = encoded_params.sort
|
39
|
+
|
40
|
+
# 参数名和参数值之间用 “=” 号连接,参数和参数之间用 “&” 号连接
|
41
|
+
normalized = sorted_params.map do |item|
|
42
|
+
item.join('=')
|
43
|
+
end
|
44
|
+
|
45
|
+
sign_str = [request_method,
|
46
|
+
URI.encode_www_form_component(path),
|
47
|
+
URI.encode_www_form_component(normalized.join('&'))
|
48
|
+
].join('&')
|
49
|
+
|
50
|
+
sign = hmac_sha1_signature(@client.client_secure, sign_str)
|
51
|
+
base_params[:sign] = sign
|
52
|
+
|
53
|
+
# 处理请求地址
|
54
|
+
path = "#{@client.url}#{path}"
|
55
|
+
|
56
|
+
payload={}
|
57
|
+
payload = params if request_method=='POST'
|
58
|
+
|
59
|
+
begin
|
60
|
+
response = RestClient::Request.execute(
|
61
|
+
method: request_method,
|
62
|
+
url: path,
|
63
|
+
headers: {
|
64
|
+
params: base_params,
|
65
|
+
payload: payload,
|
66
|
+
:content_type => :json,
|
67
|
+
:accept => :json}
|
68
|
+
)
|
69
|
+
return JSON.parse(response.body)
|
70
|
+
rescue => e
|
71
|
+
begin
|
72
|
+
return JSON.parse(e.response.body)
|
73
|
+
rescue => ex
|
74
|
+
raise ex
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'topdmc/resources/artist'
|
2
|
+
require "topdmc/resources/version"
|
3
|
+
|
4
|
+
module TopDMC
|
5
|
+
class Client
|
6
|
+
attr_accessor :client_id, :client_secure , :url
|
7
|
+
attr_writer :user_agent
|
8
|
+
|
9
|
+
def initialize(options={})
|
10
|
+
@client_id = options[:client_id]
|
11
|
+
@client_secure = options[:client_secure]
|
12
|
+
@url = options[:url]||'http://openapi.topdmc.cn'
|
13
|
+
|
14
|
+
yield(self) if block_given?
|
15
|
+
end
|
16
|
+
|
17
|
+
def user_agent
|
18
|
+
@user_agent ||= "TopDMC/#{TopDMC::Client::VERSION}"
|
19
|
+
end
|
20
|
+
|
21
|
+
def artist
|
22
|
+
TopDMC::Resources::Artist.new(self)
|
23
|
+
end
|
24
|
+
|
25
|
+
def album
|
26
|
+
TopDMC::Resources::Album.new(self)
|
27
|
+
end
|
28
|
+
|
29
|
+
def search
|
30
|
+
TopDMC::Resources::Search.new(self)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'topdmc/resources/base'
|
2
|
+
|
3
|
+
module TopDMC
|
4
|
+
module Client
|
5
|
+
module Resources
|
6
|
+
class Search < Base
|
7
|
+
def initialize(client)
|
8
|
+
super(client)
|
9
|
+
end
|
10
|
+
|
11
|
+
def find_all_by_query(query={})
|
12
|
+
perform_request('GET', "/v1/search", query)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'topdmc/resources/base'
|
2
|
+
|
3
|
+
module TopDMC
|
4
|
+
module Client
|
5
|
+
class Track < Base
|
6
|
+
def initialize(client)
|
7
|
+
super(client)
|
8
|
+
end
|
9
|
+
|
10
|
+
def find(id)
|
11
|
+
perform_request('GET', "/v1/tracks/#{id}")
|
12
|
+
end
|
13
|
+
|
14
|
+
def find_all_by_ids(ids)
|
15
|
+
perform_request('GET', "/v1/tracks", {ids: ids.join(',')})
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_mapping(id)
|
19
|
+
perform_request('POST', "/v1/tracks/#{id}/mapping")
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_mapping(id, params)
|
23
|
+
perform_request('GET', "/v1/tracks/#{id}/mapping", params)
|
24
|
+
end
|
25
|
+
|
26
|
+
def delete_mapping(id)
|
27
|
+
perform_request('DELETE', "/v1/tracks/#{id}/mapping")
|
28
|
+
end
|
29
|
+
|
30
|
+
def update_offline_play_count(id, params)
|
31
|
+
perform_request('PUT', "/v1/tracks/#{id}", params)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'openssl'
|
3
|
+
require 'cgi'
|
4
|
+
|
5
|
+
module TopDMC
|
6
|
+
module Resources
|
7
|
+
module Utils
|
8
|
+
def hmac_sha1_signature(key, signature_string)
|
9
|
+
digest = OpenSSL::Digest::Digest.new( 'sha1' )
|
10
|
+
hmac = OpenSSL::HMAC.digest( digest, key, signature_string )
|
11
|
+
Base64.encode64( hmac ).chomp.gsub( /\n/, '' )
|
12
|
+
end
|
13
|
+
|
14
|
+
def nonce
|
15
|
+
rand(10 ** 30).to_s.rjust(30,'0')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'topdmc/resources/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "topdmc-client"
|
8
|
+
spec.version = TopDMC::Resources::VERSION
|
9
|
+
spec.authors = ["liangyali"]
|
10
|
+
spec.email = ["me@liangyali.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{TopDMC OpenAPI client}
|
13
|
+
spec.description = %q{TopDMC OpenAPI client}
|
14
|
+
spec.homepage = "http://www.topdmc.com"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "'http://mygemserver.com'"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: topdmc-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- liangyali
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-16 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
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: TopDMC OpenAPI client
|
42
|
+
email:
|
43
|
+
- me@liangyali.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- .travis.yml
|
50
|
+
- CODE_OF_CONDUCT.md
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- bin/console
|
56
|
+
- bin/setup
|
57
|
+
- lib/topdmc/resources/album.rb
|
58
|
+
- lib/topdmc/resources/artist.rb
|
59
|
+
- lib/topdmc/resources/base.rb
|
60
|
+
- lib/topdmc/resources/client.rb
|
61
|
+
- lib/topdmc/resources/search.rb
|
62
|
+
- lib/topdmc/resources/track.rb
|
63
|
+
- lib/topdmc/resources/utils.rb
|
64
|
+
- lib/topdmc/resources/version.rb
|
65
|
+
- lib/topdmc/topdmc.rb
|
66
|
+
- topdmc-client.gemspec
|
67
|
+
homepage: http://www.topdmc.com
|
68
|
+
licenses:
|
69
|
+
- MIT
|
70
|
+
metadata:
|
71
|
+
allowed_push_host: '''http://mygemserver.com'''
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 2.0.14
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: TopDMC OpenAPI client
|
92
|
+
test_files: []
|