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]
-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

View file

@ -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

View file

@ -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

View file

@ -16,7 +16,11 @@ def main(argv)
cmd = Pull.new(Pull.cli(argv))
cmd.keepalive do
1.upto(114) do |surah_no|
next if cmd.keep?(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)
@ -28,4 +32,5 @@ def main(argv)
end
end
end
end
main(ARGV)

View file

@ -17,13 +17,18 @@ def main(argv)
cmd = Pull.new(Pull.cli(argv))
cmd.keepalive do
1.upto(114) do |surah_no|
next if cmd.keep?(surah_no)
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
end

View file

@ -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"
}
}