mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-13 08:34:08 +02:00
parent
ff3dc59016
commit
bb22c35221
@ -207,9 +207,13 @@ public static WebClient CreateWebClient(){
|
||||
return client;
|
||||
}
|
||||
|
||||
public static WebClient DownloadFileAsync(string url, string target, Action onSuccess, Action<Exception> onFailure){
|
||||
public static WebClient DownloadFileAsync(string url, string target, string cookie, Action onSuccess, Action<Exception> onFailure){
|
||||
WebClient client = CreateWebClient();
|
||||
|
||||
if (cookie != null){
|
||||
client.Headers[HttpRequestHeader.Cookie] = cookie;
|
||||
}
|
||||
|
||||
client.DownloadFileCompleted += (sender, args) => {
|
||||
if (args.Cancelled){
|
||||
try{
|
||||
|
@ -8,6 +8,8 @@
|
||||
using TweetDuck.Core.Other;
|
||||
using TweetDuck.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Cookie = CefSharp.Cookie;
|
||||
|
||||
namespace TweetDuck.Core.Utils{
|
||||
static class TwitterUtils{
|
||||
@ -50,7 +52,10 @@ public static string GetMediaLink(string url, ImageQuality quality){
|
||||
if (quality == ImageQuality.Orig){
|
||||
string result = ExtractMediaBaseLink(url);
|
||||
|
||||
if (result != url || url.Contains("//pbs.twimg.com/media/")){
|
||||
if (url.Contains("//ton.twitter.com/") && url.Contains("/ton/data/dm/")){
|
||||
result += ":large";
|
||||
}
|
||||
else if (result != url || url.Contains("//pbs.twimg.com/media/")){
|
||||
result += ":orig";
|
||||
}
|
||||
|
||||
@ -83,7 +88,7 @@ void ViewImageInternal(string path){
|
||||
ViewImageInternal(file);
|
||||
}
|
||||
else{
|
||||
BrowserUtils.DownloadFileAsync(GetMediaLink(url, quality), file, () => {
|
||||
DownloadFileAuth(GetMediaLink(url, quality), file, () => {
|
||||
ViewImageInternal(file);
|
||||
}, ex => {
|
||||
FormMessage.Error("Image Download", "An error occurred while downloading the image: "+ex.Message, FormMessage.OK);
|
||||
@ -119,14 +124,14 @@ void OnFailure(Exception ex){
|
||||
}
|
||||
|
||||
if (urls.Length == 1){
|
||||
BrowserUtils.DownloadFileAsync(firstImageLink, dialog.FileName, null, OnFailure);
|
||||
DownloadFileAuth(firstImageLink, dialog.FileName, null, OnFailure);
|
||||
}
|
||||
else{
|
||||
string pathBase = Path.ChangeExtension(dialog.FileName, null);
|
||||
string pathExt = Path.GetExtension(dialog.FileName);
|
||||
|
||||
for(int index = 0; index < urls.Length; index++){
|
||||
BrowserUtils.DownloadFileAsync(GetMediaLink(urls[index], quality), $"{pathBase} {index+1}{pathExt}", null, OnFailure);
|
||||
DownloadFileAuth(GetMediaLink(urls[index], quality), $"{pathBase} {index+1}{pathExt}", null, OnFailure);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -145,11 +150,33 @@ public static void DownloadVideo(string url, string username){
|
||||
Filter = "Video"+(string.IsNullOrEmpty(ext) ? " (unknown)|*.*" : $" (*{ext})|*{ext}")
|
||||
}){
|
||||
if (dialog.ShowDialog() == DialogResult.OK){
|
||||
BrowserUtils.DownloadFileAsync(url, dialog.FileName, null, ex => {
|
||||
DownloadFileAuth(url, dialog.FileName, null, ex => {
|
||||
FormMessage.Error("Video Download", "An error occurred while downloading the video: "+ex.Message, FormMessage.OK);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void DownloadFileAuth(string url, string target, Action onSuccess, Action<Exception> onFailure){
|
||||
const string AuthCookieName = "auth_token";
|
||||
|
||||
TaskScheduler scheduler = TaskScheduler.FromCurrentSynchronizationContext();
|
||||
|
||||
using(ICookieManager cookies = Cef.GetGlobalCookieManager()){
|
||||
cookies.VisitUrlCookiesAsync(url, true).ContinueWith(task => {
|
||||
string cookieStr = null;
|
||||
|
||||
if (task.Status == TaskStatus.RanToCompletion){
|
||||
Cookie found = task.Result?.Find(cookie => cookie.Name == AuthCookieName); // the list may be null
|
||||
|
||||
if (found != null){
|
||||
cookieStr = $"{found.Name}={found.Value}";
|
||||
}
|
||||
}
|
||||
|
||||
BrowserUtils.DownloadFileAsync(url, target, cookieStr, onSuccess, onFailure);
|
||||
}, scheduler);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ public void BeginSilentDownload(){
|
||||
return;
|
||||
}
|
||||
|
||||
currentDownload = BrowserUtils.DownloadFileAsync(downloadUrl, InstallerPath, () => {
|
||||
currentDownload = BrowserUtils.DownloadFileAsync(downloadUrl, InstallerPath, null, () => {
|
||||
DownloadStatus = UpdateDownloadStatus.Done;
|
||||
currentDownload = null;
|
||||
}, e => {
|
||||
|
Loading…
Reference in New Issue
Block a user