bilibili_console 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.bundle/config +2 -2
- data/.gitignore +4 -3
- data/Gemfile +10 -10
- data/Gemfile.lock +3 -9
- data/LICENSE +20 -20
- data/README.md +106 -75
- data/Rakefile +11 -11
- data/bilibili_console.gemspec +21 -21
- data/bin/banner.txt +9 -9
- data/bin/bilic +123 -0
- data/lib/bilibili_console/api.rb +30 -0
- data/lib/bilibili_console/base.rb +109 -109
- data/lib/bilibili_console/fav.rb +154 -147
- data/lib/bilibili_console/http/http.rb +75 -75
- data/lib/bilibili_console/http/response_body.rb +28 -28
- data/lib/bilibili_console/login.rb +103 -102
- data/lib/bilibili_console/manga.rb +24 -24
- data/lib/bilibili_console/version.rb +10 -10
- data/lib/bilibili_console/video.rb +193 -123
- data/lib/bilibili_console.rb +69 -69
- data/rubocop.yml +1 -1
- metadata +9 -36
- data/bin/bili-console +0 -63
@@ -1,109 +1,109 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Copyright (c) 2021 leejoker
|
4
|
-
#
|
5
|
-
# This software is released under the MIT License.
|
6
|
-
# https://opensource.org/licenses/MIT
|
7
|
-
|
8
|
-
require_relative 'http/http'
|
9
|
-
require 'fileutils'
|
10
|
-
require 'json'
|
11
|
-
|
12
|
-
# bilibili base
|
13
|
-
module Bilibili
|
14
|
-
include BiliHttp
|
15
|
-
# base class
|
16
|
-
class BilibiliBase
|
17
|
-
attr_accessor :http_client, :options
|
18
|
-
|
19
|
-
class << self
|
20
|
-
attr_accessor :video_qn
|
21
|
-
end
|
22
|
-
|
23
|
-
def initialize(http_client)
|
24
|
-
@http_client = http_client
|
25
|
-
create_request_methods
|
26
|
-
@options = {
|
27
|
-
'config_path' => '~/.bc', 'config_file' => '~/.bc/config.json',
|
28
|
-
'cookie_file' => '~/.bc/cookie.txt', 'download_path' => '~/.bc/download'
|
29
|
-
}
|
30
|
-
BilibiliBase.video_qn = {
|
31
|
-
'240' => 6, '360' => 16, '480' => 32, '720' => 64, '720P60' => 74, '1080' => 80,
|
32
|
-
'1080+' => 112, '1080P60' => 116, '4K' => 120, 'HDR' => 125
|
33
|
-
}
|
34
|
-
end
|
35
|
-
|
36
|
-
def save_cookie
|
37
|
-
check_config_path
|
38
|
-
|
39
|
-
@http_client.api_http.cookies = @http_client.login_http.cookies
|
40
|
-
json_str = @http_client.login_http.cookies.to_json
|
41
|
-
write_cookie(json_str)
|
42
|
-
end
|
43
|
-
|
44
|
-
def load_cookie
|
45
|
-
return {} unless File.exist?(cookie_path)
|
46
|
-
|
47
|
-
json_str = File.read(cookie_path)
|
48
|
-
return {} if json_str.nil? || json_str.empty?
|
49
|
-
|
50
|
-
JSON.parse(json_str)
|
51
|
-
end
|
52
|
-
|
53
|
-
def clean_cookie
|
54
|
-
File.open(cookie_path, 'w') do |file|
|
55
|
-
file.write('{}')
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
def check_config_path
|
62
|
-
FileUtils.mkdir_p(config_path) unless Dir.exist?(config_path)
|
63
|
-
end
|
64
|
-
|
65
|
-
def write_cookie(cookie)
|
66
|
-
File.open(cookie_path, 'w') do |file|
|
67
|
-
file.write(cookie)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def create_request_methods
|
72
|
-
methods = %w[login_http api_http manga_http]
|
73
|
-
methods.each do |method|
|
74
|
-
define_get_json_method(method)
|
75
|
-
define_post_form_json_method(method)
|
76
|
-
define_post_json_method(method)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def define_get_json_method(method_name)
|
81
|
-
new_method_name = "get_json#{method_name[0]}"
|
82
|
-
BilibiliBase.define_method(new_method_name) do |url|
|
83
|
-
@http_client.get_json(@http_client.instance_variable_get("@#{method_name}"), url)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def define_post_form_json_method(method_name)
|
88
|
-
new_method_name = "post_form_json#{method_name[0]}"
|
89
|
-
BilibiliBase.define_method(new_method_name) do |url, params|
|
90
|
-
@http_client.post_form_json(@http_client.instance_variable_get("@#{method_name}"), url, params)
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
def define_post_json_method(method_name)
|
95
|
-
new_method_name = "post_json#{method_name[0]}"
|
96
|
-
BilibiliBase.define_method(new_method_name) do |url, headers, req_body|
|
97
|
-
@http_client.post_json(@http_client.instance_variable_get("@#{method_name}"), url, headers, req_body)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
def config_path
|
102
|
-
File.expand_path(@options['config_path'].to_s, __dir__)
|
103
|
-
end
|
104
|
-
|
105
|
-
def cookie_path
|
106
|
-
File.expand_path(@options['cookie_file'].to_s, __FILE__)
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright (c) 2021 leejoker
|
4
|
+
#
|
5
|
+
# This software is released under the MIT License.
|
6
|
+
# https://opensource.org/licenses/MIT
|
7
|
+
|
8
|
+
require_relative 'http/http'
|
9
|
+
require 'fileutils'
|
10
|
+
require 'json'
|
11
|
+
|
12
|
+
# bilibili base
|
13
|
+
module Bilibili
|
14
|
+
include BiliHttp
|
15
|
+
# base class
|
16
|
+
class BilibiliBase
|
17
|
+
attr_accessor :http_client, :options
|
18
|
+
|
19
|
+
class << self
|
20
|
+
attr_accessor :video_qn
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(http_client)
|
24
|
+
@http_client = http_client
|
25
|
+
create_request_methods
|
26
|
+
@options = {
|
27
|
+
'config_path' => '~/.bc', 'config_file' => '~/.bc/config.json',
|
28
|
+
'cookie_file' => '~/.bc/cookie.txt', 'download_path' => '~/.bc/download'
|
29
|
+
}
|
30
|
+
BilibiliBase.video_qn = {
|
31
|
+
'240' => 6, '360' => 16, '480' => 32, '720' => 64, '720P60' => 74, '1080' => 80,
|
32
|
+
'1080+' => 112, '1080P60' => 116, '4K' => 120, 'HDR' => 125
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
def save_cookie
|
37
|
+
check_config_path
|
38
|
+
|
39
|
+
@http_client.api_http.cookies = @http_client.login_http.cookies
|
40
|
+
json_str = @http_client.login_http.cookies.to_json
|
41
|
+
write_cookie(json_str)
|
42
|
+
end
|
43
|
+
|
44
|
+
def load_cookie
|
45
|
+
return {} unless File.exist?(cookie_path)
|
46
|
+
|
47
|
+
json_str = File.read(cookie_path)
|
48
|
+
return {} if json_str.nil? || json_str.empty?
|
49
|
+
|
50
|
+
JSON.parse(json_str)
|
51
|
+
end
|
52
|
+
|
53
|
+
def clean_cookie
|
54
|
+
File.open(cookie_path, 'w') do |file|
|
55
|
+
file.write('{}')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def check_config_path
|
62
|
+
FileUtils.mkdir_p(config_path) unless Dir.exist?(config_path)
|
63
|
+
end
|
64
|
+
|
65
|
+
def write_cookie(cookie)
|
66
|
+
File.open(cookie_path, 'w') do |file|
|
67
|
+
file.write(cookie)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def create_request_methods
|
72
|
+
methods = %w[login_http api_http manga_http]
|
73
|
+
methods.each do |method|
|
74
|
+
define_get_json_method(method)
|
75
|
+
define_post_form_json_method(method)
|
76
|
+
define_post_json_method(method)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def define_get_json_method(method_name)
|
81
|
+
new_method_name = "get_json#{method_name[0]}"
|
82
|
+
BilibiliBase.define_method(new_method_name) do |url|
|
83
|
+
@http_client.get_json(@http_client.instance_variable_get("@#{method_name}"), url)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def define_post_form_json_method(method_name)
|
88
|
+
new_method_name = "post_form_json#{method_name[0]}"
|
89
|
+
BilibiliBase.define_method(new_method_name) do |url, params|
|
90
|
+
@http_client.post_form_json(@http_client.instance_variable_get("@#{method_name}"), url, params)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def define_post_json_method(method_name)
|
95
|
+
new_method_name = "post_json#{method_name[0]}"
|
96
|
+
BilibiliBase.define_method(new_method_name) do |url, headers, req_body|
|
97
|
+
@http_client.post_json(@http_client.instance_variable_get("@#{method_name}"), url, headers, req_body)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def config_path
|
102
|
+
File.expand_path(@options['config_path'].to_s, __dir__)
|
103
|
+
end
|
104
|
+
|
105
|
+
def cookie_path
|
106
|
+
File.expand_path(@options['cookie_file'].to_s, __FILE__)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
data/lib/bilibili_console/fav.rb
CHANGED
@@ -1,147 +1,154 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Copyright (c) 2021 leejoker
|
4
|
-
#
|
5
|
-
# This software is released under the MIT License.
|
6
|
-
# https://opensource.org/licenses/MIT
|
7
|
-
|
8
|
-
require_relative 'http/http'
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
@
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
@
|
55
|
-
@
|
56
|
-
@
|
57
|
-
@
|
58
|
-
@
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
@
|
113
|
-
@
|
114
|
-
@
|
115
|
-
@
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright (c) 2021 leejoker
|
4
|
+
#
|
5
|
+
# This software is released under the MIT License.
|
6
|
+
# https://opensource.org/licenses/MIT
|
7
|
+
|
8
|
+
require_relative 'http/http'
|
9
|
+
require_relative 'api'
|
10
|
+
require 'json'
|
11
|
+
require 'cgi'
|
12
|
+
|
13
|
+
# video module
|
14
|
+
module Bilibili
|
15
|
+
include BiliHttp
|
16
|
+
# fav list
|
17
|
+
class FavList
|
18
|
+
attr_accessor :count, :list, :season
|
19
|
+
|
20
|
+
def initialize(data)
|
21
|
+
return if data.nil?
|
22
|
+
|
23
|
+
@count = data[:count]
|
24
|
+
@season = data[:season]
|
25
|
+
@list = generate_fav_list(data[:list])
|
26
|
+
end
|
27
|
+
|
28
|
+
def generate_fav_list(data_list)
|
29
|
+
data = []
|
30
|
+
if !data_list.nil? && !data_list.empty?
|
31
|
+
data_list.each do |obj|
|
32
|
+
data << Bilibili::FavInfo.new(obj)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
data
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_json(*opt)
|
39
|
+
{
|
40
|
+
count: @count,
|
41
|
+
list: @list,
|
42
|
+
season: @season
|
43
|
+
}.to_json(*opt)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# bilibili fav info
|
48
|
+
class FavInfo
|
49
|
+
attr_accessor :id, :fid, :uid, :attr, :title, :fav_state, :media_count
|
50
|
+
|
51
|
+
def initialize(json)
|
52
|
+
return if json.nil?
|
53
|
+
|
54
|
+
@id = json[:id]
|
55
|
+
@fid = json[:fid]
|
56
|
+
@uid = json[:mid]
|
57
|
+
@attr = json[:attr]
|
58
|
+
@title = json[:title]
|
59
|
+
@fav_state = json[:fav_state]
|
60
|
+
@media_count = json[:media_count]
|
61
|
+
end
|
62
|
+
|
63
|
+
def to_json(*opt)
|
64
|
+
{
|
65
|
+
id: @id,
|
66
|
+
fid: @fid,
|
67
|
+
uid: @uid,
|
68
|
+
attr: @attr,
|
69
|
+
title: @title,
|
70
|
+
fav_state: @fav_state,
|
71
|
+
media_count: @media_count
|
72
|
+
}.to_json(*opt)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# fav media list
|
77
|
+
class FavResourceList
|
78
|
+
attr_accessor :info, :medias
|
79
|
+
|
80
|
+
def initialize(json)
|
81
|
+
return if json.nil?
|
82
|
+
|
83
|
+
@info = Bilibili::FavInfo.new(json[:info])
|
84
|
+
@medias = generate_media_list(json[:medias])
|
85
|
+
end
|
86
|
+
|
87
|
+
def generate_media_list(medias)
|
88
|
+
data = []
|
89
|
+
if !medias.nil? && !medias.empty?
|
90
|
+
medias.each do |media|
|
91
|
+
data << Bilibili::FavMediaInfo.new(media)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
data
|
95
|
+
end
|
96
|
+
|
97
|
+
def to_json(*opt)
|
98
|
+
{
|
99
|
+
info: @info,
|
100
|
+
medias: @medias
|
101
|
+
}.to_json(*opt)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# fav media info
|
106
|
+
class FavMediaInfo
|
107
|
+
attr_accessor :id, :type, :title, :intro, :page, :bv_id
|
108
|
+
|
109
|
+
def initialize(json)
|
110
|
+
return if json.nil?
|
111
|
+
|
112
|
+
@id = json[:id]
|
113
|
+
@type = json[:type]
|
114
|
+
@title = json[:title]
|
115
|
+
@intro = json[:intro]
|
116
|
+
@page = json[:page]
|
117
|
+
@bv_id = json[:bv_id]
|
118
|
+
end
|
119
|
+
|
120
|
+
def to_json(*opt)
|
121
|
+
{
|
122
|
+
id: @id,
|
123
|
+
type: @type,
|
124
|
+
title: @title,
|
125
|
+
intro: @intro,
|
126
|
+
page: @page,
|
127
|
+
bv_id: @bv_id
|
128
|
+
}.to_json(*opt)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# bilibili video interfaces
|
133
|
+
class Fav < BilibiliBase
|
134
|
+
# list user fav folders
|
135
|
+
def list_user_fav_video(user_info)
|
136
|
+
url = "#{Api::Fav::USER_FAV_LIST}?up_mid=#{user_info.uid}&type=2"
|
137
|
+
data = get_jsona(url)
|
138
|
+
Bilibili::FavList.new(data)
|
139
|
+
end
|
140
|
+
|
141
|
+
# list user fav folder videos by page
|
142
|
+
def list_fav_video(options)
|
143
|
+
options[:page_num] = 1 if options[:page_num].nil?
|
144
|
+
options[:page_size] = 10 if options[:page_size].nil?
|
145
|
+
options[:all] = 1 if options[:all].nil?
|
146
|
+
unless options[:search].nil?
|
147
|
+
options[:search] = "&keyword=#{CGI.escape(options[:search])}&order=mtime&type=#{options[:all]}&tid=0&jsonp=jsonp"
|
148
|
+
end
|
149
|
+
url = "#{Api::Fav::FAV_VIDEO_LIST}?media_id=#{options[:fav]}&pn=#{options[:page_num]}&ps=#{options[:page_size]}#{options[:search]}&platform=web"
|
150
|
+
data = get_jsona(url)
|
151
|
+
Bilibili::FavResourceList.new(data)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
@@ -1,75 +1,75 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Copyright (c) 2021 leejoker
|
4
|
-
#
|
5
|
-
# This software is released under the MIT License.
|
6
|
-
# https://opensource.org/licenses/MIT
|
7
|
-
|
8
|
-
require 'net/http'
|
9
|
-
require 'nice_http'
|
10
|
-
require_relative 'response_body'
|
11
|
-
|
12
|
-
# bilibili client http module
|
13
|
-
module BiliHttp
|
14
|
-
class << self
|
15
|
-
attr_accessor :headers
|
16
|
-
end
|
17
|
-
|
18
|
-
# bilibili http client
|
19
|
-
class HttpClient
|
20
|
-
attr_accessor :login_http, :api_http, :manga_http
|
21
|
-
|
22
|
-
def initialize
|
23
|
-
BiliHttp.headers = {
|
24
|
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.64',
|
25
|
-
'Referer': 'https://www.bilibili.com'
|
26
|
-
}
|
27
|
-
end
|
28
|
-
|
29
|
-
# get method
|
30
|
-
def get_json(http, url)
|
31
|
-
json_data(get(http, url))
|
32
|
-
end
|
33
|
-
|
34
|
-
def get(http, url)
|
35
|
-
request = {
|
36
|
-
headers: BiliHttp.headers,
|
37
|
-
path: URI(url).request_uri
|
38
|
-
}
|
39
|
-
http.get(request).data
|
40
|
-
end
|
41
|
-
|
42
|
-
# post method with form data
|
43
|
-
def post_form_json(http, url, params)
|
44
|
-
custom_headers = BiliHttp.headers.clone
|
45
|
-
custom_headers['Content-Type'] = 'application/x-www-form-urlencoded'
|
46
|
-
request = {
|
47
|
-
headers: custom_headers,
|
48
|
-
path: URI(url).request_uri,
|
49
|
-
data: params
|
50
|
-
}
|
51
|
-
json_data(http.post(request).data)
|
52
|
-
end
|
53
|
-
|
54
|
-
# post method with json body
|
55
|
-
def post_json(http, url, headers, req_body)
|
56
|
-
headers = BiliHttp.headers.clone if headers.nil? || headers.empty?
|
57
|
-
headers['Content-Type'] = 'application/json' unless req_body.nil? || req_body.empty?
|
58
|
-
request = {
|
59
|
-
headers: headers,
|
60
|
-
path: URI(url).request_uri
|
61
|
-
}
|
62
|
-
request.merge!({ data: req_body }) unless req_body.nil? || req_body.empty?
|
63
|
-
json_data(http.post(request).data)
|
64
|
-
end
|
65
|
-
|
66
|
-
def json_data(data)
|
67
|
-
body = BiliHttp::ResponseBody.new(data.json)
|
68
|
-
if body.data.nil?
|
69
|
-
body
|
70
|
-
else
|
71
|
-
body.data
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright (c) 2021 leejoker
|
4
|
+
#
|
5
|
+
# This software is released under the MIT License.
|
6
|
+
# https://opensource.org/licenses/MIT
|
7
|
+
|
8
|
+
require 'net/http'
|
9
|
+
require 'nice_http'
|
10
|
+
require_relative 'response_body'
|
11
|
+
|
12
|
+
# bilibili client http module
|
13
|
+
module BiliHttp
|
14
|
+
class << self
|
15
|
+
attr_accessor :headers
|
16
|
+
end
|
17
|
+
|
18
|
+
# bilibili http client
|
19
|
+
class HttpClient
|
20
|
+
attr_accessor :login_http, :api_http, :manga_http
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
BiliHttp.headers = {
|
24
|
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.64',
|
25
|
+
'Referer': 'https://www.bilibili.com'
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
# get method
|
30
|
+
def get_json(http, url)
|
31
|
+
json_data(get(http, url))
|
32
|
+
end
|
33
|
+
|
34
|
+
def get(http, url)
|
35
|
+
request = {
|
36
|
+
headers: BiliHttp.headers,
|
37
|
+
path: URI(url).request_uri
|
38
|
+
}
|
39
|
+
http.get(request).data
|
40
|
+
end
|
41
|
+
|
42
|
+
# post method with form data
|
43
|
+
def post_form_json(http, url, params)
|
44
|
+
custom_headers = BiliHttp.headers.clone
|
45
|
+
custom_headers['Content-Type'] = 'application/x-www-form-urlencoded'
|
46
|
+
request = {
|
47
|
+
headers: custom_headers,
|
48
|
+
path: URI(url).request_uri,
|
49
|
+
data: params
|
50
|
+
}
|
51
|
+
json_data(http.post(request).data)
|
52
|
+
end
|
53
|
+
|
54
|
+
# post method with json body
|
55
|
+
def post_json(http, url, headers, req_body)
|
56
|
+
headers = BiliHttp.headers.clone if headers.nil? || headers.empty?
|
57
|
+
headers['Content-Type'] = 'application/json' unless req_body.nil? || req_body.empty?
|
58
|
+
request = {
|
59
|
+
headers: headers,
|
60
|
+
path: URI(url).request_uri
|
61
|
+
}
|
62
|
+
request.merge!({ data: req_body }) unless req_body.nil? || req_body.empty?
|
63
|
+
json_data(http.post(request).data)
|
64
|
+
end
|
65
|
+
|
66
|
+
def json_data(data)
|
67
|
+
body = BiliHttp::ResponseBody.new(data.json)
|
68
|
+
if body.data.nil?
|
69
|
+
body
|
70
|
+
else
|
71
|
+
body.data
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|