This commit is contained in:
Arkadiusz Fal
2022-06-30 01:31:51 +02:00
parent 25ef0a4383
commit f8fe2961ca
6 changed files with 68 additions and 3 deletions

View File

@@ -4,7 +4,7 @@ import Foundation
struct URLParser {
static let prefixes: [Destination: [String]] = [
.playlist: ["/playlist", "playlist"],
.channel: ["/c", "c", "/channel", "channel"],
.channel: ["/c", "c", "/channel", "channel", "/user", "user"],
.search: ["/results", "search"]
]
@@ -13,6 +13,21 @@ struct URLParser {
case favorites, subscriptions, popular, trending
}
var url: URL
init(url: URL) {
self.url = url
let urlString = url.absoluteString
let scheme = urlComponents?.scheme
if scheme == nil,
urlString.contains("youtube.com"),
let url = URL(string: "https://\(urlString)"
)
{
self.url = url
}
}
var destination: Destination? {
if hasAnyOfPrefixes(path, ["favorites"]) { return .favorites }
if hasAnyOfPrefixes(path, ["subscriptions"]) { return .subscriptions }
@@ -34,8 +49,6 @@ struct URLParser {
return .video
}
var url: URL
var videoID: String? {
if host == "youtu.be", !path.isEmpty {
return String(path.suffix(from: path.index(path.startIndex, offsetBy: 1)))
@@ -85,6 +98,12 @@ struct URLParser {
return removePrefixes(path, Self.prefixes[.channel]!.map { [$0, "/"].joined() })
}
var username: String? {
guard hasAnyOfPrefixes(path, ["user/", "/user/"]) else { return nil }
return removePrefixes(path, ["user/", "/user/"])
}
private var host: String {
urlComponents?.host ?? ""
}