Commit f88a196a authored by Kirill's avatar Kirill

Список ассетов тура выводится в лог

parent eec4d977
namespace TourDataManager.Entities {
public class File {
public long TourId{ get; set; }
public string Url{ get; set; }
public int Size{ get; set; }
public string LocalUrl{ get; set; }
public override string ToString(){ return $"{Size} | {Url}"; }
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Ninject;
using TourDataManager.Entities;
namespace TourDataManager {
public interface ITourFilesListFetcher {
Task<File[]> FetchFilesAsync(long tourId);
}
public class FetchTourContentUseCase : ITourFilesListFetcher{
[Inject] public HttpClient HttpClient{ get; set; }
public ContentLoadingTask FetchTourContentFromServerAsync(long tourId){
Debug.Log($"ContentLoadingTask queued for tour {tourId}");
var contentLoadingTask = new ContentLoadingTask(tourId, this);
// add to queue
return contentLoadingTask;
}
public async Task<File[]> FetchFilesAsync(long tourId){
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.+?(?=\?|$)");
foreach (var file in files){
file.TourId = tourId;
file.LocalUrl = TourDataManager.GetPathInPersistent(regex.Match(file.Url).Value);
}
return files;
}
}
public class ContentLoadingTask {
private string PersistentPath;
private ITourFilesListFetcher filesFetcher;
private long tourId;
public ContentLoadingTask(long tourId, ITourFilesListFetcher filesFetcher){
this.filesFetcher = filesFetcher;
this.tourId = tourId;
}
public async Task Run(){
var files = await filesFetcher.FetchFilesAsync(tourId);
// add to db
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
using System; using System;
using System.Data.Entity; using System.Data.Entity;
using System.IO;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Ninject; using Ninject;
...@@ -14,18 +15,22 @@ namespace TourDataManager { ...@@ -14,18 +15,22 @@ namespace TourDataManager {
/// </summary> /// </summary>
public class TourDataManager { public class TourDataManager {
private static string PersistentPath;
public static string GetPathInPersistent(string relative){
return Path.Combine(PersistentPath, relative);
}
public FetchEstatesUseCase estatesfetch{ get; set; } public FetchEstatesUseCase estatesfetch{ get; set; }
public FetchTourPreviewsUseCase tourpreviewsfetch{ get; set; } public FetchTourPreviewsUseCase tourpreviewsfetch{ get; set; }
public Db database{ get; set; } public Db database{ get; set; }
private string persistentPath;
private IKernel Container; private IKernel Container;
public TourDataManager(string persistentPath){ public TourDataManager(string persistentPath){
this.persistentPath = persistentPath; PersistentPath = persistentPath;
Container = new StandardKernel(new MyModule(persistentPath)); Container = new StandardKernel(new MyModule());
estatesfetch = Container.Get<FetchEstatesUseCase>(); estatesfetch = Container.Get<FetchEstatesUseCase>();
...@@ -53,18 +58,17 @@ namespace TourDataManager { ...@@ -53,18 +58,17 @@ namespace TourDataManager {
database.InsertTours(tours); database.InsertTours(tours);
return tours; return tours;
} }
public ContentLoadingTask DownloadTourContent(long tourId){
return Container.Get<FetchTourContentUseCase>().FetchTourContentFromServerAsync(tourId);
}
} }
public class MyModule : NinjectModule { public class MyModule : NinjectModule {
private readonly string persistentPath;
public MyModule(string persistentPath){
this.persistentPath = persistentPath;
}
public override void Load(){ public override void Load(){
var cookieStorage = new CookieStorage(persistentPath + "\\cookie.storage"); var cookieStorage = new CookieStorage(TourDataManager.GetPathInPersistent("cookie.storage"));
var httpClientHandler = new HttpClientHandler(); var httpClientHandler = new HttpClientHandler();
//CookieContainer сам запоминает полученные из ответа куки //CookieContainer сам запоминает полученные из ответа куки
httpClientHandler.CookieContainer = cookieStorage.Get(); httpClientHandler.CookieContainer = cookieStorage.Get();
...@@ -76,6 +80,8 @@ namespace TourDataManager { ...@@ -76,6 +80,8 @@ namespace TourDataManager {
Bind<HttpClient>().ToConstant(httpClient).InSingletonScope(); Bind<HttpClient>().ToConstant(httpClient).InSingletonScope();
Bind<IAuthenticator>().To<Authenticator>().InSingletonScope(); Bind<IAuthenticator>().To<Authenticator>().InSingletonScope();
Bind<FetchEstatesUseCase>().ToSelf().InSingletonScope(); Bind<FetchEstatesUseCase>().ToSelf().InSingletonScope();
Bind<FetchTourPreviewsUseCase>().ToSelf().InSingletonScope();
Bind<FetchTourContentUseCase>().ToSelf().InSingletonScope();
Bind<Db>().ToSelf().InSingletonScope(); Bind<Db>().ToSelf().InSingletonScope();
} }
......
...@@ -109,8 +109,10 @@ ...@@ -109,8 +109,10 @@
<Compile Include="Db.cs" /> <Compile Include="Db.cs" />
<Compile Include="Debug.cs" /> <Compile Include="Debug.cs" />
<Compile Include="Entities\Estate.cs" /> <Compile Include="Entities\Estate.cs" />
<Compile Include="Entities\File.cs" />
<Compile Include="Entities\Tour.cs" /> <Compile Include="Entities\Tour.cs" />
<Compile Include="FetchEstatesUseCase.cs" /> <Compile Include="FetchEstatesUseCase.cs" />
<Compile Include="FetchTourContentUseCase.cs" />
<Compile Include="FetchTourPreviewsUseCase.cs" /> <Compile Include="FetchTourPreviewsUseCase.cs" />
<Compile Include="IAuthenticator.cs" /> <Compile Include="IAuthenticator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
......
...@@ -22,15 +22,19 @@ namespace TourDataManagerConsoleApplication { ...@@ -22,15 +22,19 @@ namespace TourDataManagerConsoleApplication {
public static async void AsyncMethod(TourDataManager.TourDataManager plugin){ public static async void AsyncMethod(TourDataManager.TourDataManager plugin){
try{ try{
var estates = await plugin.DownloadEstates(); var estates = await plugin.DownloadEstates();
Tour[] tours = null;
foreach (var estate in estates){ foreach (var estate in estates){
Debug.Log(estate); Debug.Log(estate);
var tours = await plugin.DownloadTourPreviews(estate.Id); tours = await plugin.DownloadTourPreviews(estate.Id);
foreach (var tour in tours){ foreach (var tour in tours){
Debug.Log(tour); Debug.Log(tour);
} }
} }
System.Diagnostics.Debug.Assert(tours != null, nameof(tours) + " != null");
var contentLoadingTask = plugin.DownloadTourContent(tours[0].Id);
await contentLoadingTask.Run();
} 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