commit f46c7a21a14cf9490c7ad95cb301df1280515348
parent 02c905307fd93c3cf7d040905147efee4e20fbc1
Author: Agastya Chandrakant <me@hanabi.in>
Date: Sun, 1 May 2022 03:23:53 +0530
Add sub-command to print the available translations.
Diffstat:
M | .gitignore | | | 1 | + |
M | main.go | | | 94 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------- |
M | makefile | | | 4 | ++-- |
3 files changed, 73 insertions(+), 26 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1 +1,2 @@
**/*.DS_Store
+quran
diff --git a/main.go b/main.go
@@ -8,39 +8,49 @@ import (
"net/http"
"os"
"regexp"
+ "sort"
"strconv"
"time"
)
-const DEBUG = false
+const DEBUG = true
const API = "https://api.quran.com/api/v4"
func main() {
- input, trans, timeDelay := handleInputFlags()
- if err, chap, ver1, ver2 := parseInput(input); err != nil {
- printerr(err)
- } else if err, transAuthor := checkTrans(trans); err != nil {
- printerr(err)
+
+ input, trans, timeDelay, shouldListTrans := handleInputFlags()
+
+ if shouldListTrans {
+ if err := printTranslations(); err != nil {
+ printerr(err)
+ }
} else {
- if err, maxVerse := checkVerseRange(chap, ver1, ver2); err != nil {
+
+ if err, chap, ver1, ver2 := parseInput(input); err != nil {
+ printerr(err)
+ } else if err, transAuthor := checkTrans(trans); err != nil {
printerr(err)
} else {
- if ver1 == 0 {
- // Print whole chapter, ie from 1-maxVerse
- for verse := t_verse(1); verse <= maxVerse; verse++ {
- printVerse(trans, chap, verse, timeDelay)
+ if err, maxVerse := checkVerseRange(chap, ver1, ver2); err != nil {
+ printerr(err)
+ } else {
+ if ver1 == 0 {
+ // Print whole chapter, ie from 1-maxVerse
+ for verse := t_verse(1); verse <= maxVerse; 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, timeDelay)
+ }
+ } else if ver1 == ver2 {
+ // Print just one verse, if just ver 1.
+ printVerse(trans, chap, ver1, 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, timeDelay)
- }
- } else if ver1 == ver2 {
- // Print just one verse, if just ver 1.
- printVerse(trans, chap, ver1, timeDelay)
+ printRange(transAuthor, chap, ver1, ver2, maxVerse)
}
- printRange(transAuthor, chap, ver1, ver2, maxVerse)
}
}
}
@@ -226,18 +236,54 @@ type ChaptersList struct {
Chapters []Chapter `json:"chapters"`
}
-func handleInputFlags() (input string, trans t_trans, timeDelay float64) {
+func handleInputFlags() (input string, trans t_trans, timeDelay float64, shouldListTrans bool) {
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.IntVar(&transInt, "trans", 131, "Which translation to use.")
+ flag.Float64Var(&timeDelay, "delay", 0.2, "Minimum delay in seconds between fetching of two aayah.")
+ flag.BoolVar(&shouldListTrans, "list-trans", false, "Print list of available translations.")
+
flag.Parse()
+
input = flag.Arg(0)
trans = t_trans(transInt)
return
}
+func printHelp() {
+ // @TODO
+ debug("printing help")
+}
+
+func printTranslations() (err error) {
+
+ var transList TranslationList
+ if err := quranHttpGet(API+"/resources/translations", &transList); err != nil {
+ return err
+ }
+
+ trans := transList.Translations
+ sort.Slice(trans, func(a, b int) bool {
+ return trans[a].ID < trans[b].ID
+ })
+
+ fmt.Println("Available translations:\n")
+
+ for _, elem := range transList.Translations {
+ fmt.Printf("%d\t(%s)\t\t%s\n", elem.ID, elem.LanguageName, elem.Name)
+ }
+
+ return err
+
+}
+
func parseInput(input string) (err error, chapter t_chap, verse1, verse2 t_verse) {
+ if len(os.Args) == 1 {
+ printHelp()
+ os.Exit(0)
+ }
+
// Group 1, 3, 5 = Chapter:Verse1-Verse2
re := regexp.MustCompile(`^(\d)+(:(\d+)(-(\d+))?)?$`)
isValid := re.Match([]byte(input))
diff --git a/makefile b/makefile
@@ -1,2 +1,2 @@
-dev:
- go run main.go -lang=ur 2:14-16 && read && vim main.go
+build:
+ go run -o quran main.go