using TuneRipper.Shared.Models; using System.Text.RegularExpressions; namespace TuneRipper.Api.Services; public interface IVideoService { Task GetVideoDetailsAsync(string url); bool IsValidYouTubeUrl(string url); string ExtractVideoId(string url); } public class VideoService : IVideoService { private readonly ILogger _logger; public VideoService(ILogger logger) { _logger = logger; } public async Task GetVideoDetailsAsync(string url) { try { if (!IsValidYouTubeUrl(url)) { _logger.LogWarning("Invalid YouTube URL: {Url}", url); return null; } // Simulate API call delay await Task.Delay(Random.Shared.Next(500, 2000)); var videoId = ExtractVideoId(url); // In production, use YouTube API or yt-dlp to get actual video details var videoDetails = new YouTubeVideoDetails { Id = videoId, Title = GenerateSampleTitle(videoId), Description = $"Video description for {videoId}. In production, this would be fetched from YouTube API.", ThumbnailUrl = $"https://img.youtube.com/vi/{videoId}/maxresdefault.jpg", Duration = TimeSpan.FromMinutes(Random.Shared.Next(2, 10)).Add(TimeSpan.FromSeconds(Random.Shared.Next(0, 59))), Author = $"Channel_{Random.Shared.Next(1, 1000)}", ViewCount = Random.Shared.Next(100, 5000000), UploadDate = DateTime.UtcNow.AddDays(-Random.Shared.Next(1, 1000)), Tags = GenerateSampleTags() }; _logger.LogInformation("Successfully fetched details for video {VideoId}", videoId); return videoDetails; } catch (Exception ex) { _logger.LogError(ex, "Error fetching video details for URL: {Url}", url); return null; } } public bool IsValidYouTubeUrl(string url) { if (string.IsNullOrWhiteSpace(url)) return false; var patterns = new[] { @"^https?://(www\.)?youtube\.com/watch\?v=[\w-]+", @"^https?://(www\.)?youtu\.be/[\w-]+", @"^https?://(www\.)?youtube\.com/embed/[\w-]+", @"^https?://(www\.)?youtube\.com/v/[\w-]+" }; return patterns.Any(pattern => Regex.IsMatch(url, pattern, RegexOptions.IgnoreCase)); } public string ExtractVideoId(string url) { if (string.IsNullOrWhiteSpace(url)) return string.Empty; try { // Different YouTube URL formats var patterns = new Dictionary { { @"youtu\.be/([^?&]+)", "$1" }, { @"youtube\.com/watch\?v=([^&]+)", "$1" }, { @"youtube\.com/embed/([^?&]+)", "$1" }, { @"youtube\.com/v/([^?&]+)", "$1" } }; foreach (var pattern in patterns) { var match = Regex.Match(url, pattern.Key, RegexOptions.IgnoreCase); if (match.Success) { return match.Groups[1].Value; } } } catch (Exception ex) { _logger.LogError(ex, "Error extracting video ID from URL: {Url}", url); } return string.Empty; } private static string GenerateSampleTitle(string videoId) { var sampleTitles = new[] { "Amazing Music Video", "Best Song Ever", "Epic Guitar Solo", "Relaxing Piano Music", "Dance Hit 2024", "Acoustic Session", "Live Performance", "Official Music Video" }; var randomTitle = sampleTitles[Random.Shared.Next(sampleTitles.Length)]; return $"{randomTitle} - {videoId}"; } private static List GenerateSampleTags() { var allTags = new[] { "music", "video", "song", "artist", "album", "live", "official", "acoustic", "cover", "remix" }; var tagCount = Random.Shared.Next(3, 7); return allTags.OrderBy(x => Random.Shared.Next()).Take(tagCount).ToList(); } }