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 { ...@@ -90,6 +90,7 @@ namespace TourDataManager {
} }
public void InsertFiles(IEnumerable<File> files){ 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)); var diff = files.Where(file => !context.Files.Any(file1 => file.Url == file1.Url));
context.Files.AddRange(diff); context.Files.AddRange(diff);
context.SaveChanges(); context.SaveChanges();
......
...@@ -35,16 +35,14 @@ namespace TourDataManager { ...@@ -35,16 +35,14 @@ namespace TourDataManager {
/// <param name="tourId"></param> /// <param name="tourId"></param>
/// <returns></returns> /// <returns></returns>
public async Task<File[]> FetchFilesAsync(long tourId){ public async Task<File[]> FetchFilesAsync(long tourId){
Debug.Log("Fetching Files list");
var response = await HttpClient.GetAsync( var response = await HttpClient.GetAsync(
$"https://biganto.com/api-novus/tours.getFiles?client=desktopplayer&client_version=3.0&v=2.0&id={tourId}"); $"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 content = await response.Content.ReadAsStringAsync();
var fukin_dict_wrap = JsonConvert.DeserializeObject<Dictionary<string,File[]>>(content); var fukin_dict_wrap = JsonConvert.DeserializeObject<Dictionary<string,File[]>>(content);
var files = fukin_dict_wrap[tourId.ToString()]; var files = fukin_dict_wrap[tourId.ToString()];
var regex = new Regex(@"assets.+?(?=\?|$)");
var regex = new Regex(@"\/assets.+?(?=\?|$)");
// TODO Что если не сматчится? // TODO Что если не сматчится?
foreach (var file in files){ foreach (var file in files){
// Id AutoIncrement // Id AutoIncrement
...@@ -56,43 +54,4 @@ namespace TourDataManager { ...@@ -56,43 +54,4 @@ namespace TourDataManager {
return files; 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 { ...@@ -15,7 +15,7 @@ namespace TourDataManager {
/// </summary> /// </summary>
public class TourDataManager { public class TourDataManager {
private static string PersistentPath; public static volatile string PersistentPath;
public static string GetPathInPersistent(string relative){ public static string GetPathInPersistent(string relative){
return Path.Combine(PersistentPath, relative); return Path.Combine(PersistentPath, relative);
......
...@@ -105,6 +105,7 @@ ...@@ -105,6 +105,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="Authenticator.cs" /> <Compile Include="Authenticator.cs" />
<Compile Include="BigantoErrorException.cs" /> <Compile Include="BigantoErrorException.cs" />
<Compile Include="ContentLoadingTask.cs" />
<Compile Include="CookieStorage.cs" /> <Compile Include="CookieStorage.cs" />
<Compile Include="Db.cs" /> <Compile Include="Db.cs" />
<Compile Include="Debug.cs" /> <Compile Include="Debug.cs" />
......
using System; using System;
using System.IO;
using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using TourDataManager; using TourDataManager;
using TourDataManager.Entities; using TourDataManager.Entities;
...@@ -33,8 +35,14 @@ namespace TourDataManagerConsoleApplication { ...@@ -33,8 +35,14 @@ namespace TourDataManagerConsoleApplication {
} }
System.Diagnostics.Debug.Assert(tours != null, nameof(tours) + " != null"); System.Diagnostics.Debug.Assert(tours != null, nameof(tours) + " != null");
var contentLoadingTask = plugin.DownloadTourContent(tours[0].Id); var contentLoadingTask = plugin.DownloadTourContent(tours[2].Id);
await contentLoadingTask.Run();
var x = Task.Factory
.StartNew(() => { contentLoadingTask.Run(); })
.ContinueWith(task => {
Debug.Log("Done");
});
} catch (Exception e){ } catch (Exception e){
Debug.Log(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