0x1eef
2d9a22ea8f
The project's directory structure has been changed to be similar to how FreeBSD organizes its filesystem. The english, portuguese, and farsi translations are now sourced from https://quran.com. The original Arabic is still sourced from searchtruth.com. Files have been removed, and / or replaced. The SQL-related files have been removed, with the intention to separate them into a new project in the future (quran-sql). The lessons learnt from the development of quran-audio were an inspiration for this change.
77 lines
1.6 KiB
Ruby
77 lines
1.6 KiB
Ruby
class Pull
|
|
require "ryo"
|
|
require "json"
|
|
require "net/http"
|
|
require "fileutils"
|
|
require "optparse"
|
|
require_relative "command"
|
|
include Command
|
|
include FileUtils
|
|
|
|
attr_reader :options,
|
|
:source,
|
|
:http
|
|
|
|
def self.cli(argv)
|
|
op = nil
|
|
options = Ryo({locale: "en"})
|
|
OptionParser.new do |o|
|
|
op = o
|
|
o.on("-l", "--locale LOCALE", "ar, en, pt, or fa (default: en)")
|
|
end.parse(argv, into: options)
|
|
options
|
|
rescue
|
|
puts op.help
|
|
exit
|
|
end
|
|
|
|
def initialize(options)
|
|
@options = options
|
|
@source = sources[options.locale]
|
|
@http = Net::HTTP.new(source.http.hostname, 443).tap { _1.use_ssl = true }
|
|
end
|
|
|
|
def pull_surah(surah_no, &b)
|
|
pull req_path(vars(binding)), &b
|
|
end
|
|
|
|
def pull_ayah(surah_no, ayah_no, &b)
|
|
pull req_path(vars(binding)), &b
|
|
end
|
|
|
|
def write(surah_no, rows)
|
|
dir = File.join(quran_dir, options.locale)
|
|
mkdir_p(dir)
|
|
rows.unshift(Ryo.table_of(surah_info[surah_no - 1]))
|
|
File.binwrite File.join(dir, "#{surah_no}.json"), JSON.pretty_generate(rows)
|
|
end
|
|
|
|
private
|
|
|
|
def req_path(vars)
|
|
format source.http.path, source.http.vars.map { [_1.to_sym, vars[_1.to_sym]] }.to_h
|
|
end
|
|
|
|
def pull(path)
|
|
res = http.get(path)
|
|
case res
|
|
when Net::HTTPOK
|
|
yield(res)
|
|
else
|
|
##
|
|
# TODO: Handle error
|
|
end
|
|
end
|
|
|
|
def vars(binding)
|
|
binding.local_variables.map do
|
|
[_1.to_sym, binding.local_variable_get(_1)]
|
|
end.to_h
|
|
end
|
|
|
|
def sources
|
|
@sources ||= Ryo.from(
|
|
JSON.parse File.binread(File.join(data_dir, "sources.json"))
|
|
)
|
|
end
|
|
end
|