Skip to content

Commit

Permalink
misc: extract useragents to Keys.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
yichengchen committed May 13, 2024
1 parent 37edb78 commit 3522596
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
4 changes: 4 additions & 0 deletions BilibiliLive.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
496400D32943431E0098ACA6 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 496400D22943431E0098ACA6 /* Logger.swift */; };
4973502B29161B770045C26B /* WeeklyWatchViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4973502A29161B770045C26B /* WeeklyWatchViewController.swift */; };
4973502D29162A6D0045C26B /* StandardVideoCollectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4973502C29162A6D0045C26B /* StandardVideoCollectionViewController.swift */; };
497361082BF1A16600ED213F /* Keys.swift in Sources */ = {isa = PBXBuildFile; fileRef = 497361072BF1A16600ED213F /* Keys.swift */; };
498CF2902B63AABE0009793E /* dictionary_hash.c in Sources */ = {isa = PBXBuildFile; fileRef = 498CF2422B63AABE0009793E /* dictionary_hash.c */; };
498CF2912B63AABE0009793E /* backward_references_hq.c in Sources */ = {isa = PBXBuildFile; fileRef = 498CF2432B63AABE0009793E /* backward_references_hq.c */; };
498CF2922B63AABE0009793E /* histogram.c in Sources */ = {isa = PBXBuildFile; fileRef = 498CF2462B63AABE0009793E /* histogram.c */; };
Expand Down Expand Up @@ -157,6 +158,7 @@
496400D22943431E0098ACA6 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
4973502A29161B770045C26B /* WeeklyWatchViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeeklyWatchViewController.swift; sourceTree = "<group>"; };
4973502C29162A6D0045C26B /* StandardVideoCollectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StandardVideoCollectionViewController.swift; sourceTree = "<group>"; };
497361072BF1A16600ED213F /* Keys.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Keys.swift; sourceTree = "<group>"; };
498CF2352B63AABE0009793E /* NSData+BrotliCompression.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+BrotliCompression.h"; sourceTree = "<group>"; };
498CF2362B63AABE0009793E /* LMBrotliCompression.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LMBrotliCompression.h; sourceTree = "<group>"; };
498CF2372B63AABE0009793E /* BrotliKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BrotliKit.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -657,6 +659,7 @@
F927ED6A2610391000EAB8E3 /* Vendor */,
2D3CD3F72635BD7000191419 /* Supporting Files */,
496400D22943431E0098ACA6 /* Logger.swift */,
497361072BF1A16600ED213F /* Keys.swift */,
);
path = BilibiliLive;
sourceTree = "<group>";
Expand Down Expand Up @@ -796,6 +799,7 @@
4973502B29161B770045C26B /* WeeklyWatchViewController.swift in Sources */,
AE7A3B20290298BE006FEBB0 /* Colors.swift in Sources */,
49FB8EC0291F4C520045D5DE /* VMaskProvider.swift in Sources */,
497361082BF1A16600ED213F /* Keys.swift in Sources */,
49E5F85028AF73C500FAA3CE /* BilibiliVideoResourceLoaderDelegate.swift in Sources */,
498CF2A22B63AABE0009793E /* dictionary.c in Sources */,
494741C82902C45D005D6885 /* Array+..swift in Sources */,
Expand Down
16 changes: 12 additions & 4 deletions BilibiliLive/Component/Player/CommonPlayerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,16 @@ class CommonPlayerViewController: AVPlayerViewController {
bit in
String(format: "%.2fMbps", bit / 1024.0 / 1024.0)
}
guard let player else { return "Player no init" }

guard let log = player?.currentItem?.accessLog() else { return "no log" }
guard let item = log.events.last else { return "no event log" }
var logs = """
\(additionDebugInfo())
time control status: \(player.timeControlStatus.rawValue) \(player.reasonForWaitingToPlay?.rawValue ?? "")
player status:\(player.status.rawValue)
"""

guard let log = player.currentItem?.accessLog() else { return logs }
guard let item = log.events.last else { return logs }
let uri = item.uri ?? ""
let addr = item.serverAddress ?? ""
let changes = item.numberOfServerAddressChanges
Expand All @@ -298,14 +305,15 @@ class CommonPlayerViewController: AVPlayerViewController {
let averageVideoBitrate = item.averageVideoBitrate
let indicatedBitrate = item.indicatedBitrate
let observedBitrate = item.observedBitrate
return """
logs += """
uri:\(uri), ip:\(addr), change:\(changes)
drop:\(dropped) stalls:\(stalls)
bitrate audio:\(bitrateStr(averageAudioBitrate)), video: \(bitrateStr(averageVideoBitrate))
observedBitrate:\(bitrateStr(observedBitrate))
indicatedAverageBitrate:\(bitrateStr(indicatedBitrate))
maskProvider: \(String(describing: maskProvider)) \(additionDebugInfo())
maskProvider: \(String(describing: maskProvider)))
"""
return logs
}

func additionDebugInfo() -> String { return "" }
Expand Down
13 changes: 13 additions & 0 deletions BilibiliLive/Keys.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Keys.swift
// BilibiliLive
//
// Created by yicheng on 2024/5/13.
//

import Foundation
enum Keys {
static let userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Safari/605.1.15"
static let liveReferer = "https://live.bilibili.com"
static let referer = "https://www.bilibili.com"
}
4 changes: 2 additions & 2 deletions BilibiliLive/Module/Live/LiveDanMuProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class LiveDanMuProvider {
Logger.info("Get room server info \(server.host):\(server.wss_port)")
token = info.token
var request = URLRequest(url: URL(string: "wss://\(server.host):\(server.wss_port)/sub")!)
request.allHTTPHeaderFields = ["User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
"Referer": "https://live.bilibili.com"]
request.allHTTPHeaderFields = ["User-Agent": Keys.userAgent,
"Referer": Keys.userAgent]
websocket = WebSocket(request: request)
websocket?.delegate = self
websocket?.connect()
Expand Down
7 changes: 4 additions & 3 deletions BilibiliLive/Module/Live/LivePlayerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ class LivePlayerViewController: CommonPlayerViewController {
danMuView.play()

let headers: [String: String] = [
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
"Referer": "https://live.bilibili.com",
"User-Agent": Keys.userAgent,
"Referer": Keys.liveReferer,
]
let asset = AVURLAsset(url: URL(string: url)!, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
playerItem = AVPlayerItem(asset: asset)
player = AVPlayer(playerItem: playerItem)
player?.automaticallyWaitsToMinimizeStalling = false
} else {
showErrorAlertAndExit(title: "url is nil", message: "url: \(playInfo.first?.url.count ?? 0)")
}
Expand Down Expand Up @@ -137,7 +138,7 @@ class LivePlayerViewController: CommonPlayerViewController {
}

override func additionDebugInfo() -> String {
return "\n\(playInfo.first?.formate ?? "") \(playInfo.first?.current_qn ?? 0) failed: \(failCount)"
return "\(playInfo.first?.formate ?? "") \(playInfo.first?.current_qn ?? 0) failed: \(failCount)"
}

struct PlayInfo {
Expand Down
4 changes: 2 additions & 2 deletions BilibiliLive/Request/WebRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ enum WebRequest {
}

if !afheaders.contains(where: { $0.name == "User-Agent" }) {
afheaders.add(.userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.2 Safari/605.1.15"))
afheaders.add(.userAgent(Keys.userAgent))
}

if !afheaders.contains(where: { $0.name == "Referer" }) {
afheaders.add(HTTPHeader(name: "Referer", value: "https://www.bilibili.com"))
afheaders.add(HTTPHeader(name: "Referer", value: Keys.referer))
}

var session = Session.default
Expand Down

0 comments on commit 3522596

Please sign in to comment.