bilibili_console 0.0.1 → 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.
@@ -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
@@ -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
- require 'json'
10
-
11
- # video module
12
- module Bilibili
13
- include BiliHttp
14
- # fav list
15
- class FavList
16
- attr_accessor :count, :list, :season
17
-
18
- def initialize(data)
19
- return if data.nil?
20
-
21
- @count = data[:count]
22
- @season = data[:season]
23
- @list = generate_fav_list(data[:list])
24
- end
25
-
26
- def generate_fav_list(data_list)
27
- data = []
28
- if !data_list.nil? && !data_list.empty?
29
- data_list.each do |obj|
30
- data << Bilibili::FavInfo.new(obj)
31
- end
32
- end
33
- data
34
- end
35
-
36
- def to_json(*opt)
37
- {
38
- count: @count,
39
- list: @list,
40
- season: @season
41
- }.to_json(*opt)
42
- end
43
- end
44
-
45
- # bilibili fav info
46
- class FavInfo
47
- attr_accessor :id, :fid, :uid, :attr, :title, :fav_state, :media_count
48
-
49
- def initialize(json)
50
- return if json.nil?
51
-
52
- @id = json[:id]
53
- @fid = json[:fid]
54
- @uid = json[:mid]
55
- @attr = json[:attr]
56
- @title = json[:title]
57
- @fav_state = json[:fav_state]
58
- @media_count = json[:media_count]
59
- end
60
-
61
- def to_json(*opt)
62
- {
63
- id: @id,
64
- fid: @fid,
65
- uid: @uid,
66
- attr: @attr,
67
- title: @title,
68
- fav_state: @fav_state,
69
- media_count: @media_count
70
- }.to_json(*opt)
71
- end
72
- end
73
-
74
- # fav media list
75
- class FavResourceList
76
- attr_accessor :info, :medias
77
-
78
- def initialize(json)
79
- return if json.nil?
80
-
81
- @info = Bilibili::FavInfo.new(json[:info])
82
- @medias = generate_media_list(json[:medias])
83
- end
84
-
85
- def generate_media_list(medias)
86
- data = []
87
- if !medias.nil? && !medias.empty?
88
- medias.each do |media|
89
- data << Bilibili::FavMediaInfo.new(media)
90
- end
91
- end
92
- data
93
- end
94
-
95
- def to_json(*opt)
96
- {
97
- info: @info,
98
- medias: @medias
99
- }.to_json(*opt)
100
- end
101
- end
102
-
103
- # fav media info
104
- class FavMediaInfo
105
- attr_accessor :id, :type, :title, :intro, :page, :bv_id
106
-
107
- def initialize(json)
108
- return if json.nil?
109
-
110
- @id = json[:id]
111
- @type = json[:type]
112
- @title = json[:title]
113
- @intro = json[:intro]
114
- @page = json[:page]
115
- @bv_id = json[:bv_id]
116
- end
117
-
118
- def to_json(*opt)
119
- {
120
- id: @id,
121
- type: @type,
122
- title: @title,
123
- intro: @intro,
124
- page: @page,
125
- bv_id: @bv_id
126
- }.to_json(*opt)
127
- end
128
- end
129
-
130
- # bilibili video interfaces
131
- class Fav < BilibiliBase
132
- # list user fav folders
133
- def list_user_fav_video(user_info)
134
- url = "https://api.bilibili.com/x/v3/fav/folder/created/list-all?up_mid=#{user_info.uid}&type=2"
135
- data = get_jsona(url)
136
- Bilibili::FavList.new(data)
137
- end
138
-
139
- # list user fav folder videos by page
140
- def list_fav_video(media_id, page_num = 1, page_size = 10, keyword = nil)
141
- keyword = "&keyword=#{keyword}" unless keyword.nil?
142
- url = "https://api.bilibili.com/x/v3/fav/resource/list?media_id=#{media_id}&pn=#{page_num}&ps=#{page_size}#{keyword}&platform=web"
143
- data = get_jsona(url)
144
- Bilibili::FavResourceList.new(data)
145
- end
146
- end
147
- 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_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