pull-english: rewrite with the help of bindata/chapters-length.json
This commit is contained in:
parent
01d50354e5
commit
8c283e23ee
2 changed files with 59 additions and 36 deletions
|
@ -14,55 +14,49 @@ require "net/http"
|
||||||
require "nokogiri"
|
require "nokogiri"
|
||||||
require "json"
|
require "json"
|
||||||
require "paint"
|
require "paint"
|
||||||
|
require_relative "../../binlib/line"
|
||||||
|
|
||||||
##
|
##
|
||||||
# Configuration variables.
|
# Configuration variables.
|
||||||
base_uri = "quran.com"
|
base_uri = "quran.com"
|
||||||
path = "/%{chapter_num}/%{verse_num}"
|
dest = File.join("src", "json", "en", "%{chapter}.json")
|
||||||
dest_path = File.join(
|
delay = 1.5
|
||||||
__dir__, "..", "src", "json", "en", "%{chapter_num}.json"
|
|
||||||
)
|
##
|
||||||
src_path = File.join(
|
# Utils
|
||||||
__dir__, "..", "src", "json", "ar", "%{chapter_num}.json"
|
line = Line.new($stdout)
|
||||||
)
|
find_content = ->(res) do
|
||||||
chapter_count = 114
|
html = Nokogiri::HTML(res.body)
|
||||||
cool_off = 5
|
el = html.css("div[class^='TranslationText']").last
|
||||||
|
el.text.gsub(/[0-9]/, "")
|
||||||
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Map chapters to their verse count.
|
||||||
|
vmap_path = File.join("bindata", "chapters-length.json")
|
||||||
|
vmap = JSON.parse(File.read(vmap_path))
|
||||||
|
|
||||||
##
|
##
|
||||||
# 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
|
||||||
|
|
||||||
##
|
1.upto(114) do |chapter|
|
||||||
# Helper method.
|
path = format(dest, chapter:)
|
||||||
def get_request(path, chapter_num, verse_num)
|
|
||||||
Net::HTTP::Get.new(
|
|
||||||
format(path, chapter_num:, verse_num:),
|
|
||||||
"Accept" => "text/html"
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
##
|
|
||||||
# main()
|
|
||||||
1.upto(chapter_count) do |chapter_num|
|
|
||||||
final_dest = format(dest_path, chapter_num:)
|
|
||||||
rows = []
|
rows = []
|
||||||
|
vcount = vmap[chapter.to_s]
|
||||||
|
|
||||||
verses = JSON.parse File.read(format(src_path, chapter_num:))
|
1.upto(vcount) do |verse|
|
||||||
verses.each do |verse_num, _|
|
line.rewind.print "Download: #{verse}/#{vcount}"
|
||||||
case res = http.request(get_request(path, chapter_num, verse_num))
|
res = http.request_get("/#{chapter}/#{verse}")
|
||||||
when Net::HTTPOK
|
if Net::HTTPOK === res
|
||||||
doc = Nokogiri::HTML(res.body)
|
rows.push([verse, find_content.(res)])
|
||||||
el = doc.css("div[class^='TranslationText']").last
|
|
||||||
text = el.text.gsub(/[0-9]/, "")
|
|
||||||
rows.push([verse_num, text])
|
|
||||||
print Paint["OK: ", :green, :bold], text, "\n"
|
|
||||||
else
|
else
|
||||||
print Paint["ERROR (#{res.class}): ", :red, :bold], final_dest, "\n"
|
print "\n", Paint["Error: ", :red, :bold], res.class, "\n"
|
||||||
|
exit(1)
|
||||||
end
|
end
|
||||||
print Paint["Chill for #{cool_off} seconds", :blue, :bold], "\n", "\n"
|
sleep delay
|
||||||
sleep cool_off
|
|
||||||
end
|
end
|
||||||
File.write(final_dest, JSON.pretty_generate(rows))
|
File.write(path, JSON.pretty_generate(rows))
|
||||||
print Paint["OK: ", :green, :bold], final_dest, "\n"
|
print "\n", Paint["OK: ", :green, :bold], path, "\n"
|
||||||
end
|
end
|
||||||
|
|
29
binlib/line.rb
Normal file
29
binlib/line.rb
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Line
|
||||||
|
def initialize(io)
|
||||||
|
@io = io
|
||||||
|
@size = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def puts(str)
|
||||||
|
print(str).end
|
||||||
|
end
|
||||||
|
|
||||||
|
def print(str)
|
||||||
|
str = str.gsub(/\n*/, "")
|
||||||
|
@size = str.size
|
||||||
|
@io.print str
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
def end
|
||||||
|
@io.print "\n"
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
def rewind
|
||||||
|
@io.print "\b \b" * @size
|
||||||
|
self
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue