wldeh / bible-api

Free Unlimited Bible API with support for 200+ versions and languages.⚡
145 stars 22 forks source link

Added bible books json file that enumerates the books available in a translation #13

Open Lucasharskamp opened 5 days ago

Lucasharskamp commented 5 days ago

Using a C# console app I wrote just for this, I was able to create simple JSON files for each translation; a list of books, each of which is a combination of the name (string) and amount of chapters (integer). These books are listed in no particular order, or way of knowing which bible book it is, as the bible books in the JSON files don't have indexes to compare between translations. So for example, I can set Genesis as "1" since most translations don't have Genesis to begin with and there is no way of knowing which Bible book is which between translations.

The app works as follows: just start it up with the first parameter being the folder where the bibles are stored, so for example folder "C:/{path_to_git_repo}/bible-api/bibles". It will make JSON files for all Bibles, completes within 25 seconds. In the event a bible translation doesn't contain any books (which happens for some reason) an empty array is the output in the JSON file.

using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;

namespace BookJsonFilesGenerator
{
    internal class Program
    {
        static int Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Arg 1 needed: directory to bible files.");
                return 1;
            }

            if (!Directory.Exists(args[0]))
            {
                Console.WriteLine("Directory in parameter does not exist.");
            }

            var jsonSettings = new JsonSerializerOptions()
            {
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
            };

            var directory = Directory.GetDirectories(args[0]);
            foreach(var bibleTranslation in directory)
            {
                Console.WriteLine("Handling: " + bibleTranslation);
                var booksDirectory = Path.Combine(bibleTranslation, "books");
                string output;
                if (!Directory.Exists(booksDirectory))
                {
                    output = "[]";
                }
                else
                {
                    var paths = Directory.GetDirectories(booksDirectory);
                    var books = new List<Book>();
                    foreach (var path in paths)
                    {
                        var book = Path.GetFileName(path);
                        var chapterCount = Directory.GetDirectories(Path.Combine(path, "chapters")).Length;
                        books.Add(new Book()
                        {
                            Name = book,
                            Chapters = chapterCount,
                        });
                    }
                    var result = new BibleVersionBooks()
                    {
                        Books = books,
                    };
                    output = JsonSerializer.Serialize(result, jsonSettings);
                }
                var outputPath = Path.Combine(bibleTranslation, "books.json");
                File.WriteAllText(outputPath, output);

                Console.WriteLine("Output written to: " + outputPath);
            }

            return 0;
        }
    }

    public class BibleVersionBooks
    {
        [JsonPropertyName("books")]
        public List<Book> Books { get; set; } = default!;
    }

    public class Book
    {
        [JsonPropertyName("name")]
        public string Name { get; set; } = default!;

        [JsonPropertyName("chapters")]
        public int Chapters { get; set; }
    }
}