Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional convenience method for creating a YouTubePlayer.Source from a URL object #87

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions Sources/Models/YouTubePlayer+Source.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,39 @@ public extension YouTubePlayer.Source {
/// Creats `YouTubePlayer.Source` from a given URL string, if available
/// - Parameter url: The URL string
static func url(
_ url: String
_ urlString: String
) -> Self? {
// Initialize URLComonents from URL string
let urlComponents = URLComponents(string: url)
// Initialize URL from string
let url = URL(string: url)
// Initialize URL from string and call URL-based convenience method
guard let url = URL(string: urlString) else {
return nil
}
return Self.url(url)
}

/// Creats `YouTubePlayer.Source` from a given URL, if available
/// - Parameter url: The URL
static func url(
_ url: URL
) -> Self? {
// Initialize URLComonents from URL
let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true)
// Retrieve start seconds from "t" url parameter, if available
let startSeconds = urlComponents?.queryItems?["t"].flatMap(Int.init)
// Initialize PathComponents and drop first which is the leading "/"
let pathComponents = url?.pathComponents.dropFirst()
let pathComponents = url.pathComponents.dropFirst()
// Check if URL host has YouTube share url host
if url?.host?.lowercased().hasSuffix("youtu.be") == true {
if url.host?.lowercased().hasSuffix("youtu.be") == true {
// Check if a video id is available
if let videoId = pathComponents?.first {
if let videoId = pathComponents.first {
// Return video source
return .video(
id: videoId,
startSeconds: startSeconds
)
}
} else if url?.host?.lowercased().contains("youtube") == true {
} else if url.host?.lowercased().contains("youtube") == true {
// Otherwise switch on first path component
switch pathComponents?.first {
switch pathComponents.first {
case "watch":
// Check if a playlist identifier is available
if let playlistId = urlComponents?.queryItems?["list"] {
Expand All @@ -92,15 +102,15 @@ public extension YouTubePlayer.Source {
}
case "c", "user":
// Check if a channel name is available
if let channelName = url?.pathComponents[safe: 2] {
if let channelName = url.pathComponents[safe: 2] {
// Return channel source
return .channel(
name: channelName
)
}
default:
// Check if a video identifier is available
if let videoId = url?.pathComponents[safe: 2] {
if let videoId = url.pathComponents[safe: 2] {
// Return video source
return .video(
id: videoId,
Expand All @@ -112,7 +122,6 @@ public extension YouTubePlayer.Source {
// Otherwise return nil
return nil
}

}

// MARK: - Sequence<URLQueryItem>+subscribt
Expand Down
Loading