Skip to content

Commit

Permalink
get subdir from content title
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiye committed Feb 18, 2024
1 parent 6823583 commit 6145542
Showing 1 changed file with 57 additions and 47 deletions.
104 changes: 57 additions & 47 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
Expand All @@ -17,29 +16,38 @@ internal class Program {
static async Task Main(string[] args) {

try {
Console.Clear();

var url = args.Length < 1 ? null : args[0];
var subDir = args.Length < 2 ? null : args[1];

if (string.IsNullOrEmpty(url)) {
Write("Enter book url: ", ConsoleColor.White);
Write("Enter book url: ");
url = Console.ReadLine();
}
if (string.IsNullOrEmpty(subDir)) {
Write("Enter book subDir: ", ConsoleColor.White);
subDir = Console.ReadLine();
}

if (string.IsNullOrWhiteSpace(url))
throw new Exception("Book url can not be empty");
subDir = subDir.Trim();

Console.CursorVisible = false;
Console.Clear();

WriteLine("Retrieving book content... ");
Write("Retrieving book content... ");
var content = (await GetContent(url)).TrimEnd();
WriteLine($"\tDone!", ConsoleColor.Green);

var title = string.Empty;
var pTitle = Regex.Matches(content, @"<meta property=""og:title"" content=""([^>]+)>");
if (pTitle.Count != 0 && pTitle[0].Groups.Count > 1) {
title = GetValidFileName(pTitle[0].Groups[1].Value, true);
Console.Title = $"Downloading: '{title}'";
}

var subDir = args.Length < 2 ? null : args[1];
if (string.IsNullOrEmpty(subDir)) {
Write("Press 'Enter' to use '");
Write($"{title}", ConsoleColor.Yellow);
Write("' as subdir, or enter new one: ");
var value = Console.ReadLine();
subDir = string.IsNullOrWhiteSpace(value) ? title : GetValidFileName(value, true);
}

WriteLine("Parsing book content... ");
var coll = Regex.Matches(content, @"new BookPlayer\([\d]+,\s(\[[^\[]+\]),\s\[");
if (coll.Count == 0 || coll[0].Groups.Count < 2)
throw new Exception("No tracks found");
Expand All @@ -50,30 +58,25 @@ internal class Program {
if (tracks == null || tracks.Length == 0)
throw new Exception("Error getting list of tracks");

Write("Downloading ");
Write($"{tracks.Length}", ConsoleColor.Green);
WriteLine(" tracks...", ConsoleColor.White);

Console.CursorVisible = false;
var asm = Assembly.GetExecutingAssembly();
var outPath = Path.GetDirectoryName(asm.Location);
if (!string.IsNullOrEmpty(subDir)) {
subDir = GetValidFileName(subDir);
outPath = Path.Combine(outPath, subDir);
Directory.CreateDirectory(outPath);
}
Write("Target folder: ");
WriteLine(outPath, ConsoleColor.Cyan);
WriteLine(outPath, ConsoleColor.DarkCyan);

Console.WriteLine();
var done = 0;
var failed = 0;
var progressLine = 7; //7 to 22
//todo: var errors = new List<string>();
var progressLine = 7;
ShowProgress(tracks.Length, done, failed);

var tasks = Enumerable.Range(0, tracks.Length)
.Select(async (i) => {
var track = tracks[i];
var fileName = GetValidFileName(track.title);
var fileName = GetValidFileName(track.title, false);
if (Path.GetExtension(fileName) != ".mp3")
fileName += ".mp3";
try {
Expand All @@ -83,51 +86,58 @@ internal class Program {
lock (gate) {
done++;
Console.SetCursorPosition(0, progressLine++);
if (progressLine > 22)
if (progressLine > 23)
progressLine = 7;
Write($"{fileName}");
WriteLine("\tDone!", ConsoleColor.Green);
Write($"{DateTime.Now:G} - ", ConsoleColor.Gray);
Write($"{fileName}", ConsoleColor.Cyan);
Write("\tDone!", ConsoleColor.Green);
}
}
catch (Exception ex) {
lock (gate) {
failed++;
Console.SetCursorPosition(0, progressLine++);
if (progressLine > 22)
if (progressLine > 23)
progressLine = 7;
Write($"\nError getting file from {track.url}: ", ConsoleColor.Red);
WriteLine(ex.Message, ConsoleColor.Yellow);
Write($"{DateTime.Now:G} - ", ConsoleColor.Gray);
Write($"Error loading {track.url}: ", ConsoleColor.Red);
Write(ex.Message, ConsoleColor.Yellow);
}
}
finally {
lock (gate) {
Console.SetCursorPosition(0, 5);
Write("Total: ", ConsoleColor.White);
Write($"{tracks.Length}", ConsoleColor.Cyan);
Write($", done: ", ConsoleColor.White);
Write($"{done}", ConsoleColor.Green);
if (failed > 0) {
Write($", failed: ", ConsoleColor.White);
Write($"{failed}", ConsoleColor.Red);
}
}
ShowProgress(tracks.Length, done, failed);
}
}).ToArray();
await Task.WhenAll(tasks);

Console.SetCursorPosition(0, 24);
WriteLine("Finished");
Console.SetCursorPosition(0, 25);
WriteLine("Finished", ConsoleColor.DarkCyan);
}
catch (Exception ex) {
Console.SetCursorPosition(0, 24);
Console.SetCursorPosition(0, 25);
WriteLine(ex.Message, ConsoleColor.Red);
}

Console.SetCursorPosition(0, 25);
Console.CursorVisible = true;
Console.SetCursorPosition(0, 27);
WriteLine("Press any key to exit...");
Console.ReadKey();
}

private static void ShowProgress(int count, int done, int failed) {
lock (gate) {
Console.SetCursorPosition(0, 5);
Write("Downloading ", ConsoleColor.White);
Write($"{count}", ConsoleColor.Cyan);
Write(" tracks, done: ", ConsoleColor.White);
Write($"{done}", ConsoleColor.Green);
if (failed > 0) {
Write($", failed: ", ConsoleColor.White);
Write($"{failed}", ConsoleColor.Red);
}
}
}

private static async Task<string> GetContent(string uri, int timeout = 10, string method = "GET") {
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = method;
Expand All @@ -146,11 +156,11 @@ internal class Program {
}
}

private static string GetValidFileName(string fileName) {
if (string.IsNullOrWhiteSpace(fileName))
private static string GetValidFileName(string fileName, bool allowEmpty) {
if (string.IsNullOrWhiteSpace(fileName) && !allowEmpty)
throw new ArgumentException("File name can not be empty.");
foreach (char c in Path.GetInvalidFileNameChars()) {
fileName = fileName.Replace(c, '_');
fileName = fileName.Replace(c, ' ').Trim();
}
return fileName;
}
Expand Down

0 comments on commit 6145542

Please sign in to comment.