Skip to content
This repository was archived by the owner on Jan 17, 2024. It is now read-only.

Commit 8b1349a

Browse files
committed
git@github.com:rabilrbl/TQuotes.git
1 parent c82f79c commit 8b1349a

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
quote
2+
quote.exe
3+
quote_test.sh

quote.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"net/http"
7+
"runtime"
8+
"strings"
9+
)
10+
11+
func main() {
12+
// Since the server gives error frequently
13+
// we try calling api again specific amount of time until we get value
14+
// res is to check if we have received value from api "res = 1"
15+
// count is to keep track of number of attempts to call api
16+
res, count := 0, 0
17+
// loop will run until we get proper quote from api till 15 attempts
18+
for res == 0 {
19+
quotes := quote() // quote function
20+
count += 1 //counting attempts
21+
// Check if received api value is not empty
22+
// and
23+
// Check if OS is Linux, Since i have applied text styling for linux terminals only
24+
if quotes["quoteText"] != "" && runtime.GOOS == "linux" {
25+
// Output for Linux
26+
res = 1 // make loop condition false
27+
print("\n\033[1m\033[36m" + quotes["quoteText"] + "✨\033[0m\n")
28+
if quotes["quoteAuthor"] != "" { // If author is not empty
29+
print(" - " + "\033[93m" + quotes["quoteAuthor"] + "\033[0m\n")
30+
} else { // author is found empty then Anonymous
31+
print(" - " + "\033[93m" + "Anonymous" + "\033[0m\n")
32+
}
33+
} else if quotes["quoteText"] != "" {
34+
// Output for OS other than linux
35+
res = 1
36+
print("\n" + quotes["quoteText"] + "✨\n")
37+
if quotes["quoteAuthor"] != "" {
38+
print(" - " + quotes["quoteAuthor"] + "\n")
39+
} else {
40+
print(" - " + "Anonymous\n")
41+
}
42+
}
43+
if count > 15 { // Terminate loop if retry attempts exceed 15 times
44+
res = 1 // make loop condition false
45+
print("\n Something went wrong. Try again! \n")
46+
}
47+
}
48+
}
49+
50+
// Fetch quotes from API and returns JSON like slices
51+
func quote() map[string]string {
52+
var client = &http.Client{}
53+
54+
var data = strings.NewReader(`method=getQuote&format=json&param=ms&lang=en`)
55+
56+
req, err := http.NewRequest("POST", "http://forismatic.com/api/1.0/", data)
57+
58+
if err != nil {
59+
fatal()
60+
}
61+
62+
req.Header.Set("Connection", "keep-alive")
63+
req.Header.Set("Pragma", "no-cache")
64+
req.Header.Set("Cache-Control", "no-cache")
65+
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62")
66+
req.Header.Set("DNT", "1")
67+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
68+
req.Header.Set("Accept", "*/*")
69+
req.Header.Set("Origin", "http://forismatic.com")
70+
req.Header.Set("Referer", "http://forismatic.com/en/")
71+
req.Header.Set("Accept-Language", "en,hi;q=0.5")
72+
req.Header.Set("sec-gpc", "1")
73+
74+
resp, err := client.Do(req)
75+
if err != nil {
76+
fatal()
77+
}
78+
defer resp.Body.Close()
79+
bodyText, err := ioutil.ReadAll(resp.Body)
80+
if err != nil {
81+
fatal()
82+
}
83+
values := make(map[string]string)
84+
85+
err = json.Unmarshal(bodyText, &values)
86+
if err != nil {
87+
fatal()
88+
}
89+
90+
return values
91+
}
92+
93+
func fatal() {
94+
count := 0
95+
for count > 3 {
96+
main()
97+
count++
98+
if count > 3 {
99+
print("\n Something went wrong. Try again! \n")
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)