quran-go

Read Qur'an right in the terminal.
git clone http://git.hanabi.in/repos/quran-go.git
Log | Files | Refs | README | LICENSE

commit ecbc6cb968db342669dd6a9c723def26b45bea01
parent 739a0a16df6be1471d895a46e4e9ef38fc910a01
Author: Agastya Chandrakant <me@hanabi.in>
Date:   Wed, 27 Apr 2022 17:53:39 +0530

Print verses.

Diffstat:
Mmain.go | 108+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
1 file changed, 92 insertions(+), 16 deletions(-)

diff --git a/main.go b/main.go @@ -11,41 +11,120 @@ import ( "strconv" ) -const DEBUG = true +const DEBUG = false const API = "https://api.quran.com/api/v4" func main() { - input, lang := getInput() + input, trans := getInput() if err, chap, ver1, ver2 := parseInput(input); err != nil { printerr(err) + } else if err, transAuthor := checkTrans(trans); err != nil { + printerr(err) } else { - debug(lang) 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(chap, verse) + printVerse(trans, chap, verse) } } 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(chap, verse) + printVerse(trans, chap, verse) } } else if ver1 == ver2 { // Print just one verse, if just ver 1. - printVerse(chap, ver1) + printVerse(trans, chap, ver1) } + printRange(transAuthor, chap, ver1, ver2, maxVerse) + } + } +} + +func printRange(transAuthor string, chap t_chap, ver1, ver2, maxVerse t_verse) { + res := fmt.Sprintf("\t--Qur'an %d:", chap) + // if "1:0-0" || "1:3-3" || "1:3-7" + if ver1 == 0 { // "1:0-0" + res += fmt.Sprintf("%d-%d", 1, maxVerse) + } else if ver1 == ver2 { // "1:3-3" + res += fmt.Sprintf("%d", ver1) + } else { // "1:3-7" + res += fmt.Sprintf("%d:%d", ver1, ver2) + } + + res += fmt.Sprintf(", %s translation.\n", transAuthor) + + fmt.Println(res) +} + +func checkTrans(trans t_trans) (err error, transAuthor string) { + var transList TranslationList + if err = quranHttpGet(API+"/resources/translations", &transList); err != nil { + return + } + for _, elem := range transList.Translations { + if trans == t_trans(elem.ID) { + transAuthor = elem.AuthorName + return } } + err = fmt.Errorf("%d is not a valid translation, please use `quran list-trans' to see available translations.", trans) + return } -func printVerse(chap t_chap, verse t_verse) { - debug(fmt.Sprintf("Fetching %d:%d...\n", chap, verse)) +type TranslationList struct { + Translations []Translations `json:"translations"` +} +type TranslatedName struct { + Name string `json:"name"` + LanguageName string `json:"language_name"` +} +type Translations struct { + ID int `json:"id"` + Name string `json:"name"` + AuthorName string `json:"author_name"` + Slug string `json:"slug"` + LanguageName string `json:"language_name"` + TranslatedName TranslatedName `json:"translated_name"` } +func printVerse(trans t_trans, chap t_chap, verse t_verse) { + uri := fmt.Sprintf("%s/translations/%d/by_ayah/%d:%d", API, trans, chap, verse) + var verseData Verse + if err := quranHttpGet(uri, &verseData); err != nil { + printerr(err) + } else { + formattedAayat := formatAayat(verseData.Translations[0].Text) + fmt.Println(formattedAayat) + } +} + +func formatAayat(aayat string) (formatAayat string) { + reg := regexp.MustCompile(`<sup foot_note=\d+>\d+<\/sup>`) + formatAayat = reg.ReplaceAllString(aayat, "") + return +} + +type Verse struct { + Translations []struct { + ID int `json:"id"` + ResourceID int `json:"resource_id"` + Text string `json:"text"` + } `json:"translations"` + Pagination struct { + PerPage int `json:"per_page"` + CurrentPage int `json:"current_page"` + NextPage interface{} `json:"next_page"` + TotalPages int `json:"total_pages"` + TotalRecords int `json:"total_records"` + } `json:"pagination"` +} + +type t_trans int + func checkVerseRange(chap t_chap, ver1, ver2 t_verse) (err error, maxVerse t_verse) { if err = checkChapRange(chap); err != nil { return @@ -86,11 +165,6 @@ type Chapter struct { TranslatedName TranslatedName `json:"translated_name"` } -type TranslatedName struct { - LanguageName string `json:"language_name"` - Name string `json:"name"` -} - type t_chap uint type t_verse uint @@ -150,11 +224,13 @@ type ChaptersList struct { Chapters []Chapter `json:"chapters"` } -func getInput() (input, lang string) { - flag.StringVar(&lang, "lang", "en", "Verse language.") +func getInput() (input string, trans t_trans) { + var transInt int + flag.IntVar(&transInt, "translation", 131, "Which translation to use.") flag.Parse() input = flag.Arg(0) - return input, lang + trans = t_trans(transInt) + return } func parseInput(input string) (err error, chapter t_chap, verse1, verse2 t_verse) {