Fix parsing channel url

This commit is contained in:
Arkadiusz Fal 2022-08-22 00:37:29 +02:00
parent bb43e1c377
commit 24ed872560
2 changed files with 21 additions and 1 deletions

View File

@ -13,6 +13,7 @@ final class URLParserTests: XCTestCase {
private static let channelsByName: [String: String] = [
"https://www.youtube.com/c/tennistv": "tennistv",
"https://www.youtube.com/achannel": "achannel",
"youtube.com/c/MKBHD": "MKBHD",
"c/ABCDE": "ABCDE"
]

View File

@ -43,12 +43,22 @@ struct URLParser {
}
guard let id = videoID, !id.isEmpty else {
if isYoutubeHost {
return .channel
}
return nil
}
return .video
}
var isYoutubeHost: Bool {
guard let urlComponents = urlComponents else { return false }
return urlComponents.host == "youtube.com" || urlComponents.host == "www.youtube.com"
}
var videoID: String? {
if host == "youtu.be", !path.isEmpty {
return String(path.suffix(from: path.index(path.startIndex, offsetBy: 1)))
@ -88,7 +98,10 @@ struct URLParser {
}
var channelName: String? {
guard hasAnyOfPrefixes(path, ["c/", "/c/"]) else { return nil }
guard hasAnyOfPrefixes(path, ["c/", "/c/"]) else {
if isYoutubeHost { return pathWithoutForwardSlash }
return nil
}
return removePrefixes(path, Self.prefixes[.channel]!.map { [$0, "/"].joined() })
}
@ -108,6 +121,12 @@ struct URLParser {
urlComponents?.host ?? ""
}
private var pathWithoutForwardSlash: String {
guard let urlComponents = urlComponents else { return "" }
return String(urlComponents.path.dropFirst())
}
private var path: String {
removePrefixes(urlComponents?.path ?? "", ["www.youtube.com", "youtube.com"])
}