-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcards.go
85 lines (71 loc) · 1.91 KB
/
cards.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
package main
import (
"encoding/json"
"html"
"io/ioutil"
"net/http"
"strings"
)
const cardsURL = "https://raw.githubusercontent.com/samurailink3/hangouts-against-humanity/master/source/data/cards.js"
type card struct {
ID int `json:"id"`
Text string `json:"text"`
NumAnswers int `json:"numAnswers"`
}
// holds the json from the url above
type masterCard struct {
card
CardType string `json:"cardType"`
Expansion string `json:"expansion"`
}
type cardBox struct {
questions []questionCard
answers []answerCard
}
type questionCard struct {
card
}
type answerCard struct {
card
}
func getCardsFromWeb() (*cardBox, error) {
resp, err := http.DefaultClient.Get(cardsURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
body := string(b)
body = strings.Replace(body, "masterCards = ", "", 1)
body = strings.Replace(body, "\\'", "'", -1)
body = strings.Replace(body, "\\”", "\\\"", -1)
var masterCards []masterCard
err = json.Unmarshal([]byte(body), &masterCards)
if err != nil {
return nil, err
}
var questions []questionCard
var answers []answerCard
for _, c := range masterCards {
c.Text = html.UnescapeString(c.Text)
c.Text = strings.Replace(c.Text, "<br>", "", -1)
c.Text = strings.Replace(c.Text, "<b>", "", -1)
c.Text = strings.Replace(c.Text, "</b>", "", -1)
c.Text = strings.Replace(c.Text, "<i>", "", -1)
c.Text = strings.Replace(c.Text, "</i>", "", -1)
c.Text = strings.Replace(c.Text, "<u>", "", -1)
c.Text = strings.Replace(c.Text, "</u>", "", -1)
c.Text = strings.Replace(c.Text, " ", " ", -1)
switch c.CardType {
case "Q":
questions = append(questions, questionCard{c.card})
case "A":
c.card.Text = strings.TrimRight(c.card.Text, ".")
answers = append(answers, answerCard{c.card})
}
}
return &cardBox{questions: questions, answers: answers}, nil
}