feat: 初回コミット (MomongaDownloader)

This commit is contained in:
armonica committed 2026-10-01 09:42:29 +09:00
commit fdf0c890bb
7 files changed
+451

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
## Visual Studio 2026 / .NET .gitignore
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ogs/
.vs/
*.user
*.suo
*.userosscache
*.sln.docstates
+3
View File
@@ -0,0 +1,3 @@
<Solution>
<Project Path="MomongaDownloader/MomongaDownloader.csproj" />
</Solution>
+72
View File
@@ -0,0 +1,72 @@
namespace MomongaDownloader
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
txtURL = new TextBox();
btnDownload = new Button();
SuspendLayout();
//
// txtURL
//
txtURL.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
txtURL.Location = new Point(12, 12);
txtURL.Name = "txtURL";
txtURL.Size = new Size(695, 23);
txtURL.TabIndex = 0;
//
// btnDownload
//
btnDownload.Anchor = AnchorStyles.Top | AnchorStyles.Right;
btnDownload.Location = new Point(713, 12);
btnDownload.Name = "btnDownload";
btnDownload.Size = new Size(75, 23);
btnDownload.TabIndex = 1;
btnDownload.Text = "実行";
btnDownload.UseVisualStyleBackColor = true;
btnDownload.Click += btnDownload_Click;
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(btnDownload);
Controls.Add(txtURL);
Name = "Form1";
Text = "Form1";
ResumeLayout(false);
PerformLayout();
}
#endregion
private TextBox txtURL;
private Button btnDownload;
}
}
+213
View File
@@ -0,0 +1,213 @@
using AngleSharp;
using System.IO.Compression;
using System.ServiceModel.Syndication;
using System.Xml;
namespace MomongaDownloader
{
public partial class Form1 : Form
{
// HttpClientはアプリケーション全体で使い回すため、static readonlyで定義します
private static readonly HttpClient _httpClient = new HttpClient();
public Form1()
{
InitializeComponent();
txtURL.Text = "";
// サーバー側でボットとして弾かれないよう、一般的なブラウザのユーザーエージェントを設定します
_httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36");
}
private async void btnDownload_Click(object sender, EventArgs e)
{
var pageMaster = "https://momon-ga.com/fanzine";
var linkList = new List<string>();
for (int pageId = 1; pageId <= 50; pageId++)
{
var pageURL = $"{pageMaster}/feed/atom/{(pageId > 1 ? $"?paged={pageId}" : "")}";
linkList.Add(pageURL);
}
var tasks = linkList.Select(url => GetFeedItems(_httpClient, url)).ToList();
var results = await Task.WhenAll(tasks);
var allItems = results.SelectMany(items => items).ToList();
var urlList = new List<string>();
foreach (var originalItem in allItems)
{
var link = originalItem.Links.FirstOrDefault()?.Uri;
if (link != null)
{
urlList.Add(link.ToString());
}
}
foreach (var urlString in urlList)
{
this.DownloadZip(urlString);
}
}
private async void DownloadZip(string urlString)
{
try
{
// URLの形式が正しいかチェック
if (string.IsNullOrWhiteSpace(urlString) || !Uri.TryCreate(urlString, UriKind.Absolute, out Uri baseUri))
{
// 有効なURLを入力してください。
return;
}
try
{
// UI操作を一時無効化(連打防止)
btnDownload.Enabled = false;
// 1. AngleSharpを使ってHTMLドキュメントを取得・解析
var config = Configuration.Default.WithDefaultLoader();
using var context = BrowsingContext.New(config);
using var document = await context.OpenAsync(urlString);
// 2. h1タグの値を取得してファイル名を生成し、使用不可文字をサニタイズ
var h1Element = document.QuerySelector("h1");
string title = h1Element != null ? h1Element.TextContent.Trim() : "download";
string safeFileName = SanitizeFileName(title);
if (string.IsNullOrEmpty(safeFileName))
{
safeFileName = "download";
}
// 3. 対象のdivタグ以下のimgタグからURLを抽出
var imgElements = document.QuerySelectorAll("#post-hentai img");
var imageUrls = new List<Uri>();
foreach (var img in imgElements)
{
string src = img.GetAttribute("src");
if (!string.IsNullOrEmpty(src))
{
// 相対URLの場合も考慮して、絶対URLに解決する
if (Uri.TryCreate(baseUri, src, out Uri absoluteUri))
{
imageUrls.Add(absoluteUri);
}
}
}
if (imageUrls.Count == 0)
{
// 指定された条件の画像が見つかりませんでした。
return;
}
// 4. SemaphoreSlimを用いて同時接続数を制限し、画像を並列ダウンロード
int maxDegreeOfParallelism = 5; // 同時ダウンロード数の上限(適宜調整)
using var semaphore = new SemaphoreSlim(maxDegreeOfParallelism);
// 抽出した順番通りにタスクを作成
var downloadTasks = imageUrls.Select(async uri =>
{
await semaphore.WaitAsync();
try
{
return await _httpClient.GetByteArrayAsync(uri);
}
catch (Exception)
{
// ダウンロード失敗時はnullを返すなどのエラーハンドリング
return null;
}
finally
{
semaphore.Release();
}
}).ToList();
// 全タスクを待機
byte[][] downloadedImages = await Task.WhenAll(downloadTasks);
// 5. exeのディレクトリを取得し、ZIPファイルへの絶対パスを構築
string exeDir = AppContext.BaseDirectory;
string zipFilePath = Path.Combine(exeDir, $"{safeFileName}.zip");
// 6. ZipArchiveを用いて圧縮と保存
using (var fileStream = new FileStream(zipFilePath, FileMode.Create))
using (var archive = new ZipArchive(fileStream, ZipArchiveMode.Create, false))
{
for (int i = 0; i < downloadedImages.Length; i++)
{
var imgData = downloadedImages[i];
if (imgData == null) continue; // ダウンロードに失敗した画像はスキップ
// 拡張子を取得(不明な場合は.jpgをデフォルトに)
string extension = Path.GetExtension(imageUrls[i].AbsolutePath);
if (string.IsNullOrEmpty(extension))
{
extension = ".jpg";
}
// 元のHTMLの順序通りに連番でファイル名を付ける
string entryName = $"image_{i + 1:D3}{extension}";
var zipEntry = archive.CreateEntry(entryName);
using (var entryStream = zipEntry.Open())
using (var ms = new MemoryStream(imgData))
{
await ms.CopyToAsync(entryStream);
}
}
}
//MessageBox.Show($"ダウンロードとZIP圧縮が完了しました。\n保存先: {zipFilePath}");
}
catch (Exception ex)
{
// $"処理中にエラーが発生しました: {ex.Message}"
}
finally
{
// UI操作を再度有効化
btnDownload.Enabled = true;
}
}
catch (Exception)
{
}
finally
{
}
return;
}
// ファイル名として使用できない文字をアンダースコアに置換するメソッド
private string SanitizeFileName(string fileName)
{
var invalidChars = Path.GetInvalidFileNameChars();
return string.Join("_", fileName.Split(invalidChars, StringSplitOptions.RemoveEmptyEntries));
}
public static async Task<IEnumerable<SyndicationItem>> GetFeedItems(HttpClient client, string url)
{
try
{
using var stream = await client.GetStreamAsync(url);
using var reader = XmlReader.Create(stream);
var feed = SyndicationFeed.Load(reader);
return feed?.Items ?? Enumerable.Empty<SyndicationItem>();
}
catch (Exception ex)
{
return Enumerable.Empty<SyndicationItem>();
}
}
}
}
+120
View File
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net10.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AngleSharp" Version="1.7.1" />
<PackageReference Include="System.ServiceModel.Syndication" Version="10.0.11" />
</ItemGroup>
</Project>
+17
View File
@@ -0,0 +1,17 @@
namespace MomongaDownloader
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
}