Browse Source

Fix adding code to list bug

master
joop 2 months ago
parent
commit
a3e893e9ac
  1. 2
      go.mod
  2. 2
      go.sum
  3. 128
      main.go

2
go.mod

@ -1,3 +1,5 @@ @@ -1,3 +1,5 @@
module phelpsify
go 1.18
require git.kiefte.eu/lapingvino/prompt v0.0.0-20220328003637-2fee34066122

2
go.sum

@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
git.kiefte.eu/lapingvino/prompt v0.0.0-20220328003637-2fee34066122 h1:y6OTb0LzNwARF7o1q0AN/pz4Q++KgMDRJ+NONXTMM0s=
git.kiefte.eu/lapingvino/prompt v0.0.0-20220328003637-2fee34066122/go.mod h1:ziwFN3OVeKNCkg5qsMv+BYhdtEfLxnPwh8kEAqVEjAk=

128
main.go

@ -21,6 +21,8 @@ import ( @@ -21,6 +21,8 @@ import (
"os"
"strconv"
"strings"
"git.kiefte.eu/lapingvino/prompt"
)
//BPNAPI is the API link of bahaiprayers.net
@ -96,8 +98,8 @@ type PrayerCode struct { @@ -96,8 +98,8 @@ type PrayerCode struct {
Language string
}
var Languages []Language
var CodeList map[string][]int
var Languages map[int]Language
var CodeList map[int]string
var PrayersWithCode map[PrayerCode]Prayer
// ReadLangCSV reads rel/lang.csv and puts it in Languages
@ -113,7 +115,8 @@ func ReadLangCSV() error { @@ -113,7 +115,8 @@ func ReadLangCSV() error {
if err != nil {
return err
}
for _, lang := range langCSV {
Languages = make(map[int]Language)
for _, lang := range langCSV[1:] {
var language Language
language.Id, _ = strconv.Atoi(lang[0])
language.Iso = lang[1]
@ -122,7 +125,11 @@ func ReadLangCSV() error { @@ -122,7 +125,11 @@ func ReadLangCSV() error {
language.English = lang[4]
language.FlagLink = lang[5]
language.Rtl, _ = strconv.ParseBool(lang[6])
Languages = append(Languages, language)
Languages[language.Id] = language
}
fmt.Println("Number of languages:", len(Languages))
for _, language := range Languages {
fmt.Print(language.Iso + " - ")
}
return nil
}
@ -133,51 +140,66 @@ func ReadCodeList() error { @@ -133,51 +140,66 @@ func ReadCodeList() error {
return err
}
defer file.Close()
CodeList = make(map[string][]int)
CodeList = make(map[int]string)
reader := csv.NewReader(file)
reader.FieldsPerRecord = -1
for {
line, err := reader.Read()
if err != nil {
break
}
CodeList[line[0]] = make([]int, 0)
for _, prayerIDstr := range line[1:] {
prayerID, err := strconv.Atoi(prayerIDstr)
if err != nil {
return err
}
CodeList[line[0]] = append(CodeList[line[0]], prayerID)
CodeList[prayerID] = line[0]
}
}
fmt.Println("Number of prayer codes:", len(CodeList))
return nil
}
func WriteCodeList(codeList map[string][]int) error {
func WriteCodeList(codeList map[int]string) error {
file, err := os.Create("rel/code.list")
if err != nil {
return err
}
defer file.Close()
writer := csv.NewWriter(file)
for code, prayerIDs := range codeList {
line := make([]string, 0)
line = append(line, code)
invertedCodeList := make(map[string][]int)
for prayerID, code := range codeList {
invertedCodeList[code] = append(invertedCodeList[code], prayerID)
}
for code, prayerIDs := range invertedCodeList {
line := []string{code}
for _, prayerID := range prayerIDs {
line = append(line, strconv.Itoa(prayerID))
}
writer.Write(line)
err := writer.Write(line)
if err != nil {
return err
}
}
writer.Flush()
return nil
}
func AskLanguages() []Language {
func AskLanguages(pr string) []Language {
// Ask "Which languages do you want to complete?"
// The answer is a list of language codes that must be converted to numbers
// using the conversion from lang.csv in Languages
fmt.Print("Which languages do you want to complete? ")
fmt.Print(pr)
var languages []string
fmt.Scanln(&languages)
for {
fmt.Print("Language name or code, leave blank to continue: ")
s := prompt.MustRead[string]()
if s == "" {
break
}
languages = append(languages, s)
}
var outLangs []Language
for _, language := range languages {
for _, lang := range Languages {
@ -189,36 +211,24 @@ func AskLanguages() []Language { @@ -189,36 +211,24 @@ func AskLanguages() []Language {
return outLangs
}
func Code(p Prayer) string {
// Get the prayer code for a prayer via CodeList
// or return an empty string if it's not on the list
for code, prayerIDs := range CodeList {
for _, prayerID := range prayerIDs {
if p.Id == prayerID {
return code
}
}
}
return ""
}
func ReadPrayers(lang []Language) []Prayer {
func ReadPrayers(lang []Language, codep bool) []Prayer {
var prayers []Prayer
for _, language := range lang {
response, err := http.Get(BPNAPI + strconv.Itoa(language.Id))
if err != nil {
fmt.Println(err)
return nil
panic("Could not get prayers, abort")
}
defer response.Body.Close()
var output BPNAPIOutput
err = json.NewDecoder(response.Body).Decode(&output)
if err != nil {
fmt.Println(err)
return nil
fmt.Println("Issue when reading " + language.English + " prayers: " + err.Error())
continue
}
fmt.Print(language.Iso + "..")
for _, prayer := range output.Prayers {
if Code(prayer) == "" {
if (CodeList[prayer.Id] != "") == codep {
prayers = append(prayers, prayer)
}
}
@ -235,21 +245,22 @@ func main() { @@ -235,21 +245,22 @@ func main() {
if err != nil {
panic(err)
}
// Iterate over all languages and read in all prayers
// Ask which languages to use as a reference and read in all prayers
// with a language code to PrayersWithCode
for _, language := range Languages {
prayers := ReadPrayers([]Language{language})
for _, prayer := range prayers {
code := Code(prayer)
if code != "" {
prayerCode := PrayerCode{code, language.Iso}
PrayersWithCode[prayerCode] = prayer
}
refLanguages := AskLanguages("Which languages do you want to reference to? ")
prayers := ReadPrayers(refLanguages, true)
PrayersWithCode = make(map[PrayerCode]Prayer)
for _, prayer := range prayers {
code := CodeList[prayer.Id]
if code != "" {
PrayersWithCode[PrayerCode{code, Languages[prayer.LanguageId].Iso}] = prayer
}
}
fmt.Println("Number of prayers with code: " + strconv.Itoa(len(PrayersWithCode)))
// Ask which language to complete
languages := AskLanguages("Which languages do you want to complete? ")
prayers = ReadPrayers(languages, false)
for {
languages := AskLanguages()
prayers := ReadPrayers(languages)
// randomize the order of the prayers
for i := len(prayers) - 1; i > 0; i-- {
j := rand.Intn(i + 1)
@ -260,7 +271,7 @@ func main() { @@ -260,7 +271,7 @@ func main() {
var prayer Prayer
var code string
for _, p := range prayers {
if Code(p) == "" {
if CodeList[p.Id] == "" {
prayer = p
break
}
@ -271,9 +282,16 @@ func main() { @@ -271,9 +282,16 @@ func main() {
fmt.Println("Author:", prayer.Author())
for code == "" {
// Ask for a keyword
fmt.Print("Input a keyword for this prayer: ")
var keyword string
fmt.Scanln(&keyword)
fmt.Print("Input a keyword for this prayer, skip to pick another one or code to enter the code manually: ")
keyword := prompt.MustRead[string]()
if keyword == "skip" {
break
}
if keyword == "code" {
fmt.Print("Enter the code: ")
code = prompt.MustRead[string]()
break
}
var Matches []Prayer
// Check for the prayer text of each prayer in
// PrayersWithCode if there is a match with the keyword
@ -289,28 +307,28 @@ func main() { @@ -289,28 +307,28 @@ func main() {
continue
}
// Ask which of the matches to use
fmt.Println("Found " + strconv.Itoa(len(Matches)) + " matches.")
fmt.Println("Which of the following matches?")
for i, match := range Matches {
fmt.Println(i+1, ":", match.Text)
fmt.Print("Does this match? (y/n) ")
var answer string
fmt.Scanln(&answer)
answer := prompt.MustRead[string]()
if answer == "y" {
prayer = match
code = Code(match)
fmt.Println(CodeList[match.Id])
code = CodeList[match.Id]
break
}
}
}
// Add the code to CodeList
CodeList[code] = append(CodeList[code], prayer.Id)
CodeList[prayer.Id] = code
fmt.Println("Added code ", " to prayer ", prayer.Id)
// Ask if the user wants to identify another prayer
// or if they want to quit
// To identify another prayer, continue
// To quit, save the CodeList and quit
fmt.Print("Identify another prayer? (y/n) ")
var answer string
fmt.Scanln(&answer)
answer := prompt.MustRead[string]()
if answer == "n" {
err = WriteCodeList(CodeList)
if err != nil {

Loading…
Cancel
Save