Skip to content

Commit

Permalink
embed images in html
Browse files Browse the repository at this point in the history
  • Loading branch information
setsumi committed May 28, 2023
1 parent 859c768 commit c3ab14a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
Binary file added src/syosetuDownloader/image-error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/syosetuDownloader/syosetuDownloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@
<Content Include="ChapterStyle.css">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="image-error.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
Expand Down
31 changes: 29 additions & 2 deletions src/syosetuDownloaderCore/Syousetsu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,34 @@ public static string GetNovelBody(HtmlDocument doc, Constants details)
{
if (!src.StartsWith("http"))
{
img.SetAttributeValue("src", "https:" + src);
src = "https:" + src;
img.SetAttributeValue("src", src);
}

// try to download and embed image
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(src);
request.Method = "GET";
request.CookieContainer = details.SyousetsuCookie;
request.UserAgent = details.UserAgent;

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (MemoryStream ms = new MemoryStream())
{
stream.CopyTo(ms, (int)response.ContentLength);
string base64String = Convert.ToBase64String(ms.ToArray());
string imageSrc = string.Format("data:image/png;base64,{0}", base64String);
img.SetAttributeValue("src", imageSrc);
}
}
catch (Exception)
{
byte[] fileBytes = File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), "image-error.png"));
string base64String = Convert.ToBase64String(fileBytes);
string imageSrc = string.Format("data:image/png;base64,{0}", base64String);
img.SetAttributeValue("src", imageSrc);
}
}
}
Expand Down Expand Up @@ -345,7 +372,7 @@ public static string GetNovelHeader(HtmlDocument doc, Constants details)
headerNode = doc.DocumentNode.SelectSingleNode("//div[@id='novel_p']");
}
else // kakuyomu
{
{
titleNode = doc.DocumentNode.SelectSingleNode("//p[@class='chapterTitle']/span");
headerNode = null;
}
Expand Down

0 comments on commit c3ab14a

Please sign in to comment.