Adding some CSharp classes
This commit is contained in:
parent
10426b1dcd
commit
ffb3bf8f73
59
CSharp/ExecuteCommand.csharp
Normal file
59
CSharp/ExecuteCommand.csharp
Normal file
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace AniNIX.Shared {
|
||||
|
||||
public static class ExecuteCommand {
|
||||
|
||||
/// <summary>
|
||||
/// This method allows a CSharp app to execute a command on the OS.
|
||||
/// </summary>
|
||||
/// <param name=command>The command string to run as the string argument to "bash -c 'command'"</param>
|
||||
/// <param name=input>The effective replacement for the command's stdin</param
|
||||
/// <return>The stdout of the command</return>
|
||||
/// </summary>
|
||||
public static String Run(String command, String input) {
|
||||
//Sanitize inputs.
|
||||
if (command.Contains("\'")) {
|
||||
throw new Exception("Command strings cannot include \'.");
|
||||
}
|
||||
|
||||
//Create process.
|
||||
Process proc = new Process();
|
||||
proc.StartInfo.CreateNoWindow = true;
|
||||
proc.StartInfo.FileName = "/bin/bash";
|
||||
proc.StartInfo.Arguments = String.Format("-c \'{0}\'",command);
|
||||
proc.StartInfo.UseShellExecute=false;
|
||||
ReportMessage.Log(Verbosity.Verbose,String.Format("{0} {1}",proc.StartInfo.FileName,proc.StartInfo.Arguments));
|
||||
|
||||
//Redirect input
|
||||
proc.StartInfo.RedirectStandardOutput=true;
|
||||
proc.StartInfo.RedirectStandardInput=true;
|
||||
|
||||
//Start process
|
||||
proc.Start();
|
||||
|
||||
//Add input and read output.
|
||||
proc.StandardInput.Write(input);
|
||||
proc.StandardInput.Close();
|
||||
proc.WaitForExit();
|
||||
if (proc.ExitCode != 0) {
|
||||
throw new Exception(String.Format("Failed to exit command with return code {0}",proc.ExitCode));
|
||||
}
|
||||
String stdoutString = proc.StandardOutput.ReadToEnd();
|
||||
|
||||
//Close up and return
|
||||
proc.Close();
|
||||
return stdoutString;
|
||||
}
|
||||
|
||||
//Add polymorphism to allow no stdin
|
||||
public static String Run(String command) {
|
||||
return Run(command,null);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
47
CSharp/ReportMessage.csharp
Normal file
47
CSharp/ReportMessage.csharp
Normal file
@ -0,0 +1,47 @@
|
||||
using System;
|
||||
|
||||
namespace AniNIX.Shared {
|
||||
|
||||
public enum Verbosity {
|
||||
Always = -2,
|
||||
Error,
|
||||
Quiet = 0,
|
||||
Verbose,
|
||||
VeryVerbose,
|
||||
Explicit,
|
||||
}
|
||||
|
||||
public static class ReportMessage {
|
||||
|
||||
// Set this statically here, but allow others to override.
|
||||
public static Verbosity verbosity = Verbosity.Quiet;
|
||||
|
||||
/// <summary>
|
||||
/// Log a new message for the user.
|
||||
/// </summary>
|
||||
public static void Log(Verbosity level,String message) {
|
||||
|
||||
if (level == Verbosity.Error) {
|
||||
Console.Error.WriteLine(message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (ReportMessage.verbosity == Verbosity.Quiet) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (level == Verbosity.Always
|
||||
|| (ReportMessage.verbosity == Verbosity.Verbose && level == Verbosity.Verbose)
|
||||
|| (ReportMessage.verbosity == Verbosity.VeryVerbose && (level == Verbosity.Verbose || level == Verbosity.VeryVerbose))
|
||||
|| (ReportMessage.verbosity == Verbosity.Explicit && (level == Verbosity.Verbose || level == Verbosity.VeryVerbose || level == Verbosity.Explicit))
|
||||
) {
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void Log(String message) {
|
||||
Log(Verbosity.VeryVerbose,message);
|
||||
}
|
||||
}
|
||||
}
|
39
CSharp/WebPageAPI.csharp
Normal file
39
CSharp/WebPageAPI.csharp
Normal file
@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Security;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace AniNIX.Shared {
|
||||
|
||||
public static class WebPageAPI {
|
||||
|
||||
// Thanks to MSDN for this regex.
|
||||
// https://msdn.microsoft.com/en-us/library/ms998267.aspx
|
||||
public static Regex URLRegEx = new Regex(@"(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?");
|
||||
|
||||
/// <summary>
|
||||
/// Get a webpage source -- we use this instead of WebClient because Mono doesn't handle SSL well on Linux.
|
||||
/// /usr/bin/curl -s SOMEURL
|
||||
/// </summary>
|
||||
/// <param name="pageURL"> the webpage whose title we should get</param>
|
||||
/// <returns> the webpage source </returns>
|
||||
public static string GetPage(String pageURL) {
|
||||
return ExecuteCommand.Run(String.Format("/usr/bin/curl -s {0}",pageURL));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a webpage title
|
||||
/// Should be equivalent to:
|
||||
/// /bin/bash -c '/usr/bin/curl -s SOMEURL | perl -l -0777 -ne "print \$1 if /<title.*?>\s*(.*?)\s*<\/title/si"'
|
||||
/// </summary>
|
||||
/// <param name="pageURL"> the webpage whose title we should get</param>
|
||||
/// <returns> the webpage title </returns>
|
||||
public static string GetPageTitle(String pageURL) {
|
||||
string source = GetPage(pageURL);
|
||||
return Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user