-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
175 lines (146 loc) · 4.04 KB
/
main.go
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
package main
import (
"database/sql"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"time"
_ "github.com/mattn/go-sqlite3"
)
const chuckAPI = "http://api.chucknorris.io/jokes/random"
var home = os.Getenv("HOME")
var dbLocation = home + "/.jokesdb"
// Chucknorris is the struct used to unmarshal the JSON response from the URL
type Chucknorris struct {
Category []string `json:"category"`
IconURL string `json:"icon_url"`
ID string `json:"id"`
URL string `json:"url"`
Value string `json:"value"`
}
/* getJokes takes the API url as the parameter and fetch jokes from it,
* here we have assumed it to be of ChuckNorris type
*/
//TODO: Remove the dependency on the hardcoded struct
func getJokes(URL string) (string, error) {
req, err := http.NewRequest("GET", URL, nil)
if err != nil {
return "", fmt.Errorf("No request formed %v", err)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("No response: %v", err)
}
defer resp.Body.Close()
respData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("Read error")
}
var joke Chucknorris
if err = json.Unmarshal(respData, &joke); err != nil {
return "", fmt.Errorf("Error in unmarsheling, %v", err)
}
return joke.Value, nil
}
// Check if the Database exist if it does, then flushes it off
func deleteExistingDatabase(name string) error {
_, err := os.Stat(name)
if !os.IsNotExist(err) {
if err := os.Remove(name); err != nil {
return fmt.Errorf("Can't Delete file: %v", err)
}
}
return nil
}
/*
* cacheUpJokes queries for the joke and stores it in the Database, the parameter it is,
* numberOfJokes, i.e the number of jokes that has to be stored, every joke has a joke id(jid)
* and the joke itself, this is stored in the db.
*/
func cacheUpJokes(numberOfJokes int) error {
if err := deleteExistingDatabase(dbLocation); err != nil {
return fmt.Errorf("Cannot delete old database, %v", err)
}
db, err := sql.Open("sqlite3", dbLocation)
if err != nil {
return fmt.Errorf("Couldn't make DB, %v", err)
}
defer db.Close()
_, err = db.Exec("CREATE TABLE jokes (jid INTEGER PRIMARY KEY, joke VARCHAR(64) NULL)")
if err != nil {
return fmt.Errorf("Table can't be created, %v", err)
}
stmt, err := db.Prepare("INSERT INTO jokes(jid, joke) values(?,?)")
if err != nil {
return fmt.Errorf("Insert statement couldn't be formed: %v", err)
}
for i := 1; i <= numberOfJokes; i++ {
joke, err := getJokes(chuckAPI)
if err != nil {
return fmt.Errorf("Jokes couldn't be fetched, %v", err)
}
_, err = stmt.Exec(i, joke)
if err != nil {
return fmt.Errorf("Could'nt make record entry: %v", err)
}
}
return nil
}
func fetchJoke() (string, error) {
db, err := sql.Open("sqlite3", dbLocation)
defer db.Close()
var count int
var joke string
if err != nil {
return "", fmt.Errorf("Couldn't make DB, %v", err)
}
totalRow, err := db.Query("SELECT count(*) FROM jokes")
if err != nil {
return "", fmt.Errorf("Can't get number of rows, is the db created run chuck --index=5")
}
for totalRow.Next() {
err = totalRow.Scan(&count)
if err != nil {
return "", fmt.Errorf("Rows can't be read")
}
}
rand.Seed(time.Now().Unix())
randNum := rand.Intn(count-1) + 1
stm, err := db.Prepare("SELECT joke FROM jokes where jid = ?")
if err != nil {
return "", fmt.Errorf("Can't Prepaer statement")
}
res, err := stm.Query(randNum)
if err != nil {
return "", fmt.Errorf("Not able to fetch jokes")
}
for res.Next() {
if err = res.Scan(&joke); err != nil {
return "", fmt.Errorf("No jokes found")
}
}
return joke, nil
}
func main() {
index := flag.Int("index", -1, "To cache up Chuck facts, the parameter decide the number of jokes to cache")
flag.Parse()
if *index > 0 {
if err := cacheUpJokes(*index); err != nil {
log.Fatalf("Database issue: %v", err)
os.Exit(2)
}
} else {
joke, err := fetchJoke()
if err != nil {
fmt.Printf("No jokes found did you cache it by chuck --index=5 %v", err)
os.Exit(2)
}
fmt.Println(joke)
}
}