diff --git a/Shared Tests/URLParserTests.swift b/Shared Tests/URLParserTests.swift index a4ed4da0..0fba40ae 100644 --- a/Shared Tests/URLParserTests.swift +++ b/Shared Tests/URLParserTests.swift @@ -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" ] diff --git a/Shared/URLParser.swift b/Shared/URLParser.swift index 64d84bf8..e3769c41 100644 --- a/Shared/URLParser.swift +++ b/Shared/URLParser.swift @@ -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"]) }