Skip to content

Commit

Permalink
Added thread count
Browse files Browse the repository at this point in the history
Fixed search dialog
  • Loading branch information
weespin committed May 31, 2021
1 parent 75d18f6 commit b56774c
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 19 deletions.
40 changes: 40 additions & 0 deletions Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 91 additions & 14 deletions Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using HtmlAgilityPack;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using HtmlDocument = HtmlAgilityPack.HtmlDocument;

namespace KhinsiderDownloader
{

public partial class Form1 : Form
{

Expand Down Expand Up @@ -41,14 +42,28 @@ private void checkUpdates()
Process.Start("https://github.com/weespin/KhinsiderDownloader/releases");
}
}
else if (newVersion < currentVersion)
{
if (label1.InvokeRequired)
{
label1.Invoke(new Action(() =>
{
label1.Text = "BETA " + label1.Text;
}));

}
}

}

public Form1()
{
InitializeComponent();
Program.MainForm = this;
lbl_path.Text = Downloader.sDownloadPath;
LoadConfig();
Task.Run(() => { checkUpdates(); });
num_threads.Value = 2;
}

public void Log(string textIn)
Expand All @@ -70,7 +85,7 @@ public void LoadConfig()
{
var configLines = File.ReadAllLines("khinsiderdl.config");
lbl_path.Text = Downloader.sDownloadPath = configLines[0];

Downloader.eQuality = bool.Parse(configLines[1])
? Downloader.EDownloadQuality.QUALITY_MP3_ONLY
: Downloader.EDownloadQuality.QUALITY_BEST_ONLY;
Expand All @@ -82,6 +97,8 @@ public void LoadConfig()
{
radio_betterquality.Checked = true;
}

Downloader.g_parralelopt.MaxDegreeOfParallelism = Int32.Parse(configLines[2]);
}
catch (Exception)
{
Expand All @@ -91,16 +108,17 @@ public void LoadConfig()

void SaveConfig()
{
string[] configLines = new string[2];
string[] configLines = new string[3];
configLines[0] = Downloader.sDownloadPath;
configLines[1] = (Downloader.eQuality == Downloader.EDownloadQuality.QUALITY_MP3_ONLY).ToString();
configLines[2] = Downloader.g_parralelopt.MaxDegreeOfParallelism.ToString();
File.WriteAllLines("khinsiderdl.config", configLines);
}

private void button1_Click(object sender, EventArgs e)
{
btn_download.Enabled = false;
List<string> urls = txt_urllist.Text.Split('\n').ToList();
List<string> urls = txt_urllist.Text.Split(new string[] {Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
Task.Run(() =>
{
foreach (var url in urls)
Expand Down Expand Up @@ -153,11 +171,19 @@ private void btn_opensearch_Click(object sender, EventArgs e)
searchForm.linkbox = txt_urllist ;
searchForm.Show();
}

private void num_threads_ValueChanged(object sender, EventArgs e)
{
Downloader.g_parralelopt.MaxDegreeOfParallelism = (int)num_threads.Value;
SaveConfig();
}
}


static class Downloader
{
public static ParallelOptions g_parralelopt = new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount };

public enum EDownloadQuality : byte
{
QUALITY_MP3_ONLY,
Expand Down Expand Up @@ -189,7 +215,8 @@ public static void DownloadAlbum(string sUrl)
string albumName = "error";
if (albumNameNode != null)
{
albumName = albumNameNode.InnerText;
albumName = string.Join("_", albumNameNode.InnerText.Split(Path.GetInvalidFileNameChars())).Trim();
//albumName = albumNameNode.InnerText.Trim();
}
else
{
Expand Down Expand Up @@ -224,14 +251,47 @@ public static void DownloadAlbum(string sUrl)
}

//Thread.Sleep(1);

List<Task> currentTasks = new List<Task>();

songNodes.AsParallel().ForAll(song =>
ThreadPool.SetMinThreads(g_parralelopt.MaxDegreeOfParallelism, g_parralelopt.MaxDegreeOfParallelism);
System.Net.ServicePointManager.DefaultConnectionLimit = Int32.MaxValue;
//foreach (var song in songNodes)
//{
Parallel.ForEach(songNodes, g_parralelopt,song=>
{
var songPageURL = "https://downloads.khinsider.com" + song.ChildNodes[0].Attributes["href"].Value;

var songPageDocument = webContext.Load(songPageURL);
HtmlDocument songPageDocument = new HtmlDocument();
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(songPageURL);
httpWebRequest.KeepAlive = false;
httpWebRequest.Timeout = 30 * 1000; //TCP timeout
using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
if (httpWebResponse.StatusCode == HttpStatusCode.OK)
{
using (Stream responseStream = httpWebResponse.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
var htmlstring = reader.ReadToEnd();

songPageDocument.LoadHtml(htmlstring);
}
}

}
}
}
catch (Exception e)
{
string message = $"Failed to parse {songPageURL} ({e.Message})";
Program.MainForm.Log(message);
#if DEBUG
Debug.WriteLine(message);
#endif
return;
}

var downloadLinkNodes = songPageDocument.DocumentNode.SelectNodes("//span[@class='songDownloadLink']"); //[1].ParentElement.GetAttribute("href");
foreach (var dlsongentry in downloadLinkNodes)
Expand All @@ -243,18 +303,35 @@ public static void DownloadAlbum(string sUrl)
downloadClient.Proxy = null;
var name = WebUtility.UrlDecode(songFileURL.Substring(songFileURL.LastIndexOf("/") + 1));
Program.MainForm.Log($"Downloading {name}...");
Task currentTask = downloadClient.DownloadFileTaskAsync(new Uri(songFileURL),
Downloader.sDownloadPath + "\\" +
string.Join("_", albumName.Split(Path.GetInvalidFileNameChars())) + "\\" +
string.Join("_", name.Split(Path.GetInvalidFileNameChars())));

string filename = Downloader.sDownloadPath + "\\" +
albumName + "\\" +
string.Join("_", name.Split(Path.GetInvalidFileNameChars()));

try
{
Task currentTask = downloadClient.DownloadFileTaskAsync(new Uri(songFileURL), filename);
currentTasks.Add(currentTask);
currentTask.ContinueWith((
task => { Program.MainForm.Log($"{name} has been downloaded!"); }));
}
catch (Exception e)
{
string message = $"Failed to download {songFileURL} to {filename} ({e.Message})";
Program.MainForm.Log(message);
#if DEBUG
Debug.WriteLine(message);
#endif
}

}
}
//}// for foreach
});
Task.WaitAll(currentTasks.ToArray());
Program.MainForm.Log($"Finished downloading {albumName}!");
}


}
}
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.1.0.0")]
[assembly: AssemblyFileVersion("2.1.0.0")]
[assembly: AssemblyVersion("2.2.0.0")]
[assembly: AssemblyFileVersion("2.2.0.0")]
8 changes: 5 additions & 3 deletions SearchForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace KhinsiderDownloader
{
public partial class SearchForm : Form
{
static string urlPrefix = "http://downloads.khinsider.com";

WebClient webClient;
public TextBox linkbox = null;
public SearchForm()
Expand Down Expand Up @@ -98,7 +100,7 @@ void ResolveImage(ref SearchItem searchItem)
{

HtmlWeb webContext = new HtmlWeb();
var htmlDocument = webContext.Load(searchItem.Url);
var htmlDocument = webContext.Load(urlPrefix+searchItem.Url);
var possibleCoverNode =
htmlDocument.DocumentNode.SelectSingleNode("/html[1]/body[1]/table[1]/tbody[1]/tr[1]/td[1]/div[1]/div[1]/div[2]/div[2]/table[1]/tbody[1]/tr[1]/td[1]/div[1]/table[1]/tr[1]/td[1]/div[1]/a[1]");
if (possibleCoverNode != null)
Expand Down Expand Up @@ -155,15 +157,15 @@ private void btn_add_Click(object sender, EventArgs e)
return;
}
SearchItem currentItem = (SearchItem)list_result.SelectedItem;
linkbox.Text += currentItem.Url + Environment.NewLine;
linkbox.Text += urlPrefix + currentItem.Url + Environment.NewLine;
}

private void btn_add_all_Click(object sender, EventArgs e)
{
foreach (var item in list_result.Items)
{
SearchItem currentItem = (SearchItem)item;
linkbox.Text += currentItem.Url + Environment.NewLine;
linkbox.Text += urlPrefix + currentItem.Url + Environment.NewLine;
}
}
}
Expand Down

0 comments on commit b56774c

Please sign in to comment.