From e79b3bc3e1d1e2f1e53f2df3b30f5e4d4becc870 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Fri, 30 Sep 2022 15:44:47 -0300 Subject: [PATCH] bin/json: add bin/json/insert-chapter-metadata --- bin/json/insert-chapter-metadata | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 bin/json/insert-chapter-metadata diff --git a/bin/json/insert-chapter-metadata b/bin/json/insert-chapter-metadata new file mode 100755 index 0000000..93e6902 --- /dev/null +++ b/bin/json/insert-chapter-metadata @@ -0,0 +1,51 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +## +# This script inserts metadata about each chapter in +# The Qur'an. The metadata is inserted into the JSON +# for each chapter (eg src/json/en/1.json, src/json/en/2.json, +# src/json/en/3.json, ...). +# +# The metadata is an object that's inserted as the first element +# in an array that otherwise contains the contents of a chapter. + +## +# Set process name - primarily for the "ps" command +Process.setproctitle("quran-pull (insert-chapter-metadata)") + +## +# Dependencies +require "bundler/setup" +require "json" +require "paint" +require_relative "../../binlib/io/line" + +## +# Utils +def parse_json(path)= JSON.parse(File.read(path)) + +## +# Configuration variables +lc_glob = ARGV[0] || "*" +base_dir = File.join("src", "json") +chapters = parse_json(File.join(base_dir, "chapter-metadata.json")) +dirs = Dir.glob(File.join(base_dir, lc_glob)).select { File.directory?(_1) } +line = IO::Line.new($stdout) + +print "\n", " " * 3, Paint["Add metadata", :bold], "\n\n" +print "Locale", " " * 3 +print "Location", "\n" +dirs.each do |dir| + 1.upto(114) do |number| + ch = chapters[number - 1] + lc = File.basename(dir) + fp = File.join(dir, "#{number}.json") + line.rewind.print File.basename(dir), " " * 7, File.join(lc, "#{number}.json").center(9) + contents = parse_json(fp) + contents.unshift(ch) + File.write(fp, JSON.pretty_generate(contents)) + sleep 0.01 + end + puts +end