Commit 69743608 authored by Kirill's avatar Kirill

Контент туров скачивается на диск

parent f0633a9f
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using File = TourDataManager.Entities.File;
namespace TourDataManager {
public class ContentLoadingTask {
private ITourFilesListFetcher filesFetcher;
private long tourId;
public ContentLoadingTask(long tourId, ITourFilesListFetcher filesFetcher){
this.filesFetcher = filesFetcher;
this.tourId = tourId;
}
public void Run(){
Debug.Log($"ContentLoadingTask.Run(){{ tourId = {tourId} }}");
var files = filesFetcher.FetchFilesAsync(tourId).Result;
var webClient = new WebClient();
var len = files.Length;
var count = 0;
foreach (var file in files){
Debug.Log($"{++count} of {len} | {file.LocalUrl}");
var dir = Path.GetDirectoryName(file.LocalUrl);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
webClient.DownloadFile(new Uri(file.Url),file.LocalUrl);
}
}
}
}
\ No newline at end of file
......@@ -90,6 +90,7 @@ namespace TourDataManager {
}
public void InsertFiles(IEnumerable<File> files){
Debug.Log("Inserting Files list to database");
var diff = files.Where(file => !context.Files.Any(file1 => file.Url == file1.Url));
context.Files.AddRange(diff);
context.SaveChanges();
......
......@@ -35,16 +35,14 @@ namespace TourDataManager {
/// <param name="tourId"></param>
/// <returns></returns>
public async Task<File[]> FetchFilesAsync(long tourId){
Debug.Log("Fetching Files list");
var response = await HttpClient.GetAsync(
$"https://biganto.com/api-novus/tours.getFiles?client=desktopplayer&client_version=3.0&v=2.0&id={tourId}");
var content = await response.Content.ReadAsStringAsync();
var fukin_dict_wrap = JsonConvert.DeserializeObject<Dictionary<string,File[]>>(content);
var files = fukin_dict_wrap[tourId.ToString()];
var regex = new Regex(@"\/assets.+?(?=\?|$)");
var regex = new Regex(@"assets.+?(?=\?|$)");
// TODO Что если не сматчится?
foreach (var file in files){
// Id AutoIncrement
......@@ -56,43 +54,4 @@ namespace TourDataManager {
return files;
}
}
public class ContentLoadingTask {
private ITourFilesListFetcher filesFetcher;
private long tourId;
public ContentLoadingTask(long tourId, ITourFilesListFetcher filesFetcher){
this.filesFetcher = filesFetcher;
this.tourId = tourId;
}
public async Task Run(){
Debug.Log($"ContentLoadingTask.Run(){{ tourId = {tourId} }}");
var files = await filesFetcher.FetchFilesAsync(tourId);
var webClient = new WebClient();
async void Download(File file){
await webClient.DownloadFileTaskAsync(new Uri(file.LocalUrl),file.LocalUrl);
}
await Task.Factory.StartNew(() => {
// Parallel.ForEach(files, new ParallelOptions{MaxDegreeOfParallelism = 4}, Download);
});
foreach (var file in files){
Debug.Log(file);
}
Debug.Log("Done");
}
}
}
\ No newline at end of file
......@@ -15,7 +15,7 @@ namespace TourDataManager {
/// </summary>
public class TourDataManager {
private static string PersistentPath;
public static volatile string PersistentPath;
public static string GetPathInPersistent(string relative){
return Path.Combine(PersistentPath, relative);
......
......@@ -105,6 +105,7 @@
<ItemGroup>
<Compile Include="Authenticator.cs" />
<Compile Include="BigantoErrorException.cs" />
<Compile Include="ContentLoadingTask.cs" />
<Compile Include="CookieStorage.cs" />
<Compile Include="Db.cs" />
<Compile Include="Debug.cs" />
......
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using TourDataManager;
using TourDataManager.Entities;
......@@ -33,8 +35,14 @@ namespace TourDataManagerConsoleApplication {
}
System.Diagnostics.Debug.Assert(tours != null, nameof(tours) + " != null");
var contentLoadingTask = plugin.DownloadTourContent(tours[0].Id);
await contentLoadingTask.Run();
var contentLoadingTask = plugin.DownloadTourContent(tours[2].Id);
var x = Task.Factory
.StartNew(() => { contentLoadingTask.Run(); })
.ContinueWith(task => {
Debug.Log("Done");
});
} catch (Exception e){
Debug.Log(e);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment