Skip to content

Commit

Permalink
Clean example project as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Voltstro committed Jul 1, 2021
1 parent 17d6c93 commit 03f3c56
Showing 1 changed file with 66 additions and 66 deletions.
132 changes: 66 additions & 66 deletions src/Wiki.Net.Example/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,79 @@

namespace WikiDotNet.Example
{
internal static class Example
{
private static void Main()
{
#region Using a proxy for requests
internal static class Example
{
private static void Main()
{
#region Using a proxy for requests

GetProxy:
string useProxy = AskUserString("Use a proxy to connect?", false).ToLower();
switch (useProxy)
{
case "true":
case "yes":
case "y":
WikiSearcher.Proxy = new WebProxy(AskUserString("Enter the proxy address"))
{
Credentials = new NetworkCredential(AskUserString("Enter the proxy username"),
AskUserString("Enter the proxy password"))
};
break;
case "false":
case "no":
case "n":
break;
default:
Console.Clear();
Console.WriteLine("Enter a valid response (Y/N or true/false)");
goto GetProxy;
}
GetProxy:
string useProxy = AskUserString("Use a proxy to connect?", false).ToLower();
switch (useProxy)
{
case "true":
case "yes":
case "y":
WikiSearcher.Proxy = new WebProxy(AskUserString("Enter the proxy address"))
{
Credentials = new NetworkCredential(AskUserString("Enter the proxy username"),
AskUserString("Enter the proxy password"))
};
break;
case "false":
case "no":
case "n":
break;
default:
Console.Clear();
Console.WriteLine("Enter a valid response (Y/N or true/false)");
goto GetProxy;
}

#endregion
#endregion

#region Loop until the user exits
#region Loop until the user exits

WikiSearchSettings searchSettings = new WikiSearchSettings
{RequestId = "Request ID", ResultLimit = 5, ResultOffset = 2, Language = "en"};
Request:
//Get a search from the user, or exit
string req = AskUserString("Enter a search query, 'exit' or 'quit' to quit");
// ReSharper disable once SwitchStatementMissingSomeCases
switch (req.ToLower())
{
case "quit":
case "exit":
Console.WriteLine("Exiting...");
return;
}
WikiSearchSettings searchSettings = new()
{RequestId = "Request ID", ResultLimit = 5, ResultOffset = 2, Language = "en"};
Request:
//Get a search from the user, or exit
string req = AskUserString("Enter a search query, 'exit' or 'quit' to quit");
// ReSharper disable once SwitchStatementMissingSomeCases
switch (req.ToLower())
{
case "quit":
case "exit":
Console.WriteLine("Exiting...");
return;
}

Console.Clear();
PrintResults(req, searchSettings);
//Wait until the user presses enter to search again
Console.WriteLine("Press any key to search again");
Console.ReadKey(true);
goto Request;
Console.Clear();
PrintResults(req, searchSettings);
//Wait until the user presses enter to search again
Console.WriteLine("Press any key to search again");
Console.ReadKey(true);
goto Request;

#endregion
}
#endregion
}

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

Console.WriteLine($"\nResults found ({searchString}):\n");
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(searchSettings.Language)} and {result.ConstantUrl(searchSettings.Language)}\n\tLast edited at {result.LastEdited}\n");
}
Console.WriteLine($"\nResults found ({searchString}):\n");
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(searchSettings.Language)} and {result.ConstantUrl(searchSettings.Language)}\n\tLast edited at {result.LastEdited}\n");
}

private static string AskUserString(string message, bool clearConsole = true)
{
if (clearConsole)
Console.Clear();
Console.WriteLine(message);
return Console.ReadLine();
}
}
private static string AskUserString(string message, bool clearConsole = true)
{
if (clearConsole)
Console.Clear();
Console.WriteLine(message);
return Console.ReadLine();
}
}
}

0 comments on commit 03f3c56

Please sign in to comment.