Added handelrs for networking exceptions Made Book selector a bit bigger Fixed issue with publishing date
140 lines
4.4 KiB
C#
140 lines
4.4 KiB
C#
using System.Globalization;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Bokhantarare;
|
|
|
|
public class Book
|
|
{
|
|
[JsonPropertyName("ISBN")]
|
|
public string ISBN { get; set; }
|
|
[JsonPropertyName("title")]
|
|
public string Title { get; set; }
|
|
[JsonPropertyName("authors")]
|
|
public string[] Authors { get; set; }
|
|
[JsonPropertyName("publicationYear")]
|
|
public string PubYear { get; set; }
|
|
|
|
public Book()
|
|
{
|
|
|
|
}
|
|
|
|
public Book(string ISBN, string title, string[] authors, string pubYear)
|
|
{
|
|
this.ISBN = ISBN;
|
|
this.Title = title;
|
|
this.Authors = authors;
|
|
this.PubYear = pubYear;
|
|
}
|
|
|
|
public Book(string ISBN, string title, string author, string pubYear) : this(ISBN, title, [author], pubYear)
|
|
{
|
|
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"Title: {Title}, Authors: {string.Join(", ", Authors)}, PubDate: {PubYear}";
|
|
}
|
|
|
|
public static Book? GetBookFromISBN(string ISBN)
|
|
{
|
|
ISBN = ISBN.Replace("-", "").Trim();
|
|
if (ISBN.Length != 10 && ISBN.Length != 13)
|
|
{
|
|
throw new ArgumentException("Invalid ISBN");
|
|
}
|
|
|
|
Dictionary<String, Object>? responce = JsonSerializer.Deserialize<Dictionary<String, Object>>(
|
|
Helper.Request($"https://openlibrary.org/api/books?bibkeys=ISBN:{ISBN}&jscmd=details&format=json") ?? throw new Exception("Network Error"));
|
|
|
|
if (responce == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
Dictionary<String, Object>? book = JsonSerializer.Deserialize<Dictionary<String, Object>>(responce[$"ISBN:{ISBN}"].ToString());
|
|
Dictionary<String, Object>? details = JsonSerializer.Deserialize<Dictionary<String, Object>>(book["details"].ToString());
|
|
|
|
string title = "";
|
|
if (details.ContainsKey("full_title"))
|
|
{
|
|
title = details["full_title"].ToString();
|
|
}
|
|
else if (details.ContainsKey("title"))
|
|
{
|
|
title = details["title"].ToString();
|
|
}
|
|
else
|
|
{
|
|
title = "Missing title";
|
|
}
|
|
List<string> authors = new List<string>();
|
|
if (details.ContainsKey("authors"))
|
|
{
|
|
foreach (Dictionary<string, object> listAuthors in JsonSerializer.Deserialize<Dictionary<String, Object>[]>(
|
|
details["authors"].ToString()))
|
|
{
|
|
authors.Add(listAuthors["name"].ToString());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
authors.Add("Missing Authors");
|
|
}
|
|
|
|
string pubDate = "";
|
|
if (details.ContainsKey("publish_date"))
|
|
{
|
|
pubDate = details["publish_date"].ToString();
|
|
|
|
if (!Regex.IsMatch(pubDate, @"^\d{4}$"))
|
|
{
|
|
if (DateTime.TryParse(pubDate, CultureInfo.InvariantCulture, DateTimeStyles.None,
|
|
out DateTime date))
|
|
{
|
|
pubDate = date.ToString("yyyy");
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
pubDate = "No Publication Date";
|
|
}
|
|
return new Book(ISBN, title, authors.ToArray(), pubDate);
|
|
}
|
|
|
|
public static BareBookInfo[]? GetBooksFromTitle(string title)
|
|
{
|
|
title = title.Trim();
|
|
|
|
Dictionary<String, Object>? responce = JsonSerializer.Deserialize<Dictionary<String, Object>>(
|
|
Helper.Request($"https://openlibrary.org/search.json?q={title}") ?? throw new Exception("Network Error"));
|
|
|
|
if (responce == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
List<Dictionary<String, Object>>? books = JsonSerializer.Deserialize<List<Dictionary<String, Object>>>(responce["docs"].ToString());
|
|
|
|
List<BareBookInfo> bookInfos = new List<BareBookInfo>();
|
|
|
|
foreach (Dictionary<String, Object> book in books)
|
|
{
|
|
if (book.TryGetValue("cover_edition_key", out var edition) && book.TryGetValue("title", out var value))
|
|
{
|
|
bookInfos.Add(new BareBookInfo(edition.ToString(), value.ToString()));
|
|
}
|
|
else
|
|
{
|
|
// This is a debug line to list search results that where missing the cover_edition_key
|
|
//Console.WriteLine(JsonSerializer.Serialize(book));
|
|
}
|
|
}
|
|
|
|
return bookInfos.ToArray();
|
|
}
|
|
} |