Skip to content

Commit

Permalink
git commit update process UI
Browse files Browse the repository at this point in the history
  • Loading branch information
thinhnotes committed Nov 9, 2017
1 parent 6ab2ec7 commit 63f0078
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 12 deletions.
8 changes: 8 additions & 0 deletions Pluralsight_Download/Entity/Course.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@ public enum Level
Beginner,
Advanced
}

public class LinkDownload
{
public string CDN { get; set; }
public string Source { get; set; }
public int Rank { get; set; }
public string Url { get; set; }
}
}
69 changes: 58 additions & 11 deletions Pluralsight_Download/PluralsightClient.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Pluralsight_Download.Entity;
using THttpWebRequest;
using THttpWebRequest.Utility;
using System.Threading;
using System.Threading.Tasks;

namespace PluralSight_Download
{
Expand All @@ -32,31 +37,73 @@ public bool Login(string user, string pass)
public string DownLoad(string urldownload)
{
var uri = new Uri(urldownload);
string url = "http://app.pluralsight.com/training/Player/ViewClip";
string url = "https://app.pluralsight.com/video/clips/viewclip";
var moduleName = HttpUtility.ParseQueryString(uri.Query).Get("name");
object data = new
{
a = HttpUtility.ParseQueryString(uri.Query).Get("author"),
m = HttpUtility.ParseQueryString(uri.Query).Get("name"),
course = HttpUtility.ParseQueryString(uri.Query).Get("course"),
cn = HttpUtility.ParseQueryString(uri.Query).Get("clip"),
mt = ConfigValue.FileType,
q = ConfigValue.Quality,
cap = ConfigValue.Cap,
lc = ConfigValue.Localize
author = HttpUtility.ParseQueryString(uri.Query).Get("author"),
moduleName = moduleName,
courseName = HttpUtility.ParseQueryString(uri.Query).Get("course"),
clipIndex = int.Parse(HttpUtility.ParseQueryString(uri.Query).Get("clip") ?? "0"),
mediaType = ConfigValue.FileType,
quality = ConfigValue.Quality,
includeCaptions = ConfigValue.Cap,
locale = ConfigValue.Localize
};
return Post(url, data.ToJsonString(), TypeRequest.Json);
Referer = urldownload;
var downLoad = Post(url, data.ToJsonString(), TypeRequest.Json);
var jObject = JsonConvert.DeserializeObject<JObject>(downLoad);
if(jObject!=null && jObject["urls"]!=null)
{
var urls = JsonConvert.DeserializeObject<List<LinkDownload>>(jObject["urls"].ToString());
return urls.FirstOrDefault()?.Url;
}
throw new Exception($"Not find any url download for module {moduleName}");
}

public void DownLoadFile(string url, string path, string fileName)
{
using (var webClient = new WebClient())
{
var fileSizeFromUrl = GetFileSizeFromUrl(webClient, url);

CreateIfNotExitDirectory(path);
var combine = PathCombine(path, CleanFileName(fileName));
webClient.DownloadFile(new Uri(url), combine);

if (!CheckFileIsDonloaded(combine, fileSizeFromUrl, url))
{
var progress = new ProgressBar();

webClient.DownloadProgressChanged += (sender, args) =>
{
progress.Report((double)args.BytesReceived / args.TotalBytesToReceive);
};

webClient.DownloadFileCompleted += (sender, args) =>
{
progress.Dispose();
};

webClient.DownloadFileTaskAsync(new Uri(url), combine).Wait();
}
}
}

public bool CheckFileIsDonloaded(string fileLocation, Int64 bytesTotal, string urlFile)
{
if (!File.Exists(fileLocation)) return false;

if (new FileInfo(fileLocation).Length == bytesTotal)
return true;
return false;
}

public Int64 GetFileSizeFromUrl(WebClient wc, string urlFile)
{
wc.OpenRead(urlFile);
return Convert.ToInt64(wc.ResponseHeaders["Content-Length"]);
}

public void DownloadAll(Course course, string path)
{
path = PathCombine(path, CleanFileName(course.Title));
Expand Down
4 changes: 3 additions & 1 deletion Pluralsight_Download/Pluralsight_Download.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\THttpWebRequest\THttpWebRequest.csproj">
Expand Down
1 change: 1 addition & 0 deletions THttpWebRequest/THttpWebRequest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<Compile Include="Helper\RandomHelper.cs" />
<Compile Include="Helper\StringHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utility\ProgressBar.cs" />
<Compile Include="Utility\UtilityEnumerable.cs" />
<Compile Include="Utility\UtilityObject.cs" />
<Compile Include="Utility\UtilityString.cs" />
Expand Down
102 changes: 102 additions & 0 deletions THttpWebRequest/Utility/ProgressBar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Text;
using System.Threading;

namespace THttpWebRequest.Utility
{
public class ProgressBar : IDisposable, IProgress<double>
{
private const int blockCount = 10;
private readonly TimeSpan animationInterval = TimeSpan.FromSeconds(1.0 / 8);
private const string animation = @"|/-\";

private readonly Timer timer;

private double currentProgress = 0;
private string currentText = string.Empty;
private bool disposed = false;
private int animationIndex = 0;

public ProgressBar()
{
timer = new Timer(TimerHandler);

// A progress bar is only for temporary display in a console window.
// If the console output is redirected to a file, draw nothing.
// Otherwise, we'll end up with a lot of garbage in the target file.
if (!Console.IsOutputRedirected)
{
ResetTimer();
}
}

public void Report(double value)
{
// Make sure value is in [0..1] range
value = Math.Max(0, Math.Min(1, value));
Interlocked.Exchange(ref currentProgress, value);
}

private void TimerHandler(object state)
{
lock (timer)
{
if (disposed) return;

int progressBlockCount = (int)(currentProgress * blockCount);
int percent = (int)(currentProgress * 100);
string text = string.Format("[{0}{1}] {2,3}% {3}",
new string('#', progressBlockCount), new string('-', blockCount - progressBlockCount),
percent,
animation[animationIndex++ % animation.Length]);
UpdateText(text);

ResetTimer();
}
}

private void UpdateText(string text)
{
// Get length of common portion
int commonPrefixLength = 0;
int commonLength = Math.Min(currentText.Length, text.Length);
while (commonPrefixLength < commonLength && text[commonPrefixLength] == currentText[commonPrefixLength])
{
commonPrefixLength++;
}

// Backtrack to the first differing character
StringBuilder outputBuilder = new StringBuilder();
outputBuilder.Append('\b', currentText.Length - commonPrefixLength);

// Output new suffix
outputBuilder.Append(text.Substring(commonPrefixLength));

// If the new text is shorter than the old one: delete overlapping characters
int overlapCount = currentText.Length - text.Length;
if (overlapCount > 0)
{
outputBuilder.Append(' ', overlapCount);
outputBuilder.Append('\b', overlapCount);
}

Console.Write(outputBuilder);
currentText = text;
}

private void ResetTimer()
{
timer.Change(animationInterval, TimeSpan.FromMilliseconds(-1));
}

public void Dispose()
{
lock (timer)
{
disposed = true;
UpdateText(string.Empty);
}
}

}
}

0 comments on commit 63f0078

Please sign in to comment.