-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.cs
More file actions
175 lines (169 loc) · 8.57 KB
/
Program.cs
File metadata and controls
175 lines (169 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection; // Added for Assembly.GetExecutingAssembly().GetName().Version
namespace NovelpiaDownloader
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length > 0)
{
// Headless mode for command-line operations
string novelId = null;
int? fromChapter = null;
int? toChapter = null;
string outputPath = null;
bool batchMode = false;
string listFilePath = null;
string outputDirectory = null;
bool autoStart = false;
bool saveAsEpub = false; // New: Command-line argument for EPUB
bool saveAsHtml = false; // New: Command-line argument for HTML
bool enableImageCompression = false; // New: Command-line argument for image compression
int jpegQuality = 80; // New: Command-line argument for JPEG quality, default to 80
for (int i = 0; i < args.Length; i++)
{
switch (args[i].ToLower())
{
case "-novelid":
case "--novelid":
if (i + 1 < args.Length) novelId = args[++i];
break;
case "-from":
case "--from":
if (i + 1 < args.Length && int.TryParse(args[++i], out int fromVal)) fromChapter = fromVal;
break;
case "-to":
case "--to":
if (i + 1 < args.Length && int.TryParse(args[++i], out int toVal)) toChapter = toVal;
break;
case "-output":
case "--output":
if (i + 1 < args.Length) outputPath = args[++i];
break;
case "-batch":
case "--batch":
batchMode = true;
break;
case "-listfile":
case "--listfile":
if (i + 1 < args.Length) listFilePath = args[++i];
break;
case "-outputdir":
case "--outputdir":
if (i + 1 < args.Length) outputDirectory = args[++i];
break;
case "-autostart":
case "--autostart":
autoStart = true; // Auto-start implies headless if other parameters are present
break;
case "-epub":
case "--epub":
saveAsEpub = true;
break;
case "-html":
case "--html":
saveAsHtml = true;
break;
case "-compressimages":
case "--compressimages":
enableImageCompression = true;
break;
case "-jpegquality":
case "--jpegquality":
if (i + 1 < args.Length && int.TryParse(args[++i], out int qualityVal))
{
jpegQuality = Math.Max(0, Math.Min(100, qualityVal)); // Clamp between 0 and 100
}
break;
case "-h":
case "--help":
ShowHelp();
return;
case "-v":
case "--version":
Console.WriteLine($"NovelpiaDownloader Version: {Assembly.GetExecutingAssembly().GetName().Version}");
return;
}
}
MainWin novelpiaDownloader = new MainWin(); // Instantiate the main form, passing args to its constructor
if (batchMode && !string.IsNullOrEmpty(listFilePath) && !string.IsNullOrEmpty(outputDirectory))
{
Console.WriteLine("Starting batch download in headless mode...");
novelpiaDownloader.BatchDownloadCore(
listFilePath,
outputDirectory,
saveAsEpub, // Pass EPUB setting
saveAsHtml, // Pass HTML setting
enableImageCompression, // Pass compression setting
jpegQuality, // Pass quality setting
true // isHeadless = true
);
}
else if (!string.IsNullOrEmpty(novelId) && !string.IsNullOrEmpty(outputPath))
{
Console.WriteLine("Starting single novel download in headless mode...");
novelpiaDownloader.DownloadCore(
novelId,
saveAsEpub, // Pass EPUB setting
saveAsHtml, // Pass HTML setting
outputPath,
fromChapter,
toChapter,
enableImageCompression, // Pass compression setting
jpegQuality, // Pass quality setting
true // isHeadless = true
);
}
else if (!autoStart) // If no valid headless command, show help unless auto-start implies UI
{
ShowHelp();
}
else
{
// If autoStart is true but no specific download commands, it might mean just launch UI
Application.Run(novelpiaDownloader);
}
}
else
{
// No arguments, run in UI mode
Application.Run(new MainWin());
}
}
static void ShowHelp()
{
Console.WriteLine("NovelpiaDownloader Command Line Arguments:");
Console.WriteLine(" -novelid <ID> : Specify a single novel ID to download.");
Console.WriteLine(" -from <chapter_num> : Start download from this chapter (1-indexed).");
Console.WriteLine(" -to <chapter_num> : End download at this chapter (1-indexed).");
Console.WriteLine(" -output <path> : Output file path for single novel download (e.g., C:\\novel.txt).");
Console.WriteLine(" -epub : Save as EPUB format.");
Console.WriteLine(" -html : Save as standalone HTML format (overrides -txt, if -epub is not set).");
Console.WriteLine(" -compressimages : Enable image compression for EPUB/HTML output.");
Console.WriteLine(" -jpegquality <0-100>: Set JPEG quality for image compression (default: 80).");
Console.WriteLine("");
Console.WriteLine(" -batch : Enable batch download mode.");
Console.WriteLine(" -listfile <path> : Path to a text file with novel_title,novel_id per line for batch download.");
Console.WriteLine(" -outputdir <dir> : Output directory for batch downloads.");
Console.WriteLine("");
Console.WriteLine(" -autostart : Launch UI automatically if no valid headless commands.");
Console.WriteLine(" -h, --help : Show this help message.");
Console.WriteLine(" -v, --version : Show application version.");
Console.WriteLine("\nExamples:");
Console.WriteLine(" NovelpiaDownloader.exe -novelid 12345 -output \"C:\\Novels\\MyNovel.epub\" -from 1 -to 10 -epub -compressimages -jpegquality 75");
Console.WriteLine(" NovelpiaDownloader.exe -batch -listfile \"C:\\MyList.txt\" -outputdir \"C:\\BatchOutput\" -html -compressimages");
Console.WriteLine(" NovelpiaDownloader.exe -novelid 67890 -output \"C:\\Novels\\PlainNovel.txt\"");
}
}
}