|
6 | 6 | //
|
7 | 7 |
|
8 | 8 | import Alamofire
|
9 |
| -import CommonCrypto |
10 | 9 | import Foundation
|
11 | 10 | import SwiftProtobuf
|
12 | 11 | import SwiftyJSON
|
@@ -113,36 +112,6 @@ enum WebRequest {
|
113 | 112 | }
|
114 | 113 | }
|
115 | 114 |
|
116 |
| - private static func addWbiSign(method: HTTPMethod = .get, |
117 |
| - url: URLConvertible, |
118 |
| - parameters: Parameters = [:], |
119 |
| - onComplete: @escaping (String?) -> Void) |
120 |
| - { |
121 |
| - do { |
122 |
| - let urlObj = try url.asURL() |
123 |
| - if urlObj.absoluteString.contains("/wbi/") == true, method == .get { |
124 |
| - var request = URLRequest(url: urlObj) |
125 |
| - request.method = .get |
126 |
| - request = try URLEncoding.queryString.encode(request, with: parameters) |
127 |
| - if let query = request.url?.query(percentEncoded: true) { |
128 |
| - biliWbiSign(param: query) { res in |
129 |
| - if let res { |
130 |
| - let urlString = urlObj.absoluteString + "?" + res |
131 |
| - onComplete(urlString) |
132 |
| - return |
133 |
| - } else { |
134 |
| - onComplete(nil) |
135 |
| - } |
136 |
| - } |
137 |
| - return |
138 |
| - } |
139 |
| - } |
140 |
| - onComplete(nil) |
141 |
| - } catch { |
142 |
| - onComplete(nil) |
143 |
| - } |
144 |
| - } |
145 |
| - |
146 | 115 | static func requestJSON(method: HTTPMethod = .get,
|
147 | 116 | url: URLConvertible,
|
148 | 117 | parameters: Parameters = [:],
|
@@ -550,104 +519,6 @@ extension WebRequest {
|
550 | 519 | }
|
551 | 520 | }
|
552 | 521 |
|
553 |
| -// MARK: - Wbi |
554 |
| - |
555 |
| -extension WebRequest { |
556 |
| - // https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/misc/sign/wbi.md#Swift |
557 |
| - static func biliWbiSign(param: String, completion: @escaping (String?) -> Void) { |
558 |
| - func getMixinKey(orig: String) -> String { |
559 |
| - return String(mixinKeyEncTab.map { orig[orig.index(orig.startIndex, offsetBy: $0)] }.prefix(32)) |
560 |
| - } |
561 |
| - |
562 |
| - func encWbi(params: [String: Any], imgKey: String, subKey: String) -> [String: Any] { |
563 |
| - var params = params |
564 |
| - let mixinKey = getMixinKey(orig: imgKey + subKey) |
565 |
| - let currTime = round(Date().timeIntervalSince1970) |
566 |
| - params["wts"] = currTime |
567 |
| - params = params.sorted { $0.key < $1.key }.reduce(into: [:]) { $0[$1.key] = $1.value } |
568 |
| - params = params.mapValues { String(describing: $0).filter { !"!'()*".contains($0) } } |
569 |
| - let query = params.map { "\($0.key)=\($0.value)" }.joined(separator: "&") |
570 |
| - let wbiSign = calculateMD5(string: query + mixinKey) |
571 |
| - params["w_rid"] = wbiSign |
572 |
| - return params |
573 |
| - } |
574 |
| - |
575 |
| - func getWbiKeys(completion: @escaping (Result<(imgKey: String, subKey: String), Error>) -> Void) { |
576 |
| - class Cache { |
577 |
| - var imgKey: String? |
578 |
| - var subKey: String? |
579 |
| - var lastUpdate: Date? |
580 |
| - |
581 |
| - static let shared = Cache() |
582 |
| - } |
583 |
| - |
584 |
| - if let imgKey = Cache.shared.imgKey, let subKey = Cache.shared.subKey, let lastUpdate = Cache.shared.lastUpdate, Date().timeIntervalSince(lastUpdate) < 60 * 60 * 12 { |
585 |
| - completion(.success((imgKey, subKey))) |
586 |
| - return |
587 |
| - } |
588 |
| - |
589 |
| - let headers: HTTPHeaders = [ |
590 |
| - "User-Agent": Keys.userAgent, |
591 |
| - "Referer": Keys.referer, |
592 |
| - ] |
593 |
| - |
594 |
| - AF.request("https://api.bilibili.com/x/web-interface/nav", headers: headers).responseData { response in |
595 |
| - switch response.result { |
596 |
| - case let .success(value): |
597 |
| - let json = JSON(value) |
598 |
| - let imgURL = json["data"]["wbi_img"]["img_url"].string ?? "" |
599 |
| - let subURL = json["data"]["wbi_img"]["sub_url"].string ?? "" |
600 |
| - let imgKey = imgURL.components(separatedBy: "/").last?.components(separatedBy: ".").first ?? "" |
601 |
| - let subKey = subURL.components(separatedBy: "/").last?.components(separatedBy: ".").first ?? "" |
602 |
| - Cache.shared.imgKey = imgKey |
603 |
| - Cache.shared.subKey = subKey |
604 |
| - Cache.shared.lastUpdate = Date() |
605 |
| - completion(.success((imgKey, subKey))) |
606 |
| - case let .failure(error): |
607 |
| - completion(.failure(error)) |
608 |
| - } |
609 |
| - } |
610 |
| - } |
611 |
| - |
612 |
| - func calculateMD5(string: String) -> String { |
613 |
| - let data = Data(string.utf8) |
614 |
| - var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH)) |
615 |
| - _ = data.withUnsafeBytes { |
616 |
| - CC_MD5($0.baseAddress, CC_LONG(data.count), &digest) |
617 |
| - } |
618 |
| - return digest.map { String(format: "%02hhx", $0) }.joined() |
619 |
| - } |
620 |
| - |
621 |
| - let mixinKeyEncTab = [ |
622 |
| - 46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49, |
623 |
| - 33, 9, 42, 19, 29, 28, 14, 39, 12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40, |
624 |
| - 61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11, |
625 |
| - 36, 20, 34, 44, 52, |
626 |
| - ] |
627 |
| - |
628 |
| - getWbiKeys { result in |
629 |
| - switch result { |
630 |
| - case let .success(keys): |
631 |
| - let spdParam = param.components(separatedBy: "&") |
632 |
| - var spdDicParam = [String: String]() |
633 |
| - spdParam.forEach { pair in |
634 |
| - let components = pair.components(separatedBy: "=") |
635 |
| - if components.count == 2 { |
636 |
| - spdDicParam[components[0]] = components[1] |
637 |
| - } |
638 |
| - } |
639 |
| - |
640 |
| - let signedParams = encWbi(params: spdDicParam, imgKey: keys.imgKey, subKey: keys.subKey) |
641 |
| - let query = signedParams.map { "\($0.key)=\($0.value)" }.joined(separator: "&") |
642 |
| - completion(query) |
643 |
| - case let .failure(error): |
644 |
| - print("Error getting keys: \(error)") |
645 |
| - completion(nil) |
646 |
| - } |
647 |
| - } |
648 |
| - } |
649 |
| -} |
650 |
| - |
651 | 522 | struct HistoryData: DisplayData, Codable {
|
652 | 523 | struct HistoryPage: Codable, Hashable {
|
653 | 524 | let cid: Int
|
|
0 commit comments