commit 02c905307fd93c3cf7d040905147efee4e20fbc1
parent ecbc6cb968db342669dd6a9c723def26b45bea01
Author: Agastya Chandrakant <me@hanabi.in>
Date: Fri, 29 Apr 2022 01:33:25 +0530
Add option to rate-limit. See
https://github.com/quran/quran.com-api/issues/554
Default to 0.2s delay.
Diffstat:
2 files changed, 10 insertions(+), 6 deletions(-)
diff --git a/PLAN b/PLAN
@@ -16,3 +16,4 @@ Features
========
+ Should I support 2:14- to print all verses from 14 till the end of the chapter?
++ Add `quran about|details|list-translation|help'
diff --git a/main.go b/main.go
@@ -9,13 +9,14 @@ import (
"os"
"regexp"
"strconv"
+ "time"
)
const DEBUG = false
const API = "https://api.quran.com/api/v4"
func main() {
- input, trans := getInput()
+ input, trans, timeDelay := handleInputFlags()
if err, chap, ver1, ver2 := parseInput(input); err != nil {
printerr(err)
} else if err, transAuthor := checkTrans(trans); err != nil {
@@ -27,17 +28,17 @@ func main() {
if ver1 == 0 {
// Print whole chapter, ie from 1-maxVerse
for verse := t_verse(1); verse <= maxVerse; verse++ {
- printVerse(trans, chap, verse)
+ printVerse(trans, chap, verse, timeDelay)
}
} else if ver1 < ver2 {
// Print the range of verses, if from ver1 to ver2.
// @TODO check if < or <= ?
for verse := ver1; verse <= ver2; verse++ {
- printVerse(trans, chap, verse)
+ printVerse(trans, chap, verse, timeDelay)
}
} else if ver1 == ver2 {
// Print just one verse, if just ver 1.
- printVerse(trans, chap, ver1)
+ printVerse(trans, chap, ver1, timeDelay)
}
printRange(transAuthor, chap, ver1, ver2, maxVerse)
}
@@ -91,9 +92,10 @@ type Translations struct {
TranslatedName TranslatedName `json:"translated_name"`
}
-func printVerse(trans t_trans, chap t_chap, verse t_verse) {
+func printVerse(trans t_trans, chap t_chap, verse t_verse, timeDelay float64) {
uri := fmt.Sprintf("%s/translations/%d/by_ayah/%d:%d", API, trans, chap, verse)
var verseData Verse
+ time.Sleep(time.Duration(timeDelay) * time.Second) // Rate-limit to minimise server load.
if err := quranHttpGet(uri, &verseData); err != nil {
printerr(err)
} else {
@@ -224,9 +226,10 @@ type ChaptersList struct {
Chapters []Chapter `json:"chapters"`
}
-func getInput() (input string, trans t_trans) {
+func handleInputFlags() (input string, trans t_trans, timeDelay float64) {
var transInt int
flag.IntVar(&transInt, "translation", 131, "Which translation to use.")
+ flag.Float64Var(&timeDelay, "delay", 0.2, "Minimum delay between fetching of two aayah.")
flag.Parse()
input = flag.Arg(0)
trans = t_trans(transInt)