Freigabe über Skript oder Programm downladen
Guten Tag ,
ist es möglich den Inhalt einer Freigabe
https://dracoon...de/public/download-shares/....
auch per Skript (Perl ) oder Programm (C#) herunterzuladen.
Mit freundlichen Grüßen
Volker Jonas
-
Hi Volker,
für C# kann ich dir schon mal ein Beispiel geben. Ich hoffe ich hab richtig erkannt, was du suchst.
using System;
using System.Net;
using Newtonsoft.Json;
using RestSharp;
namespace Snippets {
public class DownloadShare {
public void DownloadShareLink() {
string shareLink = "https://dracoon.team/#/public/shares-downloads/kljjshdhfg768s9dfgweu5t98ze";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Uri shareUri = new Uri(shareLink);
string host = shareUri.Scheme + "://" + shareUri.Host; // extract host from share link
string shareAccessKey = shareLink.Substring(shareLink.LastIndexOf('/') + 1); // extract access key from url
ShareInfoModel shareInfo = DownloadShareInfo(host, shareAccessKey); // download share info to get the file name
using (WebClient wc = new WebClient()) {
wc.DownloadFile(shareLink, "C:\\temp\\" + shareInfo.FileName); // download share to local file
}
}
private ShareInfoModel DownloadShareInfo(string host, string shareAccessKey) {
RestRequest request = new RestRequest("api/v4/public/shares/downloads/" + shareAccessKey, Method.GET);
IRestClient client = new RestClient(host) {
UserAgent = "DracoonApiSnippets"
};
IRestResponse response = client.Execute(request);
if (!response.IsSuccessful) {
throw new Exception(response.ErrorMessage);
}
return JsonConvert.DeserializeObject<ShareInfoModel>(response.Content);
}
private class ShareInfoModel {
[JsonProperty("fileName")]
public string FileName { get; set; }
}
}
}Viele Grüße
Andreasp.s. Der Share-Link oben ist natürlich nur ein Dummy und funktioniert nicht :)
-
Hallo Andreas,
vielen Dank für die schnelle Antwort. Aber was als Download mit meiner Test-URL ankommt ist was in HTML , vermutlich was man üblicherweise bekommt wenn man den Link in einem Browser eingibt. Eine ganz ähnliches Ergebnis hatte ich mit dieser Perl-Zeile
use strict;
use warnings;use LWP::Simple;
my $url = 'https://dracoon.../public/download-shares/....';
my $file = 'test.zip';getstore($url, $file);
Ich hatte als Idee den Browser über C# fernzusteuern aber sowas ist eher eine fragile Programmierung.
Habe mal bei anderen Cloud-Anbietern geguckt, bei Dropbox hängt man einfach ein ?raw=1 an die URL und schon kann den Inhalt direkt herunterladen.
Grüße
Volker
-
Hallo Volker,
ehm ja du hast natürlich Recht... Da fehlt noch ein Schritt, welcher die Download URL für die Datei selbst abholt. Das geht mittels:
/v4/public/shares/downloads/{access_key} (POST)
Mit der Download URL, die dann zurück kommt, kann die richtige Datei heruntergeladen werden, was bei meinem Beispiel wie folgt aussehen würde:
using System;
using System.Net;
using Newtonsoft.Json;
using RestSharp;
namespace Snippets {
public class DownloadShare {
public void DownloadShareLink() {
string shareLink = "https://dracoon.team/#/public/shares-downloads/kljjshdhfg768s9dfgweu5t98ze";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Uri shareUri = new Uri(shareLink);
string host = shareUri.Scheme + "://" + shareUri.Host; // extract host from share link
string shareAccessKey = shareLink.Substring(shareLink.LastIndexOf('/') + 1); // extract access key from url
ShareInfoModel shareInfo = DownloadShareInfo(host, shareAccessKey); // download share info to get the file name
ShareDownloadInfo downloadInfo = DownloadShareDownloadInfo(host, shareAccessKey); // get the generated download url for the file
using (WebClient wc = new WebClient()) {
wc.DownloadFile(downloadInfo.DownloadUrl, "C:\\temp\\" + shareInfo.FileName); // download share to local file
}
}
private ShareInfoModel DownloadShareInfo(string host, string shareAccessKey) {
RestRequest request = new RestRequest("api/v4/public/shares/downloads/" + shareAccessKey, Method.GET);
IRestClient client = new RestClient(host) {
UserAgent = "DracoonApiSnippets"
};
IRestResponse response = client.Execute(request);
if (!response.IsSuccessful) {
throw new Exception(response.ErrorMessage);
}
return JsonConvert.DeserializeObject<ShareInfoModel>(response.Content);
}
private ShareDownloadInfo DownloadShareDownloadInfo(string host, string shareAccessKey) {
RestRequest request = new RestRequest("api/v4/public/shares/downloads/" + shareAccessKey, Method.POST);
IRestClient client = new RestClient(host) {
UserAgent = "DracoonApiSnippets"
};
IRestResponse response = client.Execute(request);
if (!response.IsSuccessful) {
throw new Exception(response.ErrorMessage);
}
return JsonConvert.DeserializeObject<ShareDownloadInfo>(response.Content);
}
private class ShareInfoModel {
[JsonProperty("fileName")]
public string FileName { get; set; }
}
private class ShareDownloadInfo {
[JsonProperty("downloadUrl")]
public string DownloadUrl { get; set; }
}
}
}Viele Grüße
Andreas
Bitte melden Sie sich an, um einen Kommentar zu hinterlassen.
Kommentare
4 Kommentare