-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from MakeshVineeth/v1.3
v1.3
- Loading branch information
Showing
11 changed files
with
442 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using System.Collections.Generic; | ||
using System.Drawing; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.Http.Json; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using Blast.API.Search; | ||
using Blast.Core.Results; | ||
using static WikiPreview.Fluent.Plugin.WikiPreviewSearchApp; | ||
using static WikiPreview.Fluent.Plugin.WikiResult; | ||
|
||
namespace WikiPreview.Fluent.Plugin | ||
{ | ||
/// <summary> | ||
/// A static class that stores the common methods to generate search results. | ||
/// Also stores and initializes WikipediaLogo which will be used as placeholder for Wiki Results with no images. | ||
/// Stores the Image Size (set in FS Plugin Settings) and is used across multiple places. | ||
/// </summary> | ||
public static class ResultGenerator | ||
{ | ||
private static readonly BitmapImageResult WikipediaLogo; | ||
private static int _imageSizePrefs; | ||
|
||
static ResultGenerator() | ||
{ | ||
var assembly = Assembly.GetExecutingAssembly(); | ||
WikipediaLogo = | ||
new BitmapImageResult( | ||
assembly.GetManifestResourceStream("WikiPreview.Fluent.Plugin.Wikipedia-logo.png")); | ||
} | ||
|
||
public static int GetImageSizePrefs() | ||
{ | ||
return _imageSizePrefs; | ||
} | ||
|
||
public static void SetImageSizePrefs(int size) | ||
{ | ||
_imageSizePrefs = size; | ||
} | ||
|
||
public static async ValueTask<WikiPreviewSearchResult> GenerateSearchResult(PageView value, | ||
string searchedText, bool loadImage = true) | ||
{ | ||
string displayedName = value?.Title; | ||
string pageId = value?.PageId.ToString(); | ||
if (string.IsNullOrWhiteSpace(pageId) || string.IsNullOrWhiteSpace(displayedName)) return null; | ||
|
||
string resultName = value.Extract; | ||
if (string.IsNullOrWhiteSpace(resultName)) | ||
resultName = "Description not available for this Search Result."; | ||
|
||
double score = displayedName.SearchTokens(searchedText); | ||
string wikiUrl = displayedName.Replace(' ', '_'); | ||
BitmapImageResult bitmapImageResult = null; | ||
|
||
if (loadImage && value.Thumbnail != null) | ||
{ | ||
string imgUrl = value.Thumbnail.Source; | ||
|
||
using var imageClient = new HttpClient(); | ||
imageClient.DefaultRequestHeaders.UserAgent.TryParseAdd(UserAgentString); | ||
|
||
await imageClient.GetStreamAsync(imgUrl).ContinueWith(task => | ||
{ | ||
if (!task.IsCompletedSuccessfully) return; | ||
var bitmap = | ||
new Bitmap(task.Result); // Wiki Images are not working with AvaloniaBitmap as of now. | ||
|
||
if (!bitmap.Size.IsEmpty) | ||
bitmapImageResult = new BitmapImageResult(bitmap); | ||
}); | ||
} | ||
|
||
bitmapImageResult ??= WikipediaLogo; | ||
|
||
return new WikiPreviewSearchResult(resultName) | ||
{ | ||
Url = wikiUrl, | ||
PreviewImage = bitmapImageResult, | ||
DisplayedName = displayedName, | ||
SearchedText = searchedText, | ||
Score = score, | ||
SearchObjectId = pageId, | ||
PinUniqueId = pageId | ||
}; | ||
} | ||
|
||
public static async ValueTask<WikiPreviewSearchResult> GenerateOnDemand(string searchId, | ||
bool isCustomPreview = false, | ||
bool loadImage = true) | ||
{ | ||
if (string.IsNullOrWhiteSpace(searchId)) | ||
return default; | ||
|
||
string searchType = isCustomPreview ? "titles=" : "pageids="; | ||
|
||
string url = "https://en.wikipedia.org/w/api.php?action=query&prop=extracts|pageimages&" + searchType + | ||
searchId + | ||
"&explaintext&exintro&pilicense=any&pithumbsize=100&format=json"; | ||
|
||
using var httpClient = new HttpClient(); | ||
httpClient.DefaultRequestHeaders.UserAgent.TryParseAdd(UserAgentString); | ||
var wiki = await httpClient.GetFromJsonAsync<Wiki>(url, SerializerOptions); | ||
|
||
Dictionary<string, PageView>.ValueCollection pages = wiki?.Query?.Pages?.Values; | ||
if (pages is { Count: 0 }) return default; | ||
|
||
PageView pageView = pages?.First(); | ||
return await GenerateSearchResult(pageView, pageView?.Title, loadImage); | ||
} | ||
} | ||
} |
26 changes: 13 additions & 13 deletions
26
WikiPreview.Fluent.Plugin/WikiPreview.Fluent.Plugin.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0-windows10.0.19041</TargetFramework> | ||
<ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch> | ||
None | ||
</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch> | ||
<Platforms>AnyCPU;x64</Platforms> | ||
<AssemblyVersion>1.2.0.0</AssemblyVersion> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Platforms>AnyCPU;x64</Platforms> | ||
<AssemblyVersion>1.3.0.0</AssemblyVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AsyncEnumerator" Version="4.0.2" /> | ||
<PackageReference Include="Blast.API" Version="0.9.76.5-beta" /> | ||
<PackageReference Include="TextCopy" Version="1.5.1" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="AsyncEnumerator" Version="4.0.2"/> | ||
<PackageReference Include="Blast.API" Version="0.9.89.9990-beta"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Include="Wikipedia-logo.png"/> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.