XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/bilibili-API-collect

优化wbi.md里的Swift代码 (#1208)

修复Swift代码中时间戳类型转化后多出.0的问题

4abf38f
Seamain <quanminjun37@gmail.com>
提交于

代码差异

1 个文件 +44 -24
Modified docs/misc/sign/wbi.md +44 -24
@@ -1108,9 +1108,9 @@ mod tests {
1108 1108 需要 [Alamofire](https://github.com/Alamofire/Alamofire) 和 [SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON) 库
1109 1109
1110 1110 ```swift
1111 import Foundation
1112 import CommonCrypto
1113 1111 import Alamofire
1112 import CommonCrypto
1113 import Foundation
1114 1114 import SwiftyJSON
1115 1115
1116 1116 func biliWbiSign(param: String, completion: @escaping (String?) -> Void) {
@@ -1124,35 +1124,39 @@ func biliWbiSign(param: String, completion: @escaping (String?) -> Void) {
1124 1124 let currTime = round(Date().timeIntervalSince1970)
1125 1125 params["wts"] = currTime
1126 1126 params = params.sorted { $0.key < $1.key }.reduce(into: [:]) { $0[$1.key] = $1.value }
1127 params = params.mapValues { String(describing: $0).filter { !"!'()*".contains($0) } }
1127 params = params.mapValues { value in
1128 if let doubleValue = value as? Double, doubleValue.truncatingRemainder(dividingBy: 1) == 0 {
1129 return String(Int(doubleValue)).filter { !"!'()*".contains($0) }
1130 }
1131 return String(describing: value).filter { !"!'()*".contains($0) }
1132 }
1128 1133 let query = params.map { "\($0.key)=\($0.value)" }.joined(separator: "&")
1129 1134 let wbiSign = calculateMD5(string: query + mixinKey)
1130 1135 params["w_rid"] = wbiSign
1131 1136 return params
1132 1137 }
1133 1138
1134 func getWbiKeys(completion: @escaping (Result<(imgKey: String, subKey: String), Error>) -> Void) {
1135 let headers: HTTPHeaders = [
1136 "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",
1137 "Referer": "https://www.bilibili.com/"
1138 ]
1139 func getWbiKeys(completion: @escaping (Result<(imgKey: String, subKey: String), Error>) -> Void) {
1140 let headers: HTTPHeaders = [
1141 "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",
1142 "Referer": "https://www.bilibili.com/"
1143 ]
1139 1144
1140 AF.request("https://api.bilibili.com/x/web-interface/nav", headers: headers).responseJSON { response in
1141 switch response.result {
1142 case .success(let value):
1143 let json = JSON(value)
1144 let imgURL = json["data"]["wbi_img"]["img_url"].string ?? ""
1145 let subURL = json["data"]["wbi_img"]["sub_url"].string ?? ""
1146 let imgKey = imgURL.components(separatedBy: "/").last?.components(separatedBy: ".").first ?? ""
1147 let subKey = subURL.components(separatedBy: "/").last?.components(separatedBy: ".").first ?? ""
1148 completion(.success((imgKey, subKey)))
1149 case .failure(let error):
1150 completion(.failure(error))
1151 }
1152 }
1153 }
1145 AF.request("https://api.bilibili.com/x/web-interface/nav", headers: headers).responseJSON { response in
1146 switch response.result {
1147 case .success(let value):
1148 let json = JSON(value)
1149 let imgURL = json["data"]["wbi_img"]["img_url"].string ?? ""
1150 let subURL = json["data"]["wbi_img"]["sub_url"].string ?? ""
1151 let imgKey = imgURL.components(separatedBy: "/").last?.components(separatedBy: ".").first ?? ""
1152 let subKey = subURL.components(separatedBy: "/").last?.components(separatedBy: ".").first ?? ""
1153 completion(.success((imgKey, subKey)))
1154 case .failure(let error):
1155 completion(.failure(error))
1156 }
1157 }
1158 }
1154 1159
1155
1156 1160 func calculateMD5(string: String) -> String {
1157 1161 let data = Data(string.utf8)
1158 1162 var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
@@ -1174,7 +1178,7 @@ func biliWbiSign(param: String, completion: @escaping (String?) -> Void) {
1174 1178 case .success(let keys):
1175 1179 let spdParam = param.components(separatedBy: "&")
1176 1180 var spdDicParam = [String: String]()
1177 spdParam.forEach { pair in
1181 for pair in spdParam {
1178 1182 let components = pair.components(separatedBy: "=")
1179 1183 if components.count == 2 {
1180 1184 spdDicParam[components[0]] = components[1]
@@ -1191,6 +1195,22 @@ func biliWbiSign(param: String, completion: @escaping (String?) -> Void) {
1191 1195 }
1192 1196 }
1193 1197
1198 // 使用示例
1199 biliWbiSign(param: "bar=514&foo=114&zab=1919810") {
1200 signedQuery in
1201 if let signedQuery = signedQuery {
1202 print("签名后的参数: \(signedQuery)")
1203 } else {
1204 print("签名失败")
1205 }
1206 }
1207
1208 RunLoop.main.run()//程序类型为命令行程序时需要添加这行代码
1209
1210 ```
1211
1212 ```text
1213 签名后的参数: bar=514&wts=1741082093&foo=114&zab=1919810&w_rid=04775bb3debbb45bab86a93a1c08d12a
1194 1214 ```
1195 1215
1196 1216