Added the ability to download an image via REQUEST block to a screenshot file, that can then be then used in a CAPTCHA block.

This commit is contained in:
ruri
2019-10-01 18:46:05 +02:00
parent d8b5eb2133
commit c495189a11
3 changed files with 39 additions and 6 deletions
@@ -154,6 +154,7 @@
<Label Content="File Path:"/>
<TextBox Text="{Binding DownloadPath, UpdateSourceTrigger=PropertyChanged}" />
</DockPanel>
<CheckBox IsChecked="{Binding SaveAsScreenshot}" Content="Save as screenshot" VerticalContentAlignment="Center"/>
</StackPanel>
</TabItem>
</TabControl>
+12
View File
@@ -384,6 +384,18 @@ namespace RuriLib
screenshot.Save(path);
}
/// <summary>
/// Saves a stream to a file with automatically generated name.
/// </summary>
/// <param name="stream">The input stream</param>
/// <param name="data">The BotData used for path creation</param>
public static void SaveScreenshot(MemoryStream stream, BotData data)
{
var path = MakeScreenshotPath(data);
using (var fileStream = File.Create(path)) { stream.CopyTo(fileStream); }
data.Screenshots.Add(path);
}
/// <summary>
/// Builds the path for the screenshot file.
/// </summary>
+26 -6
View File
@@ -132,6 +132,10 @@ namespace RuriLib
private string downloadPath = "";
/// <summary>The path of the file where a FILE response needs to be stored.</summary>
public string DownloadPath { get { return downloadPath; } set { downloadPath = value; OnPropertyChanged(); } }
private bool saveAsScreenshot = false;
/// <summary>Whether to add the downloaded image to the default screenshot path.</summary>
public bool SaveAsScreenshot { get { return saveAsScreenshot; } set { saveAsScreenshot = value; OnPropertyChanged(); } }
#endregion
/// <summary>
@@ -231,6 +235,10 @@ namespace RuriLib
{
ResponseType = ResponseType.File;
DownloadPath = LineParser.ParseLiteral(ref input, "DOWNLOAD PATH");
while (LineParser.Lookahead(ref input) == TokenType.Boolean)
{
LineParser.SetBool(ref input, this);
}
}
}
@@ -337,7 +345,8 @@ namespace RuriLib
.Indent()
.Arrow()
.Token("FILE")
.Literal(DownloadPath);
.Literal(DownloadPath)
.Boolean(SaveAsScreenshot, "SaveAsScreenshot");
}
return writer.ToString();
@@ -528,7 +537,7 @@ namespace RuriLib
{
// Get response
response = request.Raw(method, localUrl, content);
var responseString = response.ToString();
var responseString = "";
// Get address
data.Address = response.Address.ToString();
@@ -549,8 +558,10 @@ namespace RuriLib
data.ResponseHeaders.Add(header.Key, header.Value);
data.Log(new LogEntry($"{header.Key}: {header.Value}", Colors.LightPink));
}
if (!response.ContainsHeader(HttpHeader.ContentLength))
if (!response.ContainsHeader(HttpHeader.ContentLength) && ResponseType != ResponseType.File)
{
responseString = response.ToString(); // Read the stream
if (data.ResponseHeaders.ContainsKey("Content-Encoding") && data.ResponseHeaders["Content-Encoding"].Contains("gzip"))
{
data.ResponseHeaders["Content-Length"] = GZip.Zip(responseString).Length.ToString();
@@ -581,6 +592,7 @@ namespace RuriLib
data.Log(new LogEntry("Response Source:", Colors.Green));
if (readResponseSource)
{
if (responseString == "") responseString = response.ToString(); // Read the stream if you didn't already read it
data.ResponseSource = responseString;
data.Log(new LogEntry(data.ResponseSource, Colors.GreenYellow));
}
@@ -592,9 +604,17 @@ namespace RuriLib
break;
case ResponseType.File:
var file = ReplaceValues(downloadPath, data);
using (var stream = File.Create(file)) { response.ToMemoryStream().CopyTo(stream); }
data.Log(new LogEntry("File saved as " + file, Colors.Green));
if (SaveAsScreenshot)
{
SaveScreenshot(response.ToMemoryStream(), data); // Read the stream
data.Log(new LogEntry("File saved as screenshot", Colors.Green));
}
else
{
var file = ReplaceValues(downloadPath, data);
using (var stream = File.Create(file)) { response.ToMemoryStream().CopyTo(stream); } // Read the stream
data.Log(new LogEntry("File saved as " + file, Colors.Green));
}
break;
default: