mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-08-17 00:31:42 +02:00
.github
.idea
Application
Browser
Configuration
Controls
Dialogs
Management
Plugins
Properties
Resources
Updates
Utils
BrowserUtils.cs
NativeMethods.cs
TwitterFileDownloader.cs
WindowsUtils.cs
bld
lib
subprocess
video
.gitattributes
.gitignore
LICENSE.md
Program.cs
README.md
Reporter.cs
TweetDuck.csproj
TweetDuck.sln
TweetDuck.sln.DotSettings
Version.cs
app.config
packages.config
42 lines
1.3 KiB
C#
42 lines
1.3 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Threading.Tasks;
|
|
using CefSharp;
|
|
using TweetDuck.Management;
|
|
using TweetLib.Browser.Interfaces;
|
|
using TweetLib.Utils.Static;
|
|
using Cookie = CefSharp.Cookie;
|
|
|
|
namespace TweetDuck.Utils {
|
|
sealed class TwitterFileDownloader : IFileDownloader {
|
|
public static TwitterFileDownloader Instance { get; } = new TwitterFileDownloader();
|
|
|
|
private TwitterFileDownloader() {}
|
|
|
|
public string CacheFolder => BrowserCache.CacheFolder;
|
|
|
|
public void DownloadFile(string url, string path, Action onSuccess, Action<Exception> onFailure) {
|
|
const string authCookieName = "auth_token";
|
|
|
|
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}";
|
|
}
|
|
}
|
|
|
|
WebClient client = WebUtils.NewClient(BrowserUtils.UserAgentChrome);
|
|
client.Headers[HttpRequestHeader.Cookie] = cookieStr;
|
|
client.DownloadFileCompleted += WebUtils.FileDownloadCallback(path, onSuccess, onFailure);
|
|
client.DownloadFileAsync(new Uri(url), path);
|
|
});
|
|
}
|
|
}
|
|
}
|