-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
186 lines (160 loc) · 5.97 KB
/
main.go
File metadata and controls
186 lines (160 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
)
// Verse Struct that determines how the data is formatted
type Verse struct {
Data struct {
BookID string `json:"bookId"`
ChapterID string `json:"chapterId"`
Reference string `json:"reference"`
Content []struct {
Items []struct {
Text string `json:"text"`
Type string `json:"type"`
} `json:"items"`
} `json:"content"`
} `json:"data"`
}
// Print function to keep the type squeaky clean
func (v Verse) String() string {
return fmt.Sprintf("Book: %s\nChapter: %s\nReference: %s\nVerse: %s\n",
v.Data.BookID,
v.Data.ChapterID,
v.Data.Reference,
fetchVerseText(v),
)
}
func fetchVerseData(verseID string) (Verse, error) {
//url with the verseID spliced in
// verse ID format is BOK.chrNUM.verNUM see quiz_verses for more examples
url := fmt.Sprintf("https://api.scripture.api.bible/v1/bibles/01b29f4b342acc35-01/verses/%s?content-type=json", verseID)
// Fetches the verse
req, err := http.NewRequest("GET", url, nil)
// Returns blank Verse Type and error if req fails
if err != nil {
return Verse{}, err
}
// API Key Header
req.Header.Set("api-key", os.Getenv("Bible"))
//Data is sent in JSON
req.Header.Set("accept", "application/json")
// Inits the client and sends the request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return Verse{}, err
}
// Doesnt cont until response
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(resp.Body)
// Parses the response
body, err := io.ReadAll(resp.Body)
// If response cannot be parsed return blank verse and err
if err != nil {
return Verse{}, err
}
// Creates empty verse
var verse Verse
// Parses the JSON and punches it into the verse
if err := json.Unmarshal(body, &verse); err != nil {
return Verse{}, err
}
// Returns the verse and nil
return verse, nil
}
func fetchVerseText(verseData Verse) string {
var fullText string
// Creates empty string
// Attaches all the contents and items together
for _, content := range verseData.Data.Content {
for _, item := range content.Items {
// Builds the complete verse
fullText += item.Text
}
}
// Returns the complete verse
return fullText
}
func fullVerseList(filename string) []Verse {
// Empty list for Verse Types
var verseList []Verse
// Opens the verse file
quizFile, _ := os.ReadFile(filename)
// For each line split off the \n
for _, line := range strings.Split(string(quizFile), "\n") {
// Fetch the verse data by calling fetchVerseData
verse, _ := fetchVerseData(line)
// Append it to the list
verseList = append(verseList, verse)
}
// Return the list
return verseList
}
func cert() {
cert :=
`===================== ======================= %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
======================== ======================= %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
========================== ======================= %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
=========================== ======================= %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
=========================== ======================= %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
==================== =========== %%%%%%%%
============= =========== %%%%%%%%
============ =========== =============== %%%%%%%%
====================- =========================================
========================================================================
======================================================================== ===========================
====================================================================================================
===================================================================================================
============================================ =======================================
%%% =================================== ========================================
%%%%%% ======= ============================================ =========
%%%%%%%% ============================================ =========
%%%%%%%% =========================================== =========
%%%%%%%% ========================================== =========
%%%%%%%% ======================================== =========
%%%%%%%% ===================================== =========
%%%%%%%% ============ =========
%%%%%%%% ============ =========
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ====================================
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@ ===================================
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ===================================
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ==================================
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% =================================
`
fmt.Println(cert)
}
func main() {
cert()
fmt.Println("\nFetching game data... ")
score := 0
var incorrectVerses []Verse
verses := fullVerseList("quiz_verses")
for i := 0; i < len(verses); i++ {
fmt.Println(fetchVerseText(verses[i]))
fmt.Println("What Verse is this")
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
line := scanner.Text()
if line == verses[i].Data.Reference {
fmt.Println("Correct")
score++
} else {
fmt.Println("Wrong")
incorrectVerses = append(incorrectVerses, verses[i])
}
}
}
fmt.Println("You got the following wrong: ")
for i := 0; i < len(incorrectVerses); i++ {
fmt.Println(incorrectVerses[i])
}
}