diff --git a/Shared Tests/URLParserTests.swift b/Shared Tests/URLParserTests.swift index 0fba40ae..6c196fb4 100644 --- a/Shared Tests/URLParserTests.swift +++ b/Shared Tests/URLParserTests.swift @@ -5,6 +5,7 @@ final class URLParserTests: XCTestCase { "https://www.youtube.com/watch?v=_E0PWQvW-14&list=WL&index=4&t=155s": "_E0PWQvW-14", "https://youtu.be/IRsc57nK8mg?t=20": "IRsc57nK8mg", "yattee://youtu.be/oCtYBqcN7QE": "oCtYBqcN7QE", + "https://www.youtube.com/shorts/TjOh-gfIE2s": "TjOh-gfIE2s", "https://www.youtube-nocookie.com/watch?index=4&v=cE1PSQrWc11&list=WL&t=155s": "cE1PSQrWc11", "https://invidious.snopyta.org/watch?v=XpowfENlJAw": "XpowfENlJAw", "/watch?v=VQ_f5RymW70": "VQ_f5RymW70", diff --git a/Shared/URLParser.swift b/Shared/URLParser.swift index e3769c41..0c17202c 100644 --- a/Shared/URLParser.swift +++ b/Shared/URLParser.swift @@ -2,6 +2,7 @@ import CoreMedia import Foundation struct URLParser { + static var shortsPrefix = "/shorts/" static let prefixes: [Destination: [String]] = [ .playlist: ["/playlist", "playlist"], .channel: ["/c", "c", "/channel", "channel", "/user", "user"], @@ -59,11 +60,20 @@ struct URLParser { return urlComponents.host == "youtube.com" || urlComponents.host == "www.youtube.com" } + var isShortsPath: Bool { + path.hasPrefix(Self.shortsPrefix) + } + var videoID: String? { if host == "youtu.be", !path.isEmpty { return String(path.suffix(from: path.index(path.startIndex, offsetBy: 1))) } + if isYoutubeHost, isShortsPath { + let index = path.index(path.startIndex, offsetBy: Self.shortsPrefix.count) + return String(path[index...]) + } + return queryItemValue("v") } @@ -99,7 +109,7 @@ struct URLParser { var channelName: String? { guard hasAnyOfPrefixes(path, ["c/", "/c/"]) else { - if isYoutubeHost { return pathWithoutForwardSlash } + if channelID == nil, username == nil { return pathWithoutForwardSlash } return nil } return removePrefixes(path, Self.prefixes[.channel]!.map { [$0, "/"].joined() })