From 42c3b827126cc4f1ba0e67a9dec6d2b1581b0f6c Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Mon, 13 Feb 2023 04:17:44 -0300 Subject: [PATCH] Add -u, --update switch to "pull" command --- README.md | 1 + lib/quran-json/command.rb | 16 +++++++++----- lib/quran-json/pull.rb | 12 ++++++++--- libexec/quran-json/quran.com | 21 ++++++++++++------- libexec/quran-json/www.searchtruth.com | 17 +++++++++------ .../data/{surahinfo.json => metadata.json} | 0 share/quran-json/data/sources.json | 20 +++++++++++------- 7 files changed, 58 insertions(+), 29 deletions(-) rename share/quran-json/data/{surahinfo.json => metadata.json} (100%) diff --git a/README.md b/README.md index 20d331a..c4d9a58 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ the content of The Quran in multiple languages. Usage: quran-json pull [OPTIONS] -l, --locale LOCALE ar, en, pt, fa, nl, fr, or it (default: en) -o, --overwrite Overwrite existing JSON files (default: no) + -u, --update Only update the surah metadata (implies -o, default: no) ## Thanks diff --git a/lib/quran-json/command.rb b/lib/quran-json/command.rb index ac2ddbd..d5e8839 100644 --- a/lib/quran-json/command.rb +++ b/lib/quran-json/command.rb @@ -20,7 +20,7 @@ module Command end def locale_dir - File.join(share_dir, options.locale) + File.join(quran_dir, options.locale) end def line @@ -33,9 +33,15 @@ module Command ) end - def surah_info - @surah_info ||= Ryo.from( - JSON.parse File.binread(File.join(data_dir, "surahinfo.json")) - ) + def metadata + @metadata ||= read_metadata + end + + private + + def read_metadata + c = File.binread(File.join(data_dir, "metadata.json")) + m = JSON.parse(c).map { _1.merge!("translated_by" => source.translated_by) } + Ryo.from(m) end end diff --git a/lib/quran-json/pull.rb b/lib/quran-json/pull.rb index a7c310a..7f1e4ad 100644 --- a/lib/quran-json/pull.rb +++ b/lib/quran-json/pull.rb @@ -14,12 +14,13 @@ class Pull def self.cli(argv) op = nil - options = Ryo({locale: "en", overwrite: false}) + options = Ryo({locale: "en", overwrite: false, update: false}) OptionParser.new(nil, 26, " " * 2) do |o| o.banner = "Usage: quran-json pull [OPTIONS]" op = o o.on("-l", "--locale LOCALE", "ar, en, pt, fa, nl, fr, or it (default: en)") o.on("-o", "--overwrite", "Overwrite existing JSON files (default: no)") + o.on("-u", "--update", "Only update the surah metadata (implies -o, default: no)") end.parse(argv, into: options) options rescue @@ -43,10 +44,15 @@ class Pull def write(surah_no, rows) mkdir_p(locale_dir) - rows.unshift(Ryo.table_of(surah_info[surah_no - 1])) + rows[0] = Ryo.table_of(metadata[surah_no-1]) File.binwrite File.join(locale_dir, "#{surah_no}.json"), JSON.pretty_generate(rows) end + def update(surah_no) + rows = JSON.parse File.binread(File.join(locale_dir, "#{surah_no}.json")) + write(surah_no, rows) + end + def keepalive http.start yield @@ -58,7 +64,7 @@ class Pull # @return [Boolean] # Returns true when a surah shouldn't be replaced def keep?(surah_no) - exist?(surah_no) and [options.overwrite].all? { _1.equal?(false) } + exist?(surah_no) and [options.overwrite, options.update].all? { _1.equal?(false) } end private diff --git a/libexec/quran-json/quran.com b/libexec/quran-json/quran.com index e32e09b..5be7068 100755 --- a/libexec/quran-json/quran.com +++ b/libexec/quran-json/quran.com @@ -16,15 +16,20 @@ def main(argv) cmd = Pull.new(Pull.cli(argv)) cmd.keepalive do 1.upto(114) do |surah_no| - next if cmd.keep?(surah_no) - rows = [] - 1.upto(cmd.count[surah_no]) do |ayah_no| - res = cmd.pull_ayah(surah_no, ayah_no) - rows.push([ayah_no, grep(res)]) - cmd.line.rewind.print "Surah #{surah_no} [#{ayah_no}/#{cmd.count[surah_no]}]" + if cmd.keep?(surah_no) + next + elsif cmd.options.update + cmd.update(surah_no) + else + rows = [] + 1.upto(cmd.count[surah_no]) do |ayah_no| + res = cmd.pull_ayah(surah_no, ayah_no) + rows.push([ayah_no, grep(res)]) + cmd.line.rewind.print "Surah #{surah_no} [#{ayah_no}/#{cmd.count[surah_no]}]" + end + cmd.write(surah_no, rows) + cmd.line.end end - cmd.write(surah_no, rows) - cmd.line.end end end end diff --git a/libexec/quran-json/www.searchtruth.com b/libexec/quran-json/www.searchtruth.com index 07bc376..6d61418 100755 --- a/libexec/quran-json/www.searchtruth.com +++ b/libexec/quran-json/www.searchtruth.com @@ -17,12 +17,17 @@ def main(argv) cmd = Pull.new(Pull.cli(argv)) cmd.keepalive do 1.upto(114) do |surah_no| - next if cmd.keep?(surah_no) - rows = [] - res = cmd.pull_surah(surah_no) - rows.concat(grep(res).map.with_index(1) { [_2, _1] }) - cmd.line.rewind.print "Surah #{surah_no} [#{surah_no}/114]" - cmd.write(surah_no, rows) + if cmd.keep?(surah_no) + next + elsif cmd.options.update + cmd.update(surah_no) + else + rows = [] + res = cmd.pull_surah(surah_no) + rows.concat(grep(res).map.with_index(1) { [_2, _1] }) + cmd.line.rewind.print "Surah #{surah_no} [#{surah_no}/114]" + cmd.write(surah_no, rows) + end end cmd.line.end end diff --git a/share/quran-json/data/surahinfo.json b/share/quran-json/data/metadata.json similarity index 100% rename from share/quran-json/data/surahinfo.json rename to share/quran-json/data/metadata.json diff --git a/share/quran-json/data/sources.json b/share/quran-json/data/sources.json index 1ffa557..afb2da6 100644 --- a/share/quran-json/data/sources.json +++ b/share/quran-json/data/sources.json @@ -7,7 +7,8 @@ }, "dest": { "dir": "%{share_dir}/TheQuran/ar" - } + }, + "translated_by": null }, "en": { "http": { @@ -17,7 +18,8 @@ }, "dest": { "dir": "%{share_dir}/TheQuran/en" - } + }, + "translated_by": "Dr. Mustafa Khattab" }, "pt": { "http": { @@ -27,7 +29,8 @@ }, "dest": { "dir": "%{share_dir}/TheQuran/pt" - } + }, + "translated_by": null }, "fa": { "http": { @@ -37,7 +40,8 @@ }, "dest": { "dir": "%{share_dir}/TheQuran/fa" - } + }, + "translated_by": "Hussein Taji Kal Dari" }, "nl": { "http": { @@ -47,7 +51,8 @@ }, "dest": { "dir": "%{share_dir}/TheQuran/nl" - } + }, + "translated_by": "Sofian S. Siregar" }, "fr": { "http": { @@ -57,7 +62,8 @@ }, "dest": { "dir": "%{share_dir}/TheQuran/fr" - } + }, + "translated_by": "Muhammad Hamidullah" }, "it": { "http": { @@ -68,6 +74,6 @@ "dest": { "dir": "%{share_dir}/TheQuran/it" }, - "author": "Hamza Roberto Piccardo" + "translated_by": "Hamza Roberto Piccardo" } }