Skip to content

Commit

Permalink
Merge pull request #424 from Darock-Studio/feat/split-backend
Browse files Browse the repository at this point in the history
feat: Splited some backends
  • Loading branch information
Mark Chan authored Oct 20, 2024
2 parents 4b03992 + 0a45998 commit ade9e18
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 145 deletions.
11 changes: 11 additions & 0 deletions DarockBili.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@
8CC5B7432B8749BC00BAD89E /* MeowBiliUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CC5B7422B8749BC00BAD89E /* MeowBiliUITests.swift */; };
8CD3D0402C8C715300E9C37C /* MarqueeText in Frameworks */ = {isa = PBXBuildFile; productRef = 8CD3D03F2C8C715300E9C37C /* MarqueeText */; };
8CD3D0422C8C715B00E9C37C /* MarqueeText in Frameworks */ = {isa = PBXBuildFile; productRef = 8CD3D0412C8C715B00E9C37C /* MarqueeText */; };
8CF148952CC40A4C00C13711 /* BiliAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CF148922CC403BF00C13711 /* BiliAPI.swift */; };
8CF148962CC40A4C00C13711 /* BiliAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CF148922CC403BF00C13711 /* BiliAPI.swift */; };
8CF148972CC40AAB00C13711 /* ResponseModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CF148942CC4040E00C13711 /* ResponseModels.swift */; };
8CF148982CC40AAB00C13711 /* ResponseModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CF148942CC4040E00C13711 /* ResponseModels.swift */; };
B2B8137F2CC3D0ED00C69D17 /* WidgetKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B2B8137E2CC3D0ED00C69D17 /* WidgetKit.framework */; };
B2B813812CC3D0ED00C69D17 /* SwiftUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B2B813802CC3D0ED00C69D17 /* SwiftUI.framework */; };
B2B813842CC3D0ED00C69D17 /* MeowWidgetBundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2B813832CC3D0ED00C69D17 /* MeowWidgetBundle.swift */; };
Expand Down Expand Up @@ -1255,6 +1259,7 @@
8CA7CCB62B77B3BF008E587F /* UserDetailView.swift in Sources */,
8CA7CCCE2B77B4DF008E587F /* DynamicSendView.swift in Sources */,
8CA7CCCC2B77B4CA008E587F /* DynamicDetailView.swift in Sources */,
8CF148952CC40A4C00C13711 /* BiliAPI.swift in Sources */,
8CA7CCC52B77B47A008E587F /* AudioPlayerView.swift in Sources */,
8CA7CCC82B77B49D008E587F /* ImageViewerView.swift in Sources */,
8CA7CC972B77B115008E587F /* SkinExplorerView.swift in Sources */,
Expand All @@ -1263,6 +1268,7 @@
8CC3D5652B7CCF3C005636DC /* SelfQrCardView.swift in Sources */,
8CA7CCB82B77B3D6008E587F /* HistoryView.swift in Sources */,
8CA7CCDC2B77B571008E587F /* LiveMessagesView.swift in Sources */,
8CF148972CC40AAB00C13711 /* ResponseModels.swift in Sources */,
8CC3D55A2B7CB95D005636DC /* NowPlayingExtension.m in Sources */,
8CA7CC6A2B77AE62008E587F /* Bangumi.swift in Sources */,
8CA7CBC52B77AC4A008E587F /* ContentView.swift in Sources */,
Expand Down Expand Up @@ -1322,6 +1328,7 @@
8CBFF0C52B85E55F00350E0F /* SkinExplorerView.swift in Sources */,
8CBFF0BC2B85E55F00350E0F /* HistoryView.swift in Sources */,
8CBFF0B72B85E55F00350E0F /* ContentView.swift in Sources */,
8CF148962CC40A4C00C13711 /* BiliAPI.swift in Sources */,
8CBFF0DB2B85E55F00350E0F /* SignalErrorView.swift in Sources */,
8CBFF0D82B85E55F00350E0F /* VideoDetailView.swift in Sources */,
8CBFF0D72B85E55F00350E0F /* MainView.swift in Sources */,
Expand All @@ -1330,6 +1337,7 @@
8CBFF0E82B85E55F00350E0F /* LivePlayerView.swift in Sources */,
8CBFF0BD2B85E55F00350E0F /* DynamicSendView.swift in Sources */,
8CBFF0DD2B85E55F00350E0F /* DownloadObj.swift in Sources */,
8CF148982CC40AAB00C13711 /* ResponseModels.swift in Sources */,
8CBFF0D02B85E55F00350E0F /* AVExtension.m in Sources */,
8CBFF0C12B85E55F00350E0F /* CodeExt.swift in Sources */,
8CBFF0E62B85E55F00350E0F /* PersonAccountView.swift in Sources */,
Expand Down Expand Up @@ -1684,6 +1692,7 @@
FRAMEWORK_SEARCH_PATHS = "";
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = "MeowBili-Watch-App-Info.plist";
INFOPLIST_KEY_CFBundleDisplayName = "喵哩喵哩";
INFOPLIST_KEY_UISupportedInterfaceOrientations = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
INFOPLIST_KEY_WKCompanionAppBundleIdentifier = com.darock.alt785.DarockBili;
INFOPLIST_KEY_WKRunsIndependentlyOfCompanionApp = YES;
Expand Down Expand Up @@ -1902,6 +1911,7 @@
FRAMEWORK_SEARCH_PATHS = "";
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = "MeowBili-Watch-App-Info.plist";
INFOPLIST_KEY_CFBundleDisplayName = "喵哩喵哩";
INFOPLIST_KEY_UISupportedInterfaceOrientations = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
INFOPLIST_KEY_WKCompanionAppBundleIdentifier = com.darock.DarockBili;
INFOPLIST_KEY_WKRunsIndependentlyOfCompanionApp = YES;
Expand Down Expand Up @@ -1939,6 +1949,7 @@
FRAMEWORK_SEARCH_PATHS = "";
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = "MeowBili-Watch-App-Info.plist";
INFOPLIST_KEY_CFBundleDisplayName = "喵哩喵哩";
INFOPLIST_KEY_UISupportedInterfaceOrientations = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
INFOPLIST_KEY_WKCompanionAppBundleIdentifier = com.darock.DarockBili;
INFOPLIST_KEY_WKRunsIndependentlyOfCompanionApp = YES;
Expand Down
84 changes: 84 additions & 0 deletions MeowBili/Backend/BiliAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,93 @@
//
//===----------------------------------------------------------------------===//

import SwiftUI
import DarockKit
import Alamofire
import SwiftyJSON

class BiliAPI {
static let shared = BiliAPI()

@AppStorage("DedeUserID") private var dedeUserID = ""
@AppStorage("DedeUserID__ckMd5") private var dedeUserID__ckMd5 = ""
@AppStorage("SESSDATA") private var sessdata = ""
@AppStorage("bili_jct") private var biliJct = ""
@AppStorage("CachedBiliTicket") private var cachedBiliTicket = ""

/// 获取指定用户详情
/// - Parameter uid: 用户 UID
/// - Returns: 用户详情数据
func userInfo(of uid: String? = nil) async -> UserInfo? {
let uid = uid ?? dedeUserID
let headers: HTTPHeaders = [
"cookie": "SESSDATA=\(sessdata)",
"referer": "https://message.bilibili.com/", // rdar://gh/SocialSisterYi/bilibili-API-collect/issues/631#issuecomment-2099276628
"User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
]
return await withCheckedContinuation { continuation in
biliWbiSign(paramEncoded: "mid=\(uid)".base64Encoded()) { signed in
if let signed {
DarockKit.Network.shared.requestJSON("https://api.bilibili.com/x/space/wbi/acc/info?\(signed)", headers: headers) { respJson, isSuccess in
if isSuccess {
if !CheckBApiError(from: respJson) {
continuation.resume(returning: nil)
return
}
continuation.resume(returning: UserInfo(json: respJson))
} else {
continuation.resume(returning: nil)
}
}
} else {
continuation.resume(returning: nil)
}
}
}
}

func userRelation(of uid: String) async -> UserRelation? {
let headers: HTTPHeaders = [
"cookie": "SESSDATA=\(sessdata); buvid3=\(globalBuvid3); bili_ticket=\(cachedBiliTicket)",
"referer": "https://message.bilibili.com/", // rdar://gh/SocialSisterYi/bilibili-API-collect/issues/631#issuecomment-2099276628
"User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
]
if case .success(let json) = await DarockKit.Network.shared.requestJSON("https://api.bilibili.com/x/relation/stat?vmid=\(uid)", headers: headers) {
if !CheckBApiError(from: json) { return nil }
return UserRelation(json: json["data"])
} else {
return nil
}
}

func isFollowed(user uid: String) async -> Bool {
let headers: HTTPHeaders = [
"cookie": "SESSDATA=\(sessdata); buvid3=\(globalBuvid3); bili_ticket=\(cachedBiliTicket)",
"referer": "https://message.bilibili.com/", // rdar://gh/SocialSisterYi/bilibili-API-collect/issues/631#issuecomment-2099276628
"User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
]
if case let .success(json) = await DarockKit.Network.shared.requestJSON("https://api.bilibili.com/x/relation?fid=\(uid)", headers: headers) {
if !CheckBApiError(from: json) { return false }
if json["data"]["attribute"].int ?? 0 == 2 || json["data"]["attribute"].int ?? 0 == 6 {
return true
}
}
return false
}

func currentUserExperience() async -> (current: Int, next: Int, min: Int)? {
let headers: HTTPHeaders = [
"cookie": "SESSDATA=\(sessdata); buvid3=\(globalBuvid3); bili_ticket=\(cachedBiliTicket)",
"referer": "https://message.bilibili.com/", // rdar://gh/SocialSisterYi/bilibili-API-collect/issues/631#issuecomment-2099276628
"User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
]
if case let .success(json) = await DarockKit.Network.shared.requestJSON("https://api.bilibili.com/x/web-interface/nav", headers: headers) {
if !CheckBApiError(from: json) { return nil }
let currentExp = json["data"]["level_info"]["current_exp"].int ?? 0
let nextExp = json["data"]["level_info"]["next_exp"].int ?? 0
let minExp = json["data"]["level_info"]["current_min"].int ?? 0
return (currentExp, nextExp, minExp)
}
return nil
}
}
40 changes: 25 additions & 15 deletions MeowBili/Backend/ResponseModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ struct UserInfo: Identifiable {
let id: Int64
let sex: String
let level: Int
let coins: Double?
let coins: Double
let face: String
let vip: VIPDetail?
let birthday: String
let official: OfficialDetail?
let official: OfficialDetail
let name: String
let sign: String

init(json: JSON) {
self.id = json["mid"].int64Value
self.sex = json["sex"].string/
self.level = json["level"].int ?? 0
self.coins = json["coins"].double ?? 0
self.face = json["face"].string/
if json["vip"].dictionary != nil {
self.vip = .init(json: json["vip"])
self.id = json["data"]["mid"].int64Value
self.sex = json["data"]["sex"].string/
self.level = json["data"]["level"].int ?? 0
self.coins = json["data"]["coins"].double ?? -1
self.face = json["data"]["face"].string/
if json["data"]["vip"].dictionary != nil {
self.vip = .init(json: json["data"]["vip"])
} else {
self.vip = nil
}
self.birthday = json["birthday"].string ?? "01-01"
if json["official"].dictionary != nil {
self.official = .init(json: json["official"])
}
self.name = json["name"].string/
self.sign = json["sign"].string/
self.birthday = json["data"]["birthday"].string ?? "01-01"
self.official = .init(json: json["data"]["official"])
self.name = json["data"]["name"].string/
self.sign = json["data"]["sign"].string/
}

struct VIPDetail {
Expand Down Expand Up @@ -77,3 +77,13 @@ struct UserInfo: Identifiable {
}
}
}

struct UserRelation {
let following: Int
let follower: Int

init(json: JSON) {
self.following = json["following"].int ?? -1
self.follower = json["follower"].int ?? -1
}
}
20 changes: 3 additions & 17 deletions MeowBili/InMain/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,9 @@ struct ContentView: View {
.onAppear {
updateBiliTicket(csrf: biliJct)
if dedeUserID != "" {
getBuvid(url: "https://api.bilibili.com/x/space/wbi/acc/info".urlEncoded()) { buvid3, buvid4, _uuid, _ in
let headers: HTTPHeaders = [
"cookie": "SESSDATA=\(sessdata); innersign=0; buvid3=\(buvid3); b_nut=1704873471; i-wanna-go-back=-1; b_ut=7; b_lsid=9910433CB_18CF260AB89; _uuid=\(_uuid); enable_web_push=DISABLE; header_theme_version=undefined; home_feed_column=4; browser_resolution=3440-1440; buvid4=\(buvid4);",
"referer": "https://message.bilibili.com/", // rdar://gh/SocialSisterYi/bilibili-API-collect/issues/631#issuecomment-2099276628
"User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
]
biliWbiSign(paramEncoded: "mid=\(dedeUserID)".base64Encoded()) { signed in
if let signed {
debugPrint(signed)
autoRetryRequestApi("https://api.bilibili.com/x/space/wbi/acc/info?\(signed)", headers: headers) { respJson, isSuccess in
if isSuccess {
debugPrint(respJson)
if !CheckBApiError(from: respJson) { return }
userFaceUrl = respJson["data"]["face"].string ?? "E"
}
}
}
Task {
if let info = await BiliAPI.shared.userInfo() {
userFaceUrl = info.face
}
}
}
Expand Down
30 changes: 9 additions & 21 deletions MeowBili/InMain/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,15 @@ struct MainView: View {
.sheet(isPresented: $isNewUserPresenting, content: { LoginView() })
.onAppear {
if username == "" {
let headers: HTTPHeaders = [
"cookie": "SESSDATA=\(sessdata)",
"referer": "https://message.bilibili.com/", // rdar://gh/SocialSisterYi/bilibili-API-collect/issues/631#issuecomment-2099276628
"User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
]
biliWbiSign(paramEncoded: "mid=\(dedeUserID)".base64Encoded()) { signed in
if let signed {
DarockKit.Network.shared.requestJSON("https://api.bilibili.com/x/space/wbi/acc/info?\(signed)", headers: headers) { respJson, isSuccess in
if isSuccess {
debugPrint(respJson)
if !CheckBApiError(from: respJson) { return }
username = respJson["data"]["name"].string ?? ""
userSign = respJson["data"]["sign"].string ?? ""
userFaceUrl = respJson["data"]["face"].string ?? "E"
if let bd = respJson["data"]["birthday"].string, let mo = bd.split(separator: "-")[from: 0], let d = bd.split(separator: "-")[from: 1] {
if let imo = Int(mo), let id = Int(d), imo == Date.now.month && id == Date.now.day {
festivalType = .birthday
}
}
} else if isShowNetworkFixing {
isNetworkFixPresented = true
Task {
if let info = await BiliAPI.shared.userInfo() {
username = info.name
userSign = info.sign
userFaceUrl = info.face
let bd = info.birthday
if let mo = bd.split(separator: "-")[from: 0], let d = bd.split(separator: "-")[from: 1] {
if let imo = Int(mo), let id = Int(d), imo == Date.now.month && id == Date.now.day {
festivalType = .birthday
}
}
}
Expand Down
20 changes: 6 additions & 14 deletions MeowBili/Live/LiveDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,12 @@ struct LiveDetailView: View {
tagName = respJson["data"]["tags"].string ?? "[加载失败]"
if let upUid = respJson["data"]["uid"].int64 {
streamerId = upUid
biliWbiSign(paramEncoded: "mid=\(upUid)".base64Encoded()) { signed in
if let signed {
debugPrint(signed)
autoRetryRequestApi("https://api.bilibili.com/x/space/wbi/acc/info?\(signed)", headers: headers) { respJson, isSuccess in
if isSuccess {
if !CheckBApiError(from: respJson) { return }
streamerFaceUrl = respJson["data"]["face"].string ?? "E"
streamerName = respJson["data"]["name"].string ?? "[加载失败]"
DarockKit.Network.shared.requestJSON("https://api.bilibili.com/x/relation/stat?vmid=\(upUid)", headers: headers) { respJson, isSuccess in
if isSuccess {
streamerFansCount = respJson["data"]["follower"].int ?? -1
}
}
}
Task {
if let info = await BiliAPI.shared.userInfo(of: String(upUid)) {
streamerFaceUrl = info.face
streamerName = info.name
if let relation = await BiliAPI.shared.userRelation(of: String(upUid)) {
streamerFansCount = relation.follower
}
}
}
Expand Down
26 changes: 5 additions & 21 deletions MeowBili/PersonalCenter/PersonAccountView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,27 +216,11 @@ struct PersonAccountView: View {
}
func getAccountInfo() {
if username == "" {
getBuvid(url: "https://api.bilibili.com/x/space/wbi/acc/info".urlEncoded()) { buvid3, buvid4, _uuid, _ in
let headers: HTTPHeaders = [
"cookie": "SESSDATA=\(sessdata); innersign=0; buvid3=\(buvid3); b_nut=1704873471; i-wanna-go-back=-1; b_ut=7; b_lsid=9910433CB_18CF260AB89; _uuid=\(_uuid); enable_web_push=DISABLE; header_theme_version=undefined; home_feed_column=4; browser_resolution=3440-1440; buvid4=\(buvid4);",
"referer": "https://message.bilibili.com/", // rdar://gh/SocialSisterYi/bilibili-API-collect/issues/631#issuecomment-2099276628
"User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
]
biliWbiSign(paramEncoded: "mid=\(dedeUserID)".base64Encoded()) { signed in
if let signed {
debugPrint(signed)
autoRetryRequestApi("https://api.bilibili.com/x/space/wbi/acc/info?\(signed)", headers: headers) { respJson, isSuccess in
if isSuccess {
debugPrint(respJson)
if !CheckBApiError(from: respJson) { return }
username = respJson["data"]["name"].string ?? ""
userSign = respJson["data"]["sign"].string ?? ""
userFaceUrl = respJson["data"]["face"].string ?? "E"
} else if isShowNetworkFixing {
isNetworkFixPresented = true
}
}
}
Task {
if let info = await BiliAPI.shared.userInfo() {
username = info.name
userSign = info.sign
userFaceUrl = info.face
}
}
}
Expand Down
Loading

0 comments on commit ade9e18

Please sign in to comment.