qingting_api 0.1.5 → 0.3.0
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.
- checksums.yaml +4 -4
- data/README.md +137 -51
- data/bin/console +14 -0
- data/lib/generators/qingting_api/config_generator.rb +2 -2
- data/lib/generators/qingting_api/templates/qingting_api_config.rb +1 -1
- data/lib/qingting/api/account.rb +44 -0
- data/lib/qingting/api/base.rb +62 -0
- data/lib/qingting/api/billboard.rb +40 -0
- data/lib/qingting/api/category.rb +51 -0
- data/lib/qingting/api/live.rb +42 -0
- data/lib/qingting/api/ondemand.rb +50 -0
- data/lib/qingting/api/podcaster.rb +73 -0
- data/lib/qingting/api/recent.rb +56 -0
- data/lib/qingting/config.rb +49 -0
- data/lib/qingting/utils/http.rb +69 -0
- data/lib/qingting/utils/request.rb +57 -0
- data/lib/qingting/version.rb +3 -0
- data/lib/qingting_api.rb +25 -11
- data/qingting_api.gemspec +9 -9
- metadata +21 -82
- data/.travis.yml +0 -7
- data/lib/qingting_api/account.rb +0 -38
- data/lib/qingting_api/config.rb +0 -44
- data/lib/qingting_api/qing_ting.rb +0 -70
- data/lib/qingting_api/request.rb +0 -52
- data/lib/qingting_api/version.rb +0 -3
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'qingting/api/base'
|
2
|
+
|
3
|
+
module Qingting
|
4
|
+
module Api
|
5
|
+
module Live
|
6
|
+
# extend self
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.send :extend, ClassMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
# Class methods
|
13
|
+
module ClassMethods
|
14
|
+
|
15
|
+
def channellive(id)
|
16
|
+
url = eval("Base.v6_channellives_#{id}")
|
17
|
+
Base.request(url)
|
18
|
+
end
|
19
|
+
|
20
|
+
def live_channel(id)
|
21
|
+
url = eval("Base.wapi_channels_#{id}")
|
22
|
+
Base.request(url)
|
23
|
+
end
|
24
|
+
|
25
|
+
def channellives(channel_id, days)
|
26
|
+
days = days.gsub(".","/")
|
27
|
+
|
28
|
+
url = Base.media_url + "channellives/#{channel_id}/programs/day/#{days}"
|
29
|
+
Base.request(url)
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
def nowplaying(day)
|
34
|
+
url = Base.media_url + "recommends/nowplaying/day/#{day}"
|
35
|
+
Base.request(url)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'qingting/api/base'
|
2
|
+
|
3
|
+
module Qingting
|
4
|
+
module Api
|
5
|
+
module Ondemand
|
6
|
+
# extend self
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.send :extend, ClassMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
# Class methods
|
13
|
+
module ClassMethods
|
14
|
+
|
15
|
+
def channelondemand(id)
|
16
|
+
url = eval("Base.v6_channelondemands_#{id}")
|
17
|
+
Base.request(url)
|
18
|
+
end
|
19
|
+
|
20
|
+
def channel(id)
|
21
|
+
url = eval("Base.wapi_channels_#{id}")
|
22
|
+
Base.request(url)
|
23
|
+
end
|
24
|
+
|
25
|
+
def wapi_programs(channel_id, current_page = Base.get_current_page, page_size = Base.get_page_size)
|
26
|
+
url = Base.wapi_url + "channelondemands/#{channel_id}/programs/order/0/curpage/#{current_page}/pagesize/#{page_size}"
|
27
|
+
Base.request(url)
|
28
|
+
end
|
29
|
+
|
30
|
+
def programs(channel_id, current_page = Base.get_current_page, page_size = Base.get_page_size)
|
31
|
+
url = Base.media_url + "channelondemands/#{channel_id}/programs/order/0/curpage/#{current_page}/pagesize/#{page_size}"
|
32
|
+
Base.request(url)
|
33
|
+
end
|
34
|
+
|
35
|
+
def program(id)
|
36
|
+
url = eval("Base.v6_programs_#{id}")
|
37
|
+
Base.request(url)
|
38
|
+
end
|
39
|
+
|
40
|
+
def recommends(channel_id, current_page = Base.get_current_page, page_size = Base.get_page_size)
|
41
|
+
url = Base.media_url + "channelondemands/#{channel_id}/recommends/curpage/#{current_page}/pagesize/#{page_size}"
|
42
|
+
Base.request(url)
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'qingting/api/base'
|
2
|
+
|
3
|
+
module Qingting
|
4
|
+
module Api
|
5
|
+
module Podcaster
|
6
|
+
# extend self
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.send :extend, ClassMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
# Class methods
|
13
|
+
module ClassMethods
|
14
|
+
|
15
|
+
def podcasters_guides
|
16
|
+
url = eval("Base.v6_podcasters_guides")
|
17
|
+
Base.request(url)
|
18
|
+
end
|
19
|
+
|
20
|
+
def podcasters_attributes
|
21
|
+
url = eval("Base.v6_podcasters_attributes")
|
22
|
+
Base.request(url)
|
23
|
+
end
|
24
|
+
|
25
|
+
def podcasters_type(type, page=1)
|
26
|
+
url = Base.media_url + "podcasters/type/#{type}/page/#{page} "
|
27
|
+
Base.request(url)
|
28
|
+
end
|
29
|
+
|
30
|
+
def podcasters_recommends
|
31
|
+
url = eval("Base.v6_podcasters_recommends")
|
32
|
+
Base.request(url)
|
33
|
+
end
|
34
|
+
|
35
|
+
def podcasters_attr(attr_ids, page=1)
|
36
|
+
url = Base.media_url + "podcasters/attr/#{attrids}/page/#{page} "
|
37
|
+
Base.request(url)
|
38
|
+
end
|
39
|
+
|
40
|
+
def podcaster(id)
|
41
|
+
url = eval("Base.v6_podcasters_#{id}")
|
42
|
+
Base.request(url)
|
43
|
+
end
|
44
|
+
|
45
|
+
def podcasters_channelondemands(qingting_id)
|
46
|
+
url = eval("Base.v6_podcasters_#{qingting_id}_channelondemands")
|
47
|
+
Base.request(url)
|
48
|
+
end
|
49
|
+
|
50
|
+
def podcasters_recent(qingting_id)
|
51
|
+
url = eval("Base.v6_podcasters_#{qingting_id}_recent")
|
52
|
+
Base.request(url)
|
53
|
+
end
|
54
|
+
|
55
|
+
def now
|
56
|
+
url = eval("Base.v6_now")
|
57
|
+
Base.request(url)
|
58
|
+
end
|
59
|
+
|
60
|
+
def topic(topic_id)
|
61
|
+
url = eval("Base.v6_topic_#{topic_id}")
|
62
|
+
Base.request(url)
|
63
|
+
end
|
64
|
+
|
65
|
+
def activity(activity_id)
|
66
|
+
url = eval("Base.v6_activity_#{activity_id}")
|
67
|
+
Base.request(url)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'qingting/api/base'
|
2
|
+
|
3
|
+
module Qingting
|
4
|
+
module Api
|
5
|
+
module Recent
|
6
|
+
# extend self
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.send :extend, ClassMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
# Class methods
|
13
|
+
module ClassMethods
|
14
|
+
|
15
|
+
def categories_recent(start_time, end_time)
|
16
|
+
url = Base.media_url + "categories/recent/starttime/#{start_time}/endtime/#{end_time}"
|
17
|
+
Base.request(url)
|
18
|
+
end
|
19
|
+
|
20
|
+
def channels_recent(category_id, start_time, end_time, current_page = Base.get_current_page, page_size = Base.get_page_size)
|
21
|
+
url = Base.media_url + "categories/#{category_id}/recent/channels/starttime/#{start_time}/endtime/#{end_time}/curpage/#{current_page}/pagesize/#{page_size}"
|
22
|
+
Base.request(url)
|
23
|
+
end
|
24
|
+
|
25
|
+
def channelondemands_recent(channelondemand_id, start_time, end_time, current_page = Base.get_current_page, page_size = Base.get_page_size)
|
26
|
+
url = Base.media_url + "categories/#{channelondemand_id}/recent/channelondemands/starttime/#{start_time}/endtime/#{end_time}/curpage/#{current_page}/pagesize/#{page_size}"
|
27
|
+
Base.request(url)
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def live_recent(start_time, end_time, current_page = Base.get_current_page, page_size = Base.get_page_size)
|
32
|
+
url = Base.media_url + "categories/live/recent/channels/starttime/#{start_time}/endtime/#{end_time}/curpage/#{current_page}/pagesize/#{page_size}"
|
33
|
+
Base.request(url)
|
34
|
+
end
|
35
|
+
|
36
|
+
def newsearch(keyword, type)
|
37
|
+
url = Base.base_url + "newsearch/#{keyword}/type/#{type}"
|
38
|
+
Base.request(url)
|
39
|
+
end
|
40
|
+
|
41
|
+
def search_hotkeywords
|
42
|
+
url = Base.base_url + "search/hotkeywords"
|
43
|
+
Base.request(url)
|
44
|
+
end
|
45
|
+
|
46
|
+
def suggest(keyword)
|
47
|
+
url = Base.base_url + "/newsearch/suggest?k=#{keyword}"
|
48
|
+
Base.request(url)
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Qingting
|
4
|
+
|
5
|
+
class Config
|
6
|
+
@props = {
|
7
|
+
:base_url => "http://api.open.qingting.fm",
|
8
|
+
:client_id => "ZTk4MzAwMzAtYWFiOC0xMWU0LTkyM2YtMDAxNjNlMDAyMGFk",
|
9
|
+
:client_secret => "OTdiNmI0MWEtYzhiZC0zYWE1LWExZmEtMDU0OWZhNTljZmRk",
|
10
|
+
:grant_type => "client_credentials",
|
11
|
+
:api_version => :v6,
|
12
|
+
:current_page => 1,
|
13
|
+
:page_size => 30,
|
14
|
+
:company => 'Cheenwe'
|
15
|
+
}
|
16
|
+
|
17
|
+
|
18
|
+
class << self
|
19
|
+
attr_accessor :props
|
20
|
+
|
21
|
+
def configure
|
22
|
+
yield props if block_given?
|
23
|
+
end
|
24
|
+
|
25
|
+
# Get a configuration property given a specified location, example usage: Config::get('auth.token_endpoint')
|
26
|
+
# @param [String] index - location of the property to obtain
|
27
|
+
# @return [String]
|
28
|
+
def get(index)
|
29
|
+
properties = index.split('.')
|
30
|
+
get_value(properties, props)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
# Navigate through a config array looking for a particular index
|
36
|
+
# @param [Array] index The index sequence we are navigating down
|
37
|
+
# @param [Hash, String] value The portion of the config array to process
|
38
|
+
# @return [String]
|
39
|
+
def get_value(index, value)
|
40
|
+
index = index.is_a?(Array) ? index : [index]
|
41
|
+
key = index.shift.to_sym
|
42
|
+
value.is_a?(Hash) and value[key] and value[key].is_a?(Hash) ?
|
43
|
+
get_value(index, value[key]) :
|
44
|
+
value[key]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Qingting
|
5
|
+
module Utils
|
6
|
+
class Http
|
7
|
+
|
8
|
+
DEFAULT_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36"
|
9
|
+
DEFAULT_OPEN_TIMEOUT = 20
|
10
|
+
DEFAULT_READ_TIMEOUT = 120
|
11
|
+
DEFAULT_RETRY_TIMES = 3
|
12
|
+
RETRY_SLEEP_TIME = 3
|
13
|
+
|
14
|
+
HTTP_VERB_MAP = {
|
15
|
+
get: Net::HTTP::Get,
|
16
|
+
post: Net::HTTP::Post,
|
17
|
+
put: Net::HTTP::Put,
|
18
|
+
delete: Net::HTTP::Delete
|
19
|
+
}
|
20
|
+
|
21
|
+
DEFAULT_HEADERS = {
|
22
|
+
'user-agent' => DEFAULT_USER_AGENT,
|
23
|
+
'accept' => 'application/json',
|
24
|
+
'content-type' => 'application/json',
|
25
|
+
'connection' => 'close'
|
26
|
+
}
|
27
|
+
|
28
|
+
def initialize(method, url, params: nil, body: nil, headers: {}, opts: {})
|
29
|
+
method = method.downcase.to_sym
|
30
|
+
err_msg = "http method #{method} is not supported"
|
31
|
+
raise err_msg unless HTTP_VERB_MAP.keys.include?(method)
|
32
|
+
@uri = URI(url)
|
33
|
+
@uri.query = URI.encode_www_form(params) unless params.nil?
|
34
|
+
@request = prepare_request(method, body, headers)
|
35
|
+
@opts = opts
|
36
|
+
end
|
37
|
+
|
38
|
+
def send_request
|
39
|
+
tries ||= DEFAULT_RETRY_TIMES
|
40
|
+
opts ||= default_opts.merge @opts
|
41
|
+
Net::HTTP.start(@uri.host, @uri.port, opts) do |http|
|
42
|
+
http.request(@request)
|
43
|
+
end
|
44
|
+
# if raise Timeout::Error retry it for 3 times
|
45
|
+
rescue Net::OpenTimeout, Net::ReadTimeout
|
46
|
+
(tries -= 1).zero? ? (raise "Time out") : retry
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def prepare_request(method, body, headers)
|
52
|
+
headers = DEFAULT_HEADERS.merge(headers)
|
53
|
+
request = HTTP_VERB_MAP[method].new @uri
|
54
|
+
request.initialize_http_header(headers)
|
55
|
+
request.body = body.to_json unless body.nil?
|
56
|
+
request
|
57
|
+
end
|
58
|
+
|
59
|
+
def default_opts
|
60
|
+
{
|
61
|
+
use_ssl: 'https' == @uri.scheme,
|
62
|
+
open_timeout: DEFAULT_OPEN_TIMEOUT,
|
63
|
+
read_timeout: DEFAULT_READ_TIMEOUT
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'net/http'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Qingting
|
6
|
+
module Utils
|
7
|
+
module Request
|
8
|
+
extend self
|
9
|
+
|
10
|
+
def get(url, params: nil, headers: {})
|
11
|
+
send_request(:get, url, params: params, headers: headers)
|
12
|
+
end
|
13
|
+
|
14
|
+
def post(url, body: {}, params: {}, headers: {})
|
15
|
+
send_request(:post, url, params: params, body: body, headers: headers)
|
16
|
+
end
|
17
|
+
|
18
|
+
def put(url, body: {}, headers: {})
|
19
|
+
send_request(:put, url, body: body, headers: headers)
|
20
|
+
end
|
21
|
+
|
22
|
+
def delete(url, params: nil, headers: {})
|
23
|
+
send_request(:delete, url, params: params, headers: headers)
|
24
|
+
end
|
25
|
+
|
26
|
+
def send_request(method, url, params: nil, body: nil, headers: {}, opts: {})
|
27
|
+
|
28
|
+
# puts ">>>>>>>http start>>>>>>>>>"
|
29
|
+
# puts "<method> #{method}"
|
30
|
+
# puts "<url> #{url}"
|
31
|
+
# puts "<params> #{params}"
|
32
|
+
# puts "<body> #{body}"
|
33
|
+
# puts "<headers> #{headers}"
|
34
|
+
# puts "<opts> #{opts}"
|
35
|
+
# puts ">>>>>>>http end>>>>>>>>>"
|
36
|
+
|
37
|
+
raw_response = Utils::Http.new(
|
38
|
+
method.to_sym,
|
39
|
+
url,
|
40
|
+
params: params,
|
41
|
+
body: body,
|
42
|
+
headers: headers,
|
43
|
+
opts: opts
|
44
|
+
).send_request
|
45
|
+
|
46
|
+
# puts ">>>>>>>>>>.#{raw_response}"
|
47
|
+
|
48
|
+
parse_body(raw_response.body)
|
49
|
+
end
|
50
|
+
|
51
|
+
def parse_body(body)
|
52
|
+
JSON.parse(body)
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/lib/qingting_api.rb
CHANGED
@@ -1,17 +1,31 @@
|
|
1
|
-
require "
|
2
|
-
require '
|
3
|
-
require
|
4
|
-
require '
|
1
|
+
require "qingting/version"
|
2
|
+
require 'qingting/config'
|
3
|
+
require "qingting/utils/http"
|
4
|
+
require 'qingting/utils/request'
|
5
|
+
require 'qingting/qing_ting'
|
6
|
+
require 'qingting/api/base'
|
7
|
+
require 'qingting/api/account'
|
8
|
+
require 'qingting/api/category'
|
9
|
+
require 'qingting/api/ondemand'
|
10
|
+
require 'qingting/api/live'
|
11
|
+
require 'qingting/api/podcaster'
|
12
|
+
require 'qingting/api/billboard'
|
13
|
+
require 'qingting/api/recent'
|
5
14
|
|
6
|
-
require 'qingting_api/account'
|
7
15
|
|
8
|
-
unless defined? ActiveRecord
|
9
|
-
begin
|
10
|
-
require 'active_record'
|
11
|
-
rescue LoadError; end
|
12
|
-
end
|
13
16
|
|
14
17
|
module QingtingApi
|
15
|
-
include
|
18
|
+
include Qingting
|
19
|
+
include Qingting::Utils
|
20
|
+
include Qingting::Api
|
21
|
+
include Qingting::Api::Account
|
22
|
+
include Qingting::Api::Category
|
23
|
+
include Qingting::Api::Ondemand
|
24
|
+
include Qingting::Api::Live
|
25
|
+
include Qingting::Api::Podcaster
|
26
|
+
include Qingting::Api::Billboard
|
27
|
+
include Qingting::Api::Recent
|
28
|
+
|
29
|
+
|
16
30
|
|
17
31
|
end
|