Skip to content

Commit

Permalink
Renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAndreiM committed Jul 28, 2024
1 parent 3c066fc commit cf40e54
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/Juka/GUI/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public static async Task GenerateMedia()
{
VideoId = v.VideoId,
Title = v.Title,
Description = v.Description
Description = v.Description,
Published = v.Published
}).ToList();

}
Expand Down
12 changes: 9 additions & 3 deletions src/Juka/GUI/Renderer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using SDL2;
using static Juka.GUI.Globals;
using static Juka.GUI.Helper;
using static Juka.GUI.Colors;
using System;

namespace Juka.GUI
{
Expand Down Expand Up @@ -86,7 +88,7 @@ public static void RenderMedia(nint fontFooter, SDL.SDL_Color colorWhite, SDL.SD

for (int i = 0; i < videoInfos.Count; i++)
{
RenderYThumb(videoInfos[i].VideoId,i);
RenderYThumb(videoInfos[i].Title,videoInfos[i].Published, i);
RenderText(videoInfos[i].Title, 160, 200 + (100 * i), menufont, colorWhite);
var mydesc = videoInfos[i].Description;
string[] lines = mydesc.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
Expand Down Expand Up @@ -167,11 +169,11 @@ public static void RenderMedia(nint fontFooter, SDL.SDL_Color colorWhite, SDL.SD
RenderText(keyboardbuffer,430,122,fontFooter, colorRed);
}

public static void RenderYThumb(string videoId, int offset)
public static void RenderYThumb(string title, string published, int offset)
{

// Load the logo image
nint ylogo = SDL_image.IMG_Load(videoId+".jpg");
nint ylogo = SDL_image.IMG_Load("temp/"+ YouTubeApiService.SanitizeFileName(title)+ ".jpg");
if (ylogo == nint.Zero)
{
Console.WriteLine("Failed to load image!" + SDL_image.IMG_GetError());
Expand All @@ -190,6 +192,10 @@ public static void RenderYThumb(string videoId, int offset)
// Free the surface and destroy the texture after rendering
SDL.SDL_FreeSurface(ylogo);
SDL.SDL_DestroyTexture(texture);

string[] parts = published.Split('T');
string datePart = parts[0];
RenderText(datePart, 10, 272 + (100 * offset), descriptionfont, colorWhite);
}


Expand Down
2 changes: 2 additions & 0 deletions src/Juka/GUI/VideoInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ public class VideoInfo
public string VideoId { get; set; }

public string Description { get; set; }

public string Published { get; set; }
}
}
17 changes: 14 additions & 3 deletions src/Juka/GUI/YouTube.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static Juka.GUI.Globals;

Expand Down Expand Up @@ -73,7 +74,8 @@ private async Task DownloadThumbnailsAsync(List<VideoInfo> videos)

foreach (var video in videos)
{
var filePath = Path.Combine(_tempDirectory, $"{video.VideoId}.jpg");
var sanitizedTitle = SanitizeFileName(video.Title);
var filePath = Path.Combine(_tempDirectory, $"{sanitizedTitle}.jpg");
if (!File.Exists(filePath))
{
tasks.Add(DownloadImageAsync($"https://img.youtube.com/vi/{video.VideoId}/0.jpg", filePath));
Expand All @@ -83,11 +85,19 @@ private async Task DownloadThumbnailsAsync(List<VideoInfo> videos)
await Task.WhenAll(tasks);
}

public static string SanitizeFileName(string title)
{
var invalidChars = Path.GetInvalidFileNameChars();
var sanitizedTitle = new string(title.Select(ch => invalidChars.Contains(ch) ? '_' : ch).ToArray());
return sanitizedTitle.Length > 25 ? sanitizedTitle.Substring(0, 25) : sanitizedTitle;
}

public void DeleteThumbnails(List<VideoInfo> videos)
{
foreach (var video in videos)
{
var filePath = Path.Combine(_tempDirectory, $"{video.VideoId}.jpg");
var sanitizedTitle = SanitizeFileName(video.Title);
var filePath = Path.Combine(_tempDirectory, $"{sanitizedTitle}.jpg");
if (File.Exists(filePath))
{
DeleteImage(filePath);
Expand Down Expand Up @@ -151,6 +161,7 @@ private YouTubeResponse DeserializeVideoResponse(string json)
{
videoInfo.Title = snippet.GetProperty("title").GetString();
videoInfo.Description = snippet.GetProperty("description").GetString();
videoInfo.Published = snippet.GetProperty("publishedAt").GetString();
}

youtubeResponse.Items.Add(videoInfo);
Expand Down Expand Up @@ -195,4 +206,4 @@ public class YouTubeResponse
public List<VideoInfo> Items { get; set; }
}
}
}
}

0 comments on commit cf40e54

Please sign in to comment.