/
BKLiveRoom+Info.swift
146 lines (140 loc) · 4.37 KB
/
BKLiveRoom+Info.swift
1
2
3
4
5
6
7
8
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
//
// BKLiveRoom+Info.swift
// BilibiliKit
//
// Created by Apollo Zhu on 5/11/18.
// Copyright (c) 2017-2020 ApolloZhu. MIT License.
//
import Foundation
extension BKLiveRoom {
/// Information of a live room.
public struct Info: Codable {
/// Author id.
public let mid: Int
public let roomID: Int
public let shortID: Int
/// Number of followers.
public let followers: Int
/// Number of current viewers.
public let watching: Int
public let isPortrait: Bool
/// Description in HTML
public let description: String
/*
let live_status: Int
let area_id: Int
let parent_area_id: Int
// e.g. 娱乐.
let parent_area_name: String
let old_area_id: Int
*/
public let background: String
public let title: String
/// Might be empty string.
public let userChosenCoverImageURL: String
/// Might be empty string
public let keyframeSnapshotURL: String
// let is_strict_room: Bool
/// When started. e.g. 2018-05-12 00:00:00
// let live_time: String
/// Tags. e.g. ACG音乐.
public let tags: String
/*
let is_anchor: Int
let room_silent_type: String
/// Minumum level required to comment.
let room_silent_level: Int
/// Seconds before comment is enabled.
let room_silent_second: Int
/// Classification. e.g. 音乐台.
public let area_name: String
let pendants: String
let area_pendants: String
let hot_words: [String]
let hot_words_status: Int
*/
/// Verified official name.
public let verifiedIdentity: String
/*
"new_pendants": {
"frame": {
"name": "",
"value": "",
"position": 0,
"desc": "",
"area": 0,
"area_old": 0,
"bg_color": "",
"bg_pic": "",
"use_old_area": false
},
"badge": null,
"mobile_frame": {
"name": "",
"value": "",
"position": 0,
"desc": "",
"area": 0,
"area_old": 0,
"bg_color": "",
"bg_pic": "",
"use_old_area": false
},
"mobile_badge": null
},
"up_session": "",
"pk_status": 0,
"pk_id": 0,
"battle_id": 0,
"allow_change_area_time": 0,
"allow_upload_cover_time": 0,
"studio_info": {
"status": 0,
"master_list": []
}
*/
enum CodingKeys: String, CodingKey {
case mid = "uid"
case roomID = "room_id"
case shortID = "short_id"
case followers = "attention"
case watching = "online"
case isPortrait = "is_portrait"
case description, background, title
case userChosenCoverImageURL = "user_cover"
case keyframeSnapshotURL = "keyframe"
case tags
case verifiedIdentity = "verify"
}
}
}
extension BKLiveRoom.Info {
/// The actual cover image in use.
public var coverImageURL: URL {
return URL(string: userChosenCoverImageURL)
?? URL(string: keyframeSnapshotURL)
?? .notFound
}
}
// MARK: - Networking
extension BKLiveRoom {
/// Fetchs and passes this live room's info to `handler`.
///
/// - Parameters:
/// - handler: code to process an optional `Info`.
public func getInfo(then handler: @escaping BKHandler<Info>) {
let url = "https://api.live.bilibili.com/room/v1/Room/get_info?room_id=\(id)"
URLSession.get(url, unwrap: BKWrapperMessage<_Either<Info, [String]>>.self ) {
handler($0.flatMap { either in
switch either {
case .left(let info):
return .success(info)
case .right(let emptyArray):
return emptyArray.isEmpty
? .failure(.responseError(reason: .emptyValue))
: .failure(.parseError(reason: .dataEncodeFailure))
}
})
}
}
}