From 13c26eb3d80818de35a8dafab4aceb76030de752 Mon Sep 17 00:00:00 2001 From: Zacharias Date: Mon, 3 Feb 2025 14:58:49 +0100 Subject: [PATCH] Initial --- .gitignore | 5 + Bokhantarare.sln | 16 + Bokhantarare/App.xaml | 9 + Bokhantarare/App.xaml.cs | 13 + Bokhantarare/AssemblyInfo.cs | 10 + Bokhantarare/BareBookInfo.cs | 50 ++++ Bokhantarare/Bokhantarare.csproj | 11 + Bokhantarare/Book.cs | 129 ++++++++ Bokhantarare/BookSelectionPage.xaml | 19 ++ Bokhantarare/BookSelectionPage.xaml.cs | 27 ++ Bokhantarare/Helper.cs | 21 ++ Bokhantarare/MainWindow.xaml | 113 +++++++ Bokhantarare/MainWindow.xaml.cs | 395 +++++++++++++++++++++++++ 13 files changed, 818 insertions(+) create mode 100644 .gitignore create mode 100644 Bokhantarare.sln create mode 100644 Bokhantarare/App.xaml create mode 100644 Bokhantarare/App.xaml.cs create mode 100644 Bokhantarare/AssemblyInfo.cs create mode 100644 Bokhantarare/BareBookInfo.cs create mode 100644 Bokhantarare/Bokhantarare.csproj create mode 100644 Bokhantarare/Book.cs create mode 100644 Bokhantarare/BookSelectionPage.xaml create mode 100644 Bokhantarare/BookSelectionPage.xaml.cs create mode 100644 Bokhantarare/Helper.cs create mode 100644 Bokhantarare/MainWindow.xaml create mode 100644 Bokhantarare/MainWindow.xaml.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..add57be --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ \ No newline at end of file diff --git a/Bokhantarare.sln b/Bokhantarare.sln new file mode 100644 index 0000000..81f2998 --- /dev/null +++ b/Bokhantarare.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bokhantarare", "Bokhantarare\Bokhantarare.csproj", "{3FCD531B-F2FB-462E-981D-3150093064E3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3FCD531B-F2FB-462E-981D-3150093064E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3FCD531B-F2FB-462E-981D-3150093064E3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3FCD531B-F2FB-462E-981D-3150093064E3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3FCD531B-F2FB-462E-981D-3150093064E3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Bokhantarare/App.xaml b/Bokhantarare/App.xaml new file mode 100644 index 0000000..2a52116 --- /dev/null +++ b/Bokhantarare/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/Bokhantarare/App.xaml.cs b/Bokhantarare/App.xaml.cs new file mode 100644 index 0000000..090da55 --- /dev/null +++ b/Bokhantarare/App.xaml.cs @@ -0,0 +1,13 @@ +using System.Configuration; +using System.Data; +using System.Windows; + +namespace Bokhantarare; + +/// +/// Interaction logic for App.xaml +/// +public partial class App : Application +{ +} + diff --git a/Bokhantarare/AssemblyInfo.cs b/Bokhantarare/AssemblyInfo.cs new file mode 100644 index 0000000..cc29e7f --- /dev/null +++ b/Bokhantarare/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly:ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/Bokhantarare/BareBookInfo.cs b/Bokhantarare/BareBookInfo.cs new file mode 100644 index 0000000..81daade --- /dev/null +++ b/Bokhantarare/BareBookInfo.cs @@ -0,0 +1,50 @@ +using System.IO; +using System.Text.Json; +using System.Windows; + +namespace Bokhantarare; + +public class BareBookInfo(string editionId, string title, string? workId = null) +{ + string WorkID { get; set; } = workId??""; + string EditionID { get; set; } = editionId; + string Title { get; set; } = title; + + public Book getBook() + { + Dictionary? responce = JsonSerializer.Deserialize>( + Helper.Request($"https://openlibrary.org/books/{EditionID}.json")); + + if (responce == null) + { + throw new Exception("Failed to get book info"); + } + + object? isbn_13_raw_array = 0; + if (responce.ContainsKey("isbn_13")) + { + isbn_13_raw_array = responce["isbn_13"]; + } + else if(responce.ContainsKey("isbn_10")) + { + isbn_13_raw_array = responce["isbn_10"]; + } + else + { + throw new Exception("Failed to get ISBN"); + } + if (isbn_13_raw_array == null) + { + throw new Exception("Failed to get book info"); + } + + List isbn_13_array = JsonSerializer.Deserialize>(isbn_13_raw_array.ToString()); + + return Book.GetBookFromISBN(isbn_13_array[0])??throw new Exception("Failed to get book info"); + } + + public override string ToString() + { + return Title; + } +} \ No newline at end of file diff --git a/Bokhantarare/Bokhantarare.csproj b/Bokhantarare/Bokhantarare.csproj new file mode 100644 index 0000000..c4a3262 --- /dev/null +++ b/Bokhantarare/Bokhantarare.csproj @@ -0,0 +1,11 @@ + + + + WinExe + net8.0-windows + enable + enable + true + + + diff --git a/Bokhantarare/Book.cs b/Bokhantarare/Book.cs new file mode 100644 index 0000000..2c9b616 --- /dev/null +++ b/Bokhantarare/Book.cs @@ -0,0 +1,129 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +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? responce = JsonSerializer.Deserialize>( + Helper.Request($"https://openlibrary.org/api/books?bibkeys=ISBN:{ISBN}&jscmd=details&format=json")); + + if (responce == null) + { + return null; + } + + Dictionary? book = JsonSerializer.Deserialize>(responce[$"ISBN:{ISBN}"].ToString()); + Dictionary? details = JsonSerializer.Deserialize>(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 authors = new List(); + if (details.ContainsKey("authors")) + { + foreach (Dictionary listAuthors in JsonSerializer.Deserialize[]>( + details["authors"].ToString())) + { + authors.Add(listAuthors["name"].ToString()); + } + } + else + { + authors.Add("Missing Authors"); + } + + string pubDate = ""; + if (details.ContainsKey("publish_date")) + { + details["publish_date"].ToString(); + } + else + { + pubDate = "No Publication Date"; + } + return new Book(ISBN, title, authors.ToArray(), pubDate); + } + + public static BareBookInfo[]? GetBooksFromTitle(string title) + { + title = title.Trim(); + + Dictionary? responce = JsonSerializer.Deserialize>( + Helper.Request($"https://openlibrary.org/search.json?q={title}")); + + if (responce == null) + { + return null; + } + + List>? books = JsonSerializer.Deserialize>>(responce["docs"].ToString()); + + List bookInfos = new List(); + + foreach (Dictionary 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(); + } +} \ No newline at end of file diff --git a/Bokhantarare/BookSelectionPage.xaml b/Bokhantarare/BookSelectionPage.xaml new file mode 100644 index 0000000..91b0b5c --- /dev/null +++ b/Bokhantarare/BookSelectionPage.xaml @@ -0,0 +1,19 @@ + + + + + + + + + + + + diff --git a/Bokhantarare/BookSelectionPage.xaml.cs b/Bokhantarare/BookSelectionPage.xaml.cs new file mode 100644 index 0000000..887614f --- /dev/null +++ b/Bokhantarare/BookSelectionPage.xaml.cs @@ -0,0 +1,27 @@ +using System.Windows; + +namespace Bokhantarare; + +public partial class BookSelectionPage : Window +{ + public BookSelectionPage() + { + InitializeComponent(); + } + + public BookSelectionPage(BareBookInfo[] books) : this() + { + BookSelector.ItemsSource = books; + } + + public BareBookInfo getSelectedBook() + { + return BookSelector.SelectedItem as BareBookInfo; + } + + private void ButtonBase_OnClick(object sender, RoutedEventArgs e) + { + this.DialogResult = true; // Close the dialog with 'OK' result. + this.Close(); + } +} \ No newline at end of file diff --git a/Bokhantarare/Helper.cs b/Bokhantarare/Helper.cs new file mode 100644 index 0000000..85e909a --- /dev/null +++ b/Bokhantarare/Helper.cs @@ -0,0 +1,21 @@ +using System.Net.Http; +using System.Net.Http.Headers; + +namespace Bokhantarare; + +public class Helper +{ + public static string Request(string requestUrl) + { + HttpClient client = new HttpClient(); + HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, requestUrl); + requestMessage.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); + + //Connect to the URL + HttpResponseMessage response = client.SendAsync(requestMessage).Result; + + // Get the response + string Output = response.Content.ReadAsStringAsync().Result; + return Output; + } +} \ No newline at end of file diff --git a/Bokhantarare/MainWindow.xaml b/Bokhantarare/MainWindow.xaml new file mode 100644 index 0000000..6e0bc76 --- /dev/null +++ b/Bokhantarare/MainWindow.xaml @@ -0,0 +1,113 @@ + + + + + + + + + + + + + + + + + + + + + + + +