standardrb: fix.

This commit is contained in:
0x1eef 2022-04-26 15:26:17 -03:00
parent 39a37b6887
commit 9263c5366a
2 changed files with 23 additions and 27 deletions

View file

@ -7,16 +7,16 @@ require "paint"
## ##
# Configuration variables. # Configuration variables.
base_uri = "www.sacred-texts.com" base_uri = "www.sacred-texts.com"
path = "/isl/uq/%{htm_file}" path = "/isl/uq/%{htm_file}"
dest_path = File.join(__dir__, "..", "src", "arabic", "%{chapter_num}.json") dest_path = File.join(__dir__, "..", "src", "arabic", "%{chapter_num}.json")
chapter_count = 114 chapter_count = 114
cool_off = 5 cool_off = 5
## ##
# Share a single Net::HTTP instance. # Share a single Net::HTTP instance.
http = Net::HTTP.new(base_uri, 443) http = Net::HTTP.new(base_uri, 443)
http.use_ssl = true http.use_ssl = true
## ##
# Helper method. # Helper method.
@ -35,22 +35,22 @@ end
def get_request(path, htm_file) def get_request(path, htm_file)
Net::HTTP::Get.new( Net::HTTP::Get.new(
format(path, htm_file: htm_file), format(path, htm_file: htm_file),
'Accept' => 'text/html' "Accept" => "text/html"
) )
end end
## ##
# Helper method. # Helper method.
def extract_verses!(res, rows) def extract_verses!(res, rows)
doc = Nokogiri::HTML(res.body) doc = Nokogiri::HTML(res.body)
verses = doc.css('table tr td p[align=RIGHT]') verses = doc.css("table tr td p[align=RIGHT]")
verses.each do |verse| verses.each do |verse|
verse_num = Integer(verse.css('a').inner_text) verse_num = Integer(verse.css("a").inner_text)
verse_txt = verse.text.delete(verse_num.to_s) verse_txt = verse.text.delete(verse_num.to_s)
rows.push([ rows.push([
verse_num, verse_num,
verse_txt.gsub("\u200F", "").gsub("\u200E", "").strip verse_txt.delete("\u200F").delete("\u200E").strip
]) ])
end end
end end
@ -58,7 +58,7 @@ end
## ##
# main() # main()
1.upto(chapter_count) do |chapter_num| 1.upto(chapter_count) do |chapter_num|
htm_file = get_htm_file(chapter_num) htm_file = get_htm_file(chapter_num)
final_dest = format(dest_path, chapter_num:) final_dest = format(dest_path, chapter_num:)
rows = [] rows = []
@ -74,5 +74,3 @@ end
print Paint["Chill for #{cool_off} seconds", :blue, :bold], "\n", "\n" print Paint["Chill for #{cool_off} seconds", :blue, :bold], "\n", "\n"
sleep cool_off sleep cool_off
end end

View file

@ -7,24 +7,24 @@ require "paint"
## ##
# Configuration variables. # Configuration variables.
base_uri = "quran.com" base_uri = "quran.com"
path = "/%{chapter_num}/%{verse_num}" path = "/%{chapter_num}/%{verse_num}"
dest_path = File.join(__dir__, "..", "src", "english", "%{chapter_num}.json") dest_path = File.join(__dir__, "..", "src", "english", "%{chapter_num}.json")
arab_path = File.join(__dir__, "..", "src", "arabic", "%{chapter_num}.json") arab_path = File.join(__dir__, "..", "src", "arabic", "%{chapter_num}.json")
chapter_count = 114 chapter_count = 114
cool_off = 5 cool_off = 5
## ##
# Share a single Net::HTTP instance. # Share a single Net::HTTP instance.
http = Net::HTTP.new(base_uri, 443) http = Net::HTTP.new(base_uri, 443)
http.use_ssl = true http.use_ssl = true
## ##
# Helper method. # Helper method.
def get_request(path, chapter_num, verse_num) def get_request(path, chapter_num, verse_num)
Net::HTTP::Get.new( Net::HTTP::Get.new(
format(path, chapter_num:, verse_num:), format(path, chapter_num:, verse_num:),
'Accept' => 'text/html' "Accept" => "text/html"
) )
end end
@ -38,9 +38,9 @@ end
verses.each do |verse_num, _| verses.each do |verse_num, _|
case res = http.request(get_request(path, chapter_num, verse_num)) case res = http.request(get_request(path, chapter_num, verse_num))
when Net::HTTPOK when Net::HTTPOK
doc = Nokogiri::HTML(res.body) doc = Nokogiri::HTML(res.body)
el = doc.css("div[class^='TranslationText']").last el = doc.css("div[class^='TranslationText']").last
text = el.text.gsub(/[0-9]/, '') text = el.text.gsub(/[0-9]/, "")
rows.push([verse_num, text]) rows.push([verse_num, text])
print Paint["OK: ", :green, :bold], text, "\n" print Paint["OK: ", :green, :bold], text, "\n"
else else
@ -52,5 +52,3 @@ end
File.write(final_dest, JSON.pretty_generate(rows)) File.write(final_dest, JSON.pretty_generate(rows))
print Paint["OK: ", :green, :bold], final_dest, "\n" print Paint["OK: ", :green, :bold], final_dest, "\n"
end end