-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
396 lines (341 loc) · 17.5 KB
/
Copy pathProgram.cs
File metadata and controls
396 lines (341 loc) · 17.5 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
using System.Text.RegularExpressions;
using System.Text.Json;
using System.Diagnostics;
using Microsoft.Extensions.Configuration;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Books.v1;
using Google.Apis.Services;
using Google.Apis.Util.Store;
namespace LibrisScan.Crawler
{
/// <summary>
/// LibrisScan: A utility to automatically fetch book metadata and covers
/// from Google Books API based on local eBook filenames.
/// </summary>
class Program
{
#region Configuration & State
// Global paths derived from the application execution folder
private static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
private static readonly string CredentialsPath = Path.Combine(BaseDir, "GoogleApiAuth", "google-credentials.json");
private static readonly string LogFilePath = Path.Combine(BaseDir, "Logs", "processed_files.log");
private static List<string> SearchFilters = new List<string>();
// Runtime directories loaded from appsettings.json
private static string JsonDir = string.Empty;
private static string CoversDir = string.Empty;
private static string EbookSourceDir = string.Empty;
// Shared state and statistics
private static readonly HttpClient httpClient = new HttpClient();
private static readonly HashSet<string> ProcessedFiles = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
private static readonly Stopwatch stopWatch = new Stopwatch();
private static bool isQuotaExhausted = false;
private static int countTotal, countLogSkipped, countIsbnSkipped, countSaved;
#endregion
static async Task Main(string[] args)
{
try
{
// Initialize folders, logs, and configuration
SetupEnvironment();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("==================================================");
Console.WriteLine(" LibrisScan: Metadata Crawler Active ");
Console.WriteLine("==================================================");
Console.ResetColor();
// Authentication is required for the BooksService to function
if (!File.Exists(CredentialsPath))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[FATAL] Google Credentials not found at: {CredentialsPath}");
Console.ResetColor();
return;
}
var service = await AuthenticateService();
// Recursively gather all PDF and EPUB files
var files = Directory.GetFiles(EbookSourceDir, "*.*", SearchOption.AllDirectories)
.Where(f => f.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase) ||
f.EndsWith(".epub", StringComparison.OrdinalIgnoreCase)).ToList();
countTotal = files.Count;
int currentIdx = 0;
foreach (var filePath in files)
{
currentIdx++;
UpdateTitle(currentIdx);
if (isQuotaExhausted)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("\n[!] Quota exhausted. Stopping scan...");
Console.ResetColor();
break;
}
// Skip files already successfully logged
if (ProcessedFiles.Contains(filePath))
{
countLogSkipped++;
// Optional: Print small indicator for skipped files to show activity
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine($"[{currentIdx}/{countTotal}] Skipping: {Path.GetFileName(filePath)} (Already Processed)");
Console.ResetColor();
continue;
}
// Highlight the file currently being searched
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($"\n[{currentIdx}/{countTotal}] Searching: {Path.GetFileNameWithoutExtension(filePath)}");
Console.ResetColor();
// Step 1: Search for the book by its filename
var isbns = await GetIsbnsFromSearch(service, Path.GetFileNameWithoutExtension(filePath));
if (isbns.Count == 0)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($" [?] No ISBNs found for this title.");
Console.ResetColor();
}
else
{
// Step 2: For every found ISBN, try to download metadata and covers
foreach (var isbn in isbns)
{
if (isQuotaExhausted) break;
Console.Write($" [→] Processing ISBN: {isbn}... ");
// Check if we already have this metadata on disk
string jsonFileName = $"{isbn}.json";
string jsonPath = Path.Combine(JsonDir, jsonFileName); // Ensure JsonDir is accessible here
if (File.Exists(jsonPath))
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine("Exist");
Console.ResetColor();
continue; // Move to the next ISBN without calling the API
}
// If it doesn't exist, try to download it
if (await ProcessIsbn(isbn))
{
countSaved++;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Saved!");
Console.ResetColor();
}
else
{
// If ProcessIsbn returns false, it was either a network error or quota hit
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error (API Fail or Quota)");
Console.ResetColor();
}
await Task.Delay(2500); // Respectful delay
}
}
// Log this file as 'handled'
SaveToLog(filePath);
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"\n[FATAL ERROR] {ex.Message}");
Console.ResetColor();
}
Finish();
}
#region Core Logic Methods
/// <summary>
/// Cleans the filename and queries Google Books to find valid ISBNs.
/// </summary>
private static async Task<List<string>> GetIsbnsFromSearch(BooksService service, string fileName)
{
string safeFileName = fileName ?? string.Empty;
// Build the regex pattern dynamically from the external SearchFilters list.
// Use an empty pattern "$^" (which matches nothing) if no filters are provided.
string pattern = (SearchFilters != null && SearchFilters.Any())
? string.Join("|", SearchFilters)
: "$^";
// Apply the dynamically built regex to clean the filename for the API search
var cleanQuery = Regex.Replace(safeFileName, pattern, "", RegexOptions.IgnoreCase).Trim();
var list = new List<string>();
try
{
var request = service.Volumes.List(cleanQuery);
request.MaxResults = 10;
var results = await request.ExecuteAsync();
if (results.Items != null)
{
foreach (var item in results.Items)
{
var identifiers = item.VolumeInfo?.IndustryIdentifiers;
if (identifiers == null) continue;
foreach (var id in identifiers)
{
if (string.IsNullOrEmpty(id.Identifier)) continue;
// Normalize all results to ISBN_13 for consistency
if (id.Type == "ISBN_13") list.Add(id.Identifier);
else if (id.Type == "ISBN_10") list.Add(ConvertIsbn10To13(id.Identifier));
}
}
}
}
catch (Google.GoogleApiException ex) when ((int)ex.HttpStatusCode == 429 || (int)ex.HttpStatusCode == 403)
{
isQuotaExhausted = true; // Mark quota as full to stop further requests
}
catch (Exception ex) { Console.WriteLine($" [SEARCH ERROR] {ex.Message}"); }
return list.Distinct().ToList();
}
/// <summary>
/// Fetches full JSON metadata and the high-res thumbnail for a specific ISBN.
/// </summary>
private static async Task<bool> ProcessIsbn(string isbn)
{
string path = Path.Combine(JsonDir, $"{isbn}.json");
if (File.Exists(path)) { countIsbnSkipped++; return false; }
try
{
var resp = await httpClient.GetAsync($"https://www.googleapis.com/books/v1/volumes?q=isbn:{isbn}");
if (!resp.IsSuccessStatusCode)
{
if ((int)resp.StatusCode == 429 || (int)resp.StatusCode == 403) isQuotaExhausted = true;
return false;
}
string body = await resp.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(body);
if (doc.RootElement.TryGetProperty("items", out var items) && items.GetArrayLength() > 0)
{
// Persist the full raw JSON for future local parsing
File.WriteAllText(path, body);
var info = items[0].GetProperty("volumeInfo");
if (info.TryGetProperty("imageLinks", out var links) && links.TryGetProperty("thumbnail", out var thumbProp))
{
var thumb = thumbProp.GetString();
if (!string.IsNullOrEmpty(thumb))
{
// Upgrade to HTTPS to ensure successful download
thumb = thumb.Replace("http://", "https://");
var imgBytes = await httpClient.GetByteArrayAsync(thumb);
File.WriteAllBytes(Path.Combine(CoversDir, $"{isbn}.jpg"), imgBytes);
}
}
//Console.ForegroundColor = ConsoleColor.Green;
//Console.WriteLine($" [✔] Saved: {isbn}");
//Console.ResetColor();
return true;
}
}
catch { } // Silently skip failures for individual ISBNs to keep the batch moving
return false;
}
#endregion
#region Infrastructure & Helpers
/// <summary>
/// Updates the Console Title bar with a real-time progress percentage.
/// </summary>
private static void UpdateTitle(int current) =>
Console.Title = $"({(double)current / countTotal:P0}) LibrisScan | {current}/{countTotal} | Saved: {countSaved}";
/// <summary>
/// Handles directory creation, config generation, and loading the processed files log.
/// </summary>
private static void SetupEnvironment()
{
var logDir = Path.GetDirectoryName(LogFilePath);
if (!string.IsNullOrEmpty(logDir)) Directory.CreateDirectory(logDir);
string configPath = Path.Combine(BaseDir, "appsettings.json");
if (!File.Exists(configPath))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("[CONFIG] Creating default appsettings.json using BaseDirectory...");
string safeBaseDir = BaseDir.Replace("\\", "\\\\");
// Note: The margin of the raw string is defined by the leftmost character of the closing triple quotes below
string defaultConfig = $$"""
{
"StorageSettings": {
"JsonMetadataDir": "{{safeBaseDir}}GoogleBooks\\Metadata",
"CoversDir": "{{safeBaseDir}}GoogleBooks\\Covers",
"EbookSourceDir": "{{safeBaseDir}}E-Books Collections"
},
"SearchFilters": [
"\\(Z-Library\\)",
"-- Anna['’]s Archive",
"- libgen\\.li",
"\\(z-library\\.sk, 1lib\\.sk, z-lib\\.sk\\)"
]
}
""";
File.WriteAllText(configPath, defaultConfig);
Console.ResetColor();
}
var config = new ConfigurationBuilder()
.SetBasePath(BaseDir)
.AddJsonFile("appsettings.json", optional: false)
.Build();
JsonDir = config["StorageSettings:JsonMetadataDir"] ?? Path.Combine(BaseDir, "Metadata");
CoversDir = config["StorageSettings:CoversDir"] ?? Path.Combine(BaseDir, "Covers");
EbookSourceDir = config["StorageSettings:EbookSourceDir"] ?? Path.Combine(BaseDir, "Ebooks");
SearchFilters = config.GetSection("SearchFilters").Get<List<string>>() ?? new List<string>();
Directory.CreateDirectory(JsonDir);
Directory.CreateDirectory(CoversDir);
if (File.Exists(LogFilePath))
{
foreach (var line in File.ReadAllLines(LogFilePath))
{
if (!string.IsNullOrWhiteSpace(line)) ProcessedFiles.Add(line.Trim());
}
}
stopWatch.Start();
}
private static void SaveToLog(string path) => File.AppendAllLines(LogFilePath, new[] { path });
/// <summary>
/// Clears the console and provides a final summary report of the session.
/// </summary>
private static void Finish()
{
stopWatch.Stop();
Console.Clear();
Console.Title = isQuotaExhausted ? "LibrisScan - STOPPED (Quota)" : "LibrisScan - COMPLETED";
Console.WriteLine("=============================================");
Console.WriteLine(" LIBRISSCAN FINAL SUMMARY ");
if (isQuotaExhausted)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(" *** DAILY GOOGLE QUOTA EXHAUSTED *** ");
Console.ResetColor();
}
Console.WriteLine("=============================================");
Console.WriteLine($"Time Elapsed: {stopWatch.Elapsed:hh\\:mm\\:ss}");
Console.WriteLine($"Total Files Found: {countTotal}");
Console.WriteLine($"Skipped (By Log): {countLogSkipped}");
Console.WriteLine($"Newly Downloaded: {countSaved}");
Console.WriteLine("=============================================");
Console.WriteLine("\nDone. Press any key to exit.");
Console.ReadKey();
}
/// <summary>
/// Standard algorithm to convert legacy 10-digit ISBNs to the modern 13-digit format.
/// </summary>
private static string ConvertIsbn10To13(string isbn10)
{
if (isbn10.Length != 10) return isbn10;
string t = "978" + isbn10.Substring(0, 9);
int s = 0;
for (int i = 0; i < t.Length; i++)
s += (t[i] - '0') * (i % 2 == 0 ? 1 : 3);
return t + ((10 - (s % 10)) % 10);
}
/// <summary>
/// Initializes the OAuth2 or API Key flow for Google Services.
/// </summary>
private static async Task<BooksService> AuthenticateService()
{
using var stream = new FileStream(CredentialsPath, FileMode.Open, FileAccess.Read);
var cred = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.FromStream(stream).Secrets,
new[] { BooksService.Scope.Books },
"user",
CancellationToken.None,
new FileDataStore(Path.Combine(BaseDir, "GoogleApiAuth", "token_store"), true));
return new BooksService(new BaseClientService.Initializer
{
HttpClientInitializer = cred,
ApplicationName = "LibrisScan"
});
}
#endregion
}
}