Skip to content

Commit

Permalink
Development (#2)
Browse files Browse the repository at this point in the history
Merge development into Stable for V2.0.0
  • Loading branch information
EternalClickbait authored Oct 29, 2019
1 parent 882faad commit 76bf5d9
Show file tree
Hide file tree
Showing 15 changed files with 356 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,4 @@ ASALocalRun/

# MFractors (Xamarin productivity tool) working folder
.mfractor/
*.cs___jb_tmp___
40 changes: 24 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Wiki.Net

[![License](https://img.shields.io/github/license/Creepysin-Studios/Wiki.Net)](/LICENSE) [![Requirements Status](https://requires.io/github/Creepysin-Studios/Wiki.Net/requirements.svg?branch=Stable)](https://requires.io/github/Creepysin-Studios/Wiki.Net/requirements/?branch=Stable) [![NuGet](https://img.shields.io/nuget/v/Wiki.Net)](https://www.nuget.org/packages/Wiki.Net/)
[![Nuget](https://img.shields.io/nuget/dt/Wiki.Net)](https://www.nuget.org/packages/Wiki.Net/) [![Discord](https://img.shields.io/badge/Discord-Creepysin-7289da.svg?logo=discord)](https://discord.creepysin.com)

Wiki.Net – An unofficial C# Wikipedia API

## Features

Searches Wikipedia (duh!) and returns:
Searches Wikipedia (duh!) and returns (per result):
* Title
* Page ID
* Titles
* Word Count
* Size (bytes?)
* Text Preview
* URL of page
* Time of last edit

## Getting Started

Expand All @@ -29,37 +29,45 @@ You can also download the binaries from the [releases](https://github.com/Creepy

### Example

```csharp
```c#
string searchString =Computer”;
WikiSearchSettings searchSettings = new WikiSearchSettings
{RequestId = "Request ID", ResultLimit = 5, ResultOffset = 2};

WikiSearchResponse response = WikiSearcher.Search(searchString);
WikiSearchResponse response = WikiSearcher.Search(searchString, searchSettings);

Console.WriteLine($"\nResults found ({searchString}):\n");
for (int i = 0; i < response.SearchResults.Length; i++)
foreach (WikiSearchResult result in response.Query.SearchResults)
{
WikiSearchResult result = response.SearchResults[i];
Console.WriteLine($"\t{result.Title} ({result.WordCount} words, {result.Size} bytes, id {result.PageId}):\t{result.Preview}...\n\tAt {result.Url}\n\tLast edited at {result.LastEdited}\n");
Console.WriteLine(
$"\t{result.Title} ({result.WordCount} words, {result.Size} bytes, id {result.PageId}):\t{result.Preview}...\n\tAt {result.Url}\n\tLast edited at {result.LastEdited}\n");
}

Console.ReadLine();
```

**Output**
```
Computer (12154 words)
A computer is a machine that can be instructed to carry out sequences of arithmetic or logical operations automatically via computer programming. Modern...
https://en.wikipedia.org/?curid=7878457
Results found (Computer):
Computer science (7267 words)
Computer science (sometimes called computation science or computing science, but not to be confused with computational science or software engineering)...
https://en.wikipedia.org/?curid=5323
Information technology (2836 words, 27146 bytes, id 36674345): Information technology (IT) is the use of computers to store, retrieve, transmit, and manipulate data, or information, often in the context of a business...
At https://en.wikipedia.org/?curid=36674345
Last edited at 24/10/2019 11:53:39 AM
*More results*
Computer graphics (computer science) (1632 words, 18720 bytes, id 18567168): Computer graphics is a sub-field of Computer Science which studies methods for digitally synthesizing and manipulating visual content. Although the term...
At https://en.wikipedia.org/?curid=18567168
Last edited at 17/09/2019 12:21:21 AM
Computer hardware (2479 words, 22776 bytes, id 21808348): Computer hardware includes the physical, tangible parts or components of a computer, such as the cabinet, central processing unit, monitor, keyboard,...
At https://en.wikipedia.org/?curid=21808348
Last edited at 16/10/2019 4:00:29 PM
*More results*
```

## Authors

**EternalClickbait** - *Initial work* - [EternalClickbait]( https://github.com/EternalClickbait)
**EternalClickbait** - *Initial work* - [EternalClickbait](https://github.com/EternalClickbait)

## License

Expand Down
15 changes: 7 additions & 8 deletions Wiki.Net.Example/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ private static void Main()

#region Loop until the user exits

WikiSearchSettings searchSettings = new WikiSearchSettings
{RequestId = "Request ID", ResultLimit = 5, ResultOffset = 2};
Request:
//Get a search from the user, or exit
string req = AskUserString("Enter a search query, 'exit' or 'quit' to quit");
Expand All @@ -53,7 +55,7 @@ private static void Main()
}

Console.Clear();
PrintResults(req);
PrintResults(req, searchSettings);
//Wait until the user presses enter to search again
Console.WriteLine("Press any key to search again");
Console.ReadKey(true);
Expand All @@ -62,17 +64,14 @@ private static void Main()
#endregion
}

private static void PrintResults(string searchString)
private static void PrintResults(string searchString, WikiSearchSettings searchSettings = null)
{
WikiSearchResponse response = WikiSearcher.Search(searchString);
WikiSearchResponse response = WikiSearcher.Search(searchString, searchSettings);

Console.WriteLine($"\nResults found ({searchString}):\n");
for (int i = 0; i < response.SearchResults.Length; i++)
{
WikiSearchResult result = response.SearchResults[i];
foreach (WikiSearchResult result in response.Query.SearchResults)
Console.WriteLine(
$"\t{result.Title} ({result.WordCount} words, {result.Size} bytes, id {result.PageId}):\t{result.Preview}...\n\tAt {result.Url}\n\tLast edited at {result.LastEdited}\n");
}
$"\t{result.Title} ({result.WordCount} words, {result.Size} bytes, id {result.PageId}):\t{result.Preview}...\n\tAt {result.Url} and {result.ConstantUrl}\n\tLast edited at {result.LastEdited}\n");
}

private static string AskUserString(string message, bool clearConsole = true)
Expand Down
1 change: 1 addition & 0 deletions Wiki.Net.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Creepysin/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Wikipedia/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Wikipedia_0027s/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
35 changes: 35 additions & 0 deletions Wiki.Net/Error.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Newtonsoft.Json;

namespace CreepysinStudios.WikiDotNet
{
/// <summary>
/// A class that represents a Wikipedia API error
/// </summary>
// ReSharper disable once ClassCannotBeInstantiated
public sealed class Error
{
/// <summary>
/// What error code does this this error correspond to
/// </summary>
[JsonProperty("code")] public readonly string Code;

/// <summary>
/// Any extra information the assist with debugging
/// </summary>
[JsonProperty("data")] public readonly string Data;

/// <summary>
/// What Wikipedia module gave this error
/// </summary>
[JsonProperty("module")] public readonly string Module;

/// <summary>
/// Information about this error
/// </summary>
[JsonProperty("*")] public readonly string Text;

private Error()
{
}
}
}
16 changes: 16 additions & 0 deletions Wiki.Net/Inheritance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Class Inheritance

### WikiSearchResponse
* #### Query
* ##### Search Results (Array)
* ###### Ns (Namespace?)
* ###### Last edited
* ###### Page ID
* ###### Preview
* ###### Size
* ###### Title
* ###### Word Count
* ###### Url
* ##### SearchInfo
* ###### TotalHits
* #### Request ID
21 changes: 21 additions & 0 deletions Wiki.Net/SearchInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Newtonsoft.Json;

namespace CreepysinStudios.WikiDotNet
{
/// <summary>
/// A class that contains information about a Wikipedia search. Currently only contains an int for the total number of
/// results.
/// </summary>
public class SearchInfo
{
/// <summary>
/// How many hits did the search return (in total, including those not shown)
/// </summary>
// ReSharper disable once StringLiteralTypo
[JsonProperty("totalhits")] public int TotalHits;

private SearchInfo()
{
}
}
}
31 changes: 31 additions & 0 deletions Wiki.Net/Warning.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Newtonsoft.Json;

namespace CreepysinStudios.WikiDotNet
{
/// <summary>
/// A class that represents a Wikipedia API warning. Often returned when invalid parameters/arguments are passed to the
/// Wikipedia API
/// </summary>
// ReSharper disable once ClassCannotBeInstantiated
public sealed class Warning
{
/// <summary>
/// What warning code does this this warning correspond to
/// </summary>
[JsonProperty("code")] public readonly string Code;

/// <summary>
/// What Wikipedia module gave this warning
/// </summary>
[JsonProperty("module")] public readonly string Module;

/// <summary>
/// Information about this warning
/// </summary>
[JsonProperty("*")] public readonly string Text;

private Warning()
{
}
}
}
12 changes: 12 additions & 0 deletions Wiki.Net/Wiki.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>

<ItemGroup>
<Content Include="..\.gitignore">
<Link>.gitignore</Link>
</Content>
<Content Include="..\LICENSE">
<Link>LICENSE</Link>
</Content>
<Content Include="..\README.md">
<Link>README.md</Link>
</Content>
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions Wiki.Net/Wiki.Net.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=settingclasses/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
26 changes: 26 additions & 0 deletions Wiki.Net/WikiSearchQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Newtonsoft.Json;

namespace CreepysinStudios.WikiDotNet
{
/// <summary>
/// Contains an array of <see cref="WikiSearchResult" />s and a <see cref="SearchInfo" />
/// </summary>
// ReSharper disable once ClassCannotBeInstantiated
public sealed class WikiSearchQuery
{
/// <summary>
/// A read-only field that contains information such as the total amount of hits the search returned
/// </summary>
// ReSharper disable once StringLiteralTypo
[JsonProperty("searchinfo")] public readonly SearchInfo SearchInfo;

/// <summary>
/// An array of results returned from the wikipedia servers
/// </summary>
[JsonProperty("search")] public readonly WikiSearchResult[] SearchResults;

private WikiSearchQuery()
{
}
}
}
62 changes: 45 additions & 17 deletions Wiki.Net/WikiSearchResponse.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,72 @@
#region

using System;
using System.Net.Http;
using Newtonsoft.Json;

#endregion

namespace CreepysinStudios.WikiDotNet
{
/// <summary>
/// A class that contains an array of <see cref="WikiSearchResult" />, returned from the Wikipedia servers
/// An object returned by the Wikipedia API that contains a <see cref="WikiSearchQuery" /> and <see cref="RequestId" />
/// </summary>
//TODO: Add Error and warning class in case
// ReSharper disable once ClassCannotBeInstantiated
public sealed class WikiSearchResponse
{
/// <summary>
/// The Json string from which the results were taken
/// Any errors returned with the request, or <see langword="null" /> if there weren't any
/// </summary>
public readonly string JsonResult;
[JsonProperty("errors")] public readonly Error[] Errors;

/// <summary>
/// The response message from which the <see cref="SearchResults" /> and <see cref="JsonResult" /> are parsed
/// The Query that the search returned
/// </summary>
public readonly HttpResponseMessage ResponseMessage;
[JsonProperty("query")] public readonly WikiSearchQuery Query;

/// <summary>
/// An array of results returned from the wikipedia servers
/// The Request ID that was passed during the request
/// </summary>
public readonly WikiSearchResult[] SearchResults;
// ReSharper disable once StringLiteralTypo
[JsonProperty("requestid")] public readonly string RequestId;

/// <summary>
/// A constructor that creates a new <see cref="WikiSearchResponse" />
/// The Wikipedia server that this request was served by
/// </summary>
/// <param name="jsonResult">The Json string used to parse the search results</param>
/// <param name="responseMessage">The <see cref="HttpResponseMessage" /> that was returned from the server</param>
/// <param name="searchResults">An array of parsed search results</param>
internal WikiSearchResponse(string jsonResult,
HttpResponseMessage responseMessage, WikiSearchResult[] searchResults)
// ReSharper disable once StringLiteralTypo
[JsonProperty("servedby")] public readonly string ServedBy;

/// <summary>
/// The time at which the Wikipedia server received the search request
/// </summary>
// ReSharper disable once StringLiteralTypo
[JsonProperty("curtimestamp")] public readonly DateTime Timestamp;

/// <summary>
/// Any warnings returned with the request, or <see langword="null" /> if there weren't any
/// </summary>
[JsonProperty("warnings")] public readonly Warning[] Warnings;

private WikiSearchResponse()
{
JsonResult = jsonResult ?? throw new ArgumentNullException(nameof(jsonResult));
SearchResults = searchResults ?? throw new ArgumentNullException(nameof(searchResults));
ResponseMessage = responseMessage ?? throw new ArgumentNullException(nameof(responseMessage));
}

/// <summary>
/// Was this request successful, or were there errors?
/// </summary>
public bool WasSuccessful
{
get
{
//If our errors and warnings arrays are null, we know this request was successful
if (Errors == null && Warnings == null) return true;

//If our arrays aren't null and their length is not zero, return false
if (Warnings != null && Warnings.Length != 0) return false;
if (Errors != null && Errors.Length != 0) return false;

return true;
}
}
}
}
Loading

0 comments on commit 76bf5d9

Please sign in to comment.