Add -u, --update switch to "pull" command

This commit is contained in:
0x1eef 2023-02-13 04:17:44 -03:00
parent 316133c235
commit 42c3b82712
7 changed files with 58 additions and 29 deletions

View file

@ -41,6 +41,7 @@ the content of The Quran in multiple languages.
Usage: quran-json pull [OPTIONS] Usage: quran-json pull [OPTIONS]
-l, --locale LOCALE ar, en, pt, fa, nl, fr, or it (default: en) -l, --locale LOCALE ar, en, pt, fa, nl, fr, or it (default: en)
-o, --overwrite Overwrite existing JSON files (default: no) -o, --overwrite Overwrite existing JSON files (default: no)
-u, --update Only update the surah metadata (implies -o, default: no)
## Thanks ## Thanks

View file

@ -20,7 +20,7 @@ module Command
end end
def locale_dir def locale_dir
File.join(share_dir, options.locale) File.join(quran_dir, options.locale)
end end
def line def line
@ -33,9 +33,15 @@ module Command
) )
end end
def surah_info def metadata
@surah_info ||= Ryo.from( @metadata ||= read_metadata
JSON.parse File.binread(File.join(data_dir, "surahinfo.json")) 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
end end

View file

@ -14,12 +14,13 @@ class Pull
def self.cli(argv) def self.cli(argv)
op = nil op = nil
options = Ryo({locale: "en", overwrite: false}) options = Ryo({locale: "en", overwrite: false, update: false})
OptionParser.new(nil, 26, " " * 2) do |o| OptionParser.new(nil, 26, " " * 2) do |o|
o.banner = "Usage: quran-json pull [OPTIONS]" o.banner = "Usage: quran-json pull [OPTIONS]"
op = o op = o
o.on("-l", "--locale LOCALE", "ar, en, pt, fa, nl, fr, or it (default: en)") 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("-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) end.parse(argv, into: options)
options options
rescue rescue
@ -43,10 +44,15 @@ class Pull
def write(surah_no, rows) def write(surah_no, rows)
mkdir_p(locale_dir) 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) File.binwrite File.join(locale_dir, "#{surah_no}.json"), JSON.pretty_generate(rows)
end end
def update(surah_no)
rows = JSON.parse File.binread(File.join(locale_dir, "#{surah_no}.json"))
write(surah_no, rows)
end
def keepalive def keepalive
http.start http.start
yield yield
@ -58,7 +64,7 @@ class Pull
# @return [Boolean] # @return [Boolean]
# Returns true when a surah shouldn't be replaced # Returns true when a surah shouldn't be replaced
def keep?(surah_no) 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 end
private private

View file

@ -16,15 +16,20 @@ def main(argv)
cmd = Pull.new(Pull.cli(argv)) cmd = Pull.new(Pull.cli(argv))
cmd.keepalive do cmd.keepalive do
1.upto(114) do |surah_no| 1.upto(114) do |surah_no|
next if cmd.keep?(surah_no) if cmd.keep?(surah_no)
rows = [] next
1.upto(cmd.count[surah_no]) do |ayah_no| elsif cmd.options.update
res = cmd.pull_ayah(surah_no, ayah_no) cmd.update(surah_no)
rows.push([ayah_no, grep(res)]) else
cmd.line.rewind.print "Surah #{surah_no} [#{ayah_no}/#{cmd.count[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]}]"
end
cmd.write(surah_no, rows)
cmd.line.end
end end
cmd.write(surah_no, rows)
cmd.line.end
end end
end end
end end

View file

@ -17,12 +17,17 @@ def main(argv)
cmd = Pull.new(Pull.cli(argv)) cmd = Pull.new(Pull.cli(argv))
cmd.keepalive do cmd.keepalive do
1.upto(114) do |surah_no| 1.upto(114) do |surah_no|
next if cmd.keep?(surah_no) if cmd.keep?(surah_no)
rows = [] next
res = cmd.pull_surah(surah_no) elsif cmd.options.update
rows.concat(grep(res).map.with_index(1) { [_2, _1] }) cmd.update(surah_no)
cmd.line.rewind.print "Surah #{surah_no} [#{surah_no}/114]" else
cmd.write(surah_no, rows) 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 end
cmd.line.end cmd.line.end
end end

View file

@ -7,7 +7,8 @@
}, },
"dest": { "dest": {
"dir": "%{share_dir}/TheQuran/ar" "dir": "%{share_dir}/TheQuran/ar"
} },
"translated_by": null
}, },
"en": { "en": {
"http": { "http": {
@ -17,7 +18,8 @@
}, },
"dest": { "dest": {
"dir": "%{share_dir}/TheQuran/en" "dir": "%{share_dir}/TheQuran/en"
} },
"translated_by": "Dr. Mustafa Khattab"
}, },
"pt": { "pt": {
"http": { "http": {
@ -27,7 +29,8 @@
}, },
"dest": { "dest": {
"dir": "%{share_dir}/TheQuran/pt" "dir": "%{share_dir}/TheQuran/pt"
} },
"translated_by": null
}, },
"fa": { "fa": {
"http": { "http": {
@ -37,7 +40,8 @@
}, },
"dest": { "dest": {
"dir": "%{share_dir}/TheQuran/fa" "dir": "%{share_dir}/TheQuran/fa"
} },
"translated_by": "Hussein Taji Kal Dari"
}, },
"nl": { "nl": {
"http": { "http": {
@ -47,7 +51,8 @@
}, },
"dest": { "dest": {
"dir": "%{share_dir}/TheQuran/nl" "dir": "%{share_dir}/TheQuran/nl"
} },
"translated_by": "Sofian S. Siregar"
}, },
"fr": { "fr": {
"http": { "http": {
@ -57,7 +62,8 @@
}, },
"dest": { "dest": {
"dir": "%{share_dir}/TheQuran/fr" "dir": "%{share_dir}/TheQuran/fr"
} },
"translated_by": "Muhammad Hamidullah"
}, },
"it": { "it": {
"http": { "http": {
@ -68,6 +74,6 @@
"dest": { "dest": {
"dir": "%{share_dir}/TheQuran/it" "dir": "%{share_dir}/TheQuran/it"
}, },
"author": "Hamza Roberto Piccardo" "translated_by": "Hamza Roberto Piccardo"
} }
} }