Skip to content

Commit 0b1cfbd

Browse files
committed
fix: drastically improve processing speed of VideoInfosWithDownloadFormatsResponse
1 parent 6136ce0 commit 0b1cfbd

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

Sources/YouTubeKit/Utils/String+ytkRegexMatches.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,14 @@ public extension String {
4242
guard let regex = try? NSRegularExpression(pattern: stringRegex) else { return nil }
4343
return self.ytkFirstGroupMatch(for: regex)
4444
}
45+
46+
/// Gets the first substring that is between a prefix and a suffix (**excluding them**)
47+
func ytkFirstSubstringBetween(prefix: String, suffix: String) -> String? {
48+
guard let prefixRange = self.range(of: prefix) else { return nil }
49+
let afterPrefix = prefixRange.upperBound
50+
51+
guard let suffixRange = self[afterPrefix...].range(of: suffix) else { return nil }
52+
53+
return String(self[afterPrefix..<suffixRange.lowerBound])
54+
}
4555
}

Sources/YouTubeKit/YouTubeResponseTypes/VideoInfos/VideoInfosWithDownloadFormatsResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public struct VideoInfosWithDownloadFormatsResponse: YouTubeResponse {
5454

5555
let (instructionArray, playerJs, playerName) = try processPlayerScrapping(playerPath: playerPath)
5656

57-
guard let stringJSONData = dataToString.ytkFirstGroupMatch(for: "var ytInitialPlayerResponse = ([\\S\\s]*\\}\\}\\}\\})[\\S\\s]*;</script><div id=\"player\"") else {
57+
guard let stringJSONData = dataToString.ytkFirstSubstringBetween(prefix: "var ytInitialPlayerResponse = ", suffix: ";</script><div id=\"player\"") else {
5858
throw ResponseError(step: .decodeData, reason: "Couldn't get player's JSON data.")
5959
}
6060

0 commit comments

Comments
 (0)