Skip to content

Commit

Permalink
Merge pull request #151 from nooperation/develop
Browse files Browse the repository at this point in the history
Develop to master
  • Loading branch information
nooperation authored Oct 9, 2020
2 parents 096d29d + 34763bc commit 13eea65
Show file tree
Hide file tree
Showing 79 changed files with 2,853 additions and 262 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto
LibSanBag.Tests\Samples\* binary

###############################################################################
# Set default behavior for command prompt diff.
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "LibSanBag"]
path = LibSanBag
url = https://github.com/nooperation/LibSanBag.git
40 changes: 19 additions & 21 deletions AtlasView/AtlasView.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\LibSanBag.1.7.4\build\LibSanBag.props" Condition="Exists('..\packages\LibSanBag.1.7.4\build\LibSanBag.props')" />
<Import Project="..\packages\SanTools.LibUserPreferences.1.0.6\build\SanTools.LibUserPreferences.props" Condition="Exists('..\packages\SanTools.LibUserPreferences.1.0.6\build\SanTools.LibUserPreferences.props')" />
<Import Project="..\packages\SanTools.LibFSB.1.0.2\build\SanTools.LibFSB.props" Condition="Exists('..\packages\SanTools.LibFSB.1.0.2\build\SanTools.LibFSB.props')" />
<Import Project="..\packages\SanTools.LibCRN.1.0.8\build\SanTools.LibCRN.props" Condition="Exists('..\packages\SanTools.LibCRN.1.0.8\build\SanTools.LibCRN.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -42,12 +38,6 @@
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="LibSanBag, Version=1.7.4.0, Culture=neutral, processorArchitecture=AMD64">
<HintPath>..\packages\LibSanBag.1.7.4\lib\net472\LibSanBag.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
Expand Down Expand Up @@ -120,7 +110,6 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand All @@ -134,18 +123,27 @@
<Project>{c799b853-615e-4215-a17a-4e2f9687a163}</Project>
<Name>CommonUI</Name>
</ProjectReference>
<ProjectReference Include="..\LibSanBag\LibSanBag\LibSanBag.csproj">
<Project>{14a58a49-5247-442f-b0f2-c3a5d7a88d76}</Project>
<Name>LibSanBag</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="x64\LibDDS.dll" />
<PackageReference Include="HtmlAgilityPack">
<Version>1.11.16</Version>
</PackageReference>
<PackageReference Include="Newtonsoft.Json">
<Version>12.0.3</Version>
</PackageReference>
<PackageReference Include="SanTools.LibCRN">
<Version>1.0.8</Version>
</PackageReference>
<PackageReference Include="SanTools.LibFSB">
<Version>1.0.2</Version>
</PackageReference>
<PackageReference Include="SanTools.LibUserPreferences">
<Version>1.0.11</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\SanTools.LibCRN.1.0.8\build\SanTools.LibCRN.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\SanTools.LibCRN.1.0.8\build\SanTools.LibCRN.props'))" />
<Error Condition="!Exists('..\packages\SanTools.LibFSB.1.0.2\build\SanTools.LibFSB.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\SanTools.LibFSB.1.0.2\build\SanTools.LibFSB.props'))" />
<Error Condition="!Exists('..\packages\SanTools.LibUserPreferences.1.0.6\build\SanTools.LibUserPreferences.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\SanTools.LibUserPreferences.1.0.6\build\SanTools.LibUserPreferences.props'))" />
<Error Condition="!Exists('..\packages\LibSanBag.1.7.4\build\LibSanBag.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\LibSanBag.1.7.4\build\LibSanBag.props'))" />
</Target>
</Project>
144 changes: 114 additions & 30 deletions AtlasView/ViewModels/AtlasViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
using CommonUI.Views;
using CommonUI.Views.ResourceViews;
using LibSanBag.Providers;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;
using HtmlAgilityPack;

namespace AtlasView.ViewModels
{
Expand Down Expand Up @@ -216,47 +220,127 @@ private void OnPropertyChanged([CallerMemberName] string propertyName = null)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public async void Search(string query, int page=1)
private async Task SearchByQuery(string query, int page=1)
{
try
CurrentAtlasView = new LoadingView();

var loadingViewModel = new LoadingViewModel();
CurrentAtlasView.DataContext = loadingViewModel;

var progress = new Progress<ProgressEventArgs>(args => {
loadingViewModel.BytesDownloaded = args.BytesDownloaded;
loadingViewModel.CurrentResourceIndex = args.CurrentResourceIndex;
loadingViewModel.TotalResources = args.TotalResources;
loadingViewModel.Status = args.Status;
loadingViewModel.TotalBytes = args.TotalBytes;
loadingViewModel.DownloadUrl = args.Resource;
});

var perPage = 4;
var client = new HttpClientProvider();
var responseBytes = await client.GetByteArrayAsync($"https://atlas.sansar.com/proxies/web/atlas-api/v3/experiences?perPage={perPage}&q={query}&page={page}", progress);
var responseJson = Encoding.ASCII.GetString(responseBytes);

var results = JsonConvert.DeserializeObject<AtlasResponse>(responseJson);
var tempSearchResults = new List<ExperienceView>();
foreach (var experienceData in results.Data)
{
CurrentAtlasView = new LoadingView();
tempSearchResults.Add(new ExperienceView
{
DataContext = new ExperienceViewModel(experienceData)
});
}
SearchResults = tempSearchResults;

var loadingViewModel = new LoadingViewModel();
CurrentAtlasView.DataContext = loadingViewModel;
TotalPages = results.Meta.Pages;
LastQuery = query;

var progress = new Progress<ProgressEventArgs>(args => {
loadingViewModel.BytesDownloaded = args.BytesDownloaded;
loadingViewModel.CurrentResourceIndex = args.CurrentResourceIndex;
loadingViewModel.TotalResources = args.TotalResources;
loadingViewModel.Status = args.Status;
loadingViewModel.TotalBytes = args.TotalBytes;
loadingViewModel.DownloadUrl = args.Resource;
});
_currentPage = page;
OnPropertyChanged(nameof(CurrentPage));

CurrentAtlasView = null;
}

private static async Task<string> DownloadExperienceJsonByUri(string experienceUri, Progress<ProgressEventArgs> progress)
{
var match = Regex.Match(experienceUri, @".*/experience/([^/]+/.*)");
if (!match.Success)
{
throw new Exception("Invalid experience URI. Expected to be in the form of sansar://sansar.com/experience/username/experiencename");
}

var perPage = 4;
var client = new HttpClientProvider();
var responseBytes = await client.GetByteArrayAsync($"https://atlas.sansar.com/proxies/web/atlas-api/v3/experiences?perPage={perPage}&q={query}&page={page}", progress);
var responseJson = Encoding.ASCII.GetString(responseBytes);
var webUrl = $"https://atlas.sansar.com/experiences/{match.Groups[1].Value}";

var results = JsonConvert.DeserializeObject<AtlasResponse>(responseJson);
var tempSearchResults = new List<ExperienceView>();
foreach (var experienceData in results.Data)
var client = new HttpClientProvider();
var responseBytes = await client.GetByteArrayAsync(webUrl, progress);
var responseText = Encoding.ASCII.GetString(responseBytes);

var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(responseText);
var scriptNodes = htmlDocument.DocumentNode.Descendants("script");

foreach (var item in scriptNodes)
{
if (item.InnerText.StartsWith("window.__STATE__="))
{
tempSearchResults.Add(new ExperienceView
{
DataContext = new ExperienceViewModel(experienceData)
});
var scriptSource = item.InnerText.Substring("window.__STATE__=".Length);
scriptSource = Regex.Replace(scriptSource, @"new Date\(([^\)]+)\)", "$1");

var obj = JObject.Parse(scriptSource);
return obj.SelectToken("experience.data").ToString();
}
SearchResults = tempSearchResults;
}

throw new Exception("Failed to parse response.");
}

private async Task SearchByUri(string experienceUri)
{
CurrentAtlasView = new LoadingView();

var loadingViewModel = new LoadingViewModel();
CurrentAtlasView.DataContext = loadingViewModel;

var progress = new Progress<ProgressEventArgs>(args => {
loadingViewModel.BytesDownloaded = args.BytesDownloaded;
loadingViewModel.CurrentResourceIndex = args.CurrentResourceIndex;
loadingViewModel.TotalResources = args.TotalResources;
loadingViewModel.Status = args.Status;
loadingViewModel.TotalBytes = args.TotalBytes;
loadingViewModel.DownloadUrl = args.Resource;
});

var responseJson = await AtlasViewModel.DownloadExperienceJsonByUri(experienceUri, progress);
var datum = JsonConvert.DeserializeObject<Datum>(responseJson);
var tempSearchResults = new List<ExperienceView>();
tempSearchResults.Add(new ExperienceView
{
DataContext = new ExperienceViewModel(datum)
});

TotalPages = results.Meta.Pages;
LastQuery = query;
SearchResults = tempSearchResults;

_currentPage = page;
OnPropertyChanged(nameof(CurrentPage));
TotalPages = 1;
LastQuery = experienceUri;

CurrentAtlasView = null;
_currentPage = 1;
OnPropertyChanged(nameof(CurrentPage));

CurrentAtlasView = null;
}

public async Task Search(string query, int page=1)
{
try
{
if (query.ToLower().Contains("/experience/"))
{
await SearchByUri(query);
}
else
{
await SearchByQuery(query, page);
}
}
catch (Exception ex)
{
Expand Down
8 changes: 0 additions & 8 deletions AtlasView/packages.config

This file was deleted.

Loading

0 comments on commit 13eea65

Please sign in to comment.