mirror of
https://github.com/yattee/yattee.git
synced 2024-12-13 05:40:32 +05:30
Fix parsing channel url
This commit is contained in:
parent
bb43e1c377
commit
24ed872560
@ -13,6 +13,7 @@ final class URLParserTests: XCTestCase {
|
|||||||
|
|
||||||
private static let channelsByName: [String: String] = [
|
private static let channelsByName: [String: String] = [
|
||||||
"https://www.youtube.com/c/tennistv": "tennistv",
|
"https://www.youtube.com/c/tennistv": "tennistv",
|
||||||
|
"https://www.youtube.com/achannel": "achannel",
|
||||||
"youtube.com/c/MKBHD": "MKBHD",
|
"youtube.com/c/MKBHD": "MKBHD",
|
||||||
"c/ABCDE": "ABCDE"
|
"c/ABCDE": "ABCDE"
|
||||||
]
|
]
|
||||||
|
@ -43,12 +43,22 @@ struct URLParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
guard let id = videoID, !id.isEmpty else {
|
guard let id = videoID, !id.isEmpty else {
|
||||||
|
if isYoutubeHost {
|
||||||
|
return .channel
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return .video
|
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? {
|
var videoID: String? {
|
||||||
if host == "youtu.be", !path.isEmpty {
|
if host == "youtu.be", !path.isEmpty {
|
||||||
return String(path.suffix(from: path.index(path.startIndex, offsetBy: 1)))
|
return String(path.suffix(from: path.index(path.startIndex, offsetBy: 1)))
|
||||||
@ -88,7 +98,10 @@ struct URLParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var channelName: String? {
|
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() })
|
return removePrefixes(path, Self.prefixes[.channel]!.map { [$0, "/"].joined() })
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,6 +121,12 @@ struct URLParser {
|
|||||||
urlComponents?.host ?? ""
|
urlComponents?.host ?? ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var pathWithoutForwardSlash: String {
|
||||||
|
guard let urlComponents = urlComponents else { return "" }
|
||||||
|
|
||||||
|
return String(urlComponents.path.dropFirst())
|
||||||
|
}
|
||||||
|
|
||||||
private var path: String {
|
private var path: String {
|
||||||
removePrefixes(urlComponents?.path ?? "", ["www.youtube.com", "youtube.com"])
|
removePrefixes(urlComponents?.path ?? "", ["www.youtube.com", "youtube.com"])
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user