|
| 1 | +import Foundation |
| 2 | + |
| 3 | +// MARK: - URLHandler |
| 4 | + |
| 5 | +/// Handles parsing and routing of YouTube Music URLs. |
| 6 | +/// |
| 7 | +/// Supports URLs like: |
| 8 | +/// - `https://music.youtube.com/watch?v=dQw4w9WgXcQ` - Play song |
| 9 | +/// - `https://music.youtube.com/playlist?list=PLxxx` - Open playlist |
| 10 | +/// - `https://music.youtube.com/browse/MPRExxx` - Open album |
| 11 | +/// - `https://music.youtube.com/browse/VLPLxxx` - Open playlist (browse format) |
| 12 | +/// - `https://music.youtube.com/channel/UCxxx` - Open artist |
| 13 | +/// - `kaset://play?v=dQw4w9WgXcQ` - Custom scheme for song |
| 14 | +/// - `kaset://playlist?list=PLxxx` - Custom scheme for playlist |
| 15 | +/// - `kaset://album?id=MPRExxx` - Custom scheme for album |
| 16 | +/// - `kaset://artist?id=UCxxx` - Custom scheme for artist |
| 17 | +enum URLHandler { |
| 18 | + // MARK: - Types |
| 19 | + |
| 20 | + /// Represents the type of content from a parsed URL. |
| 21 | + enum ParsedContent: Sendable, Equatable { |
| 22 | + /// A song/video to play. |
| 23 | + case song(videoId: String) |
| 24 | + |
| 25 | + /// A playlist to open. |
| 26 | + case playlist(id: String) |
| 27 | + |
| 28 | + /// An album to open. |
| 29 | + case album(id: String) |
| 30 | + |
| 31 | + /// An artist/channel to open. |
| 32 | + case artist(id: String) |
| 33 | + } |
| 34 | + |
| 35 | + // MARK: - URL Parsing |
| 36 | + |
| 37 | + /// Parses a YouTube Music URL and returns the content type. |
| 38 | + /// - Parameter url: The URL to parse. |
| 39 | + /// - Returns: The parsed content, or nil if the URL is not recognized. |
| 40 | + static func parse(_ url: URL) -> ParsedContent? { |
| 41 | + // Handle custom scheme |
| 42 | + if url.scheme == "kaset" { |
| 43 | + return self.parseKasetURL(url) |
| 44 | + } |
| 45 | + |
| 46 | + // Handle YouTube Music web URLs |
| 47 | + if self.isYouTubeMusicURL(url) { |
| 48 | + return self.parseYouTubeMusicURL(url) |
| 49 | + } |
| 50 | + |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + /// Checks if a URL is a YouTube Music URL. |
| 55 | + private static func isYouTubeMusicURL(_ url: URL) -> Bool { |
| 56 | + guard let host = url.host?.lowercased() else { return false } |
| 57 | + return host == "music.youtube.com" || host == "www.music.youtube.com" |
| 58 | + } |
| 59 | + |
| 60 | + /// Parses a kaset:// custom scheme URL. |
| 61 | + private static func parseKasetURL(_ url: URL) -> ParsedContent? { |
| 62 | + guard url.scheme == "kaset" else { return nil } |
| 63 | + |
| 64 | + let host = url.host?.lowercased() ?? "" |
| 65 | + let components = URLComponents(url: url, resolvingAgainstBaseURL: false) |
| 66 | + let queryItems = components?.queryItems ?? [] |
| 67 | + |
| 68 | + switch host { |
| 69 | + case "play": |
| 70 | + // kaset://play?v=videoId |
| 71 | + if let videoId = Self.queryValue(for: "v", in: queryItems), !videoId.isEmpty { |
| 72 | + return .song(videoId: videoId) |
| 73 | + } |
| 74 | + |
| 75 | + case "playlist": |
| 76 | + // kaset://playlist?list=playlistId |
| 77 | + if let listId = Self.queryValue(for: "list", in: queryItems), !listId.isEmpty { |
| 78 | + return .playlist(id: listId) |
| 79 | + } |
| 80 | + |
| 81 | + case "album": |
| 82 | + // kaset://album?id=albumId |
| 83 | + if let albumId = Self.queryValue(for: "id", in: queryItems), !albumId.isEmpty { |
| 84 | + return .album(id: albumId) |
| 85 | + } |
| 86 | + |
| 87 | + case "artist": |
| 88 | + // kaset://artist?id=artistId |
| 89 | + if let artistId = Self.queryValue(for: "id", in: queryItems), !artistId.isEmpty { |
| 90 | + return .artist(id: artistId) |
| 91 | + } |
| 92 | + |
| 93 | + default: |
| 94 | + break |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + /// Parses a music.youtube.com URL. |
| 101 | + private static func parseYouTubeMusicURL(_ url: URL) -> ParsedContent? { |
| 102 | + let components = URLComponents(url: url, resolvingAgainstBaseURL: false) |
| 103 | + let queryItems = components?.queryItems ?? [] |
| 104 | + let path = url.path |
| 105 | + let pathLower = path.lowercased() |
| 106 | + |
| 107 | + // /watch?v=videoId - Play song |
| 108 | + if pathLower == "/watch" || pathLower.hasPrefix("/watch") { |
| 109 | + if let videoId = Self.queryValue(for: "v", in: queryItems), !videoId.isEmpty { |
| 110 | + return .song(videoId: videoId) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // /playlist?list=playlistId - Open playlist |
| 115 | + if pathLower == "/playlist" || pathLower.hasPrefix("/playlist") { |
| 116 | + if let listId = Self.queryValue(for: "list", in: queryItems), !listId.isEmpty { |
| 117 | + return .playlist(id: listId) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // /browse/XXX - Album, Playlist (VLPL prefix), or other browse content |
| 122 | + if pathLower.hasPrefix("/browse/") { |
| 123 | + // Extract browseId preserving original case |
| 124 | + let browseId = String(path.dropFirst("/browse/".count)) |
| 125 | + if !browseId.isEmpty { |
| 126 | + // VLPL prefix indicates a playlist in browse format |
| 127 | + if browseId.hasPrefix("VLPL") { |
| 128 | + // Convert VLPL... to PL... for playlist ID |
| 129 | + let playlistId = String(browseId.dropFirst(2)) |
| 130 | + return .playlist(id: playlistId) |
| 131 | + } |
| 132 | + // MPRE or OLAK prefix indicates an album |
| 133 | + if browseId.hasPrefix("MPRE") || browseId.hasPrefix("OLAK") { |
| 134 | + return .album(id: browseId) |
| 135 | + } |
| 136 | + // UC prefix indicates a channel/artist |
| 137 | + if browseId.hasPrefix("UC") { |
| 138 | + return .artist(id: browseId) |
| 139 | + } |
| 140 | + // Other browse IDs could be albums or playlists |
| 141 | + // Default to treating as album since it's under /browse |
| 142 | + return .album(id: browseId) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // /channel/UCxxx - Open artist |
| 147 | + if pathLower.hasPrefix("/channel/") { |
| 148 | + // Extract channelId preserving original case |
| 149 | + let channelId = String(path.dropFirst("/channel/".count)) |
| 150 | + if !channelId.isEmpty { |
| 151 | + return .artist(id: channelId) |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return nil |
| 156 | + } |
| 157 | + |
| 158 | + /// Gets a query parameter value. |
| 159 | + private static func queryValue(for name: String, in items: [URLQueryItem]) -> String? { |
| 160 | + items.first { $0.name == name }?.value |
| 161 | + } |
| 162 | +} |
0 commit comments