summaryrefslogtreecommitdiffstats
path: root/frog-tipper.go
blob: f506b4f72bb8f1e971139af2e4eb288df96f0acb (plain)
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
// Copyright 2016 Robert S. Gerus. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package main

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/http/cookiejar"
	"sync"
	"time"
)

type tip struct {
	Number int    `json:"number"`
	Tip    string `json:"tip"`
}

type tips struct {
	Tips []tip `json:"tips"`
	lock sync.RWMutex
}

func (tips *tips) fetchTips() error {
	tips.lock.Lock()
	defer tips.lock.Unlock()

	data, err := httpGet("http://frog.tips/api/1/tips/")
	if err != nil {
		return err
	}

	err = json.Unmarshal(data, tips)
	if err != nil {
		return err
	}

	return nil
}

func (tips *tips) popTip() string {
	if len(tips.Tips) == 0 {
		if err := tips.fetchTips(); err != nil {
			return fmt.Sprint(err)
		}
	}

	tips.lock.RLock()
	defer tips.lock.RUnlock()

	rmsg := tips.Tips[len(tips.Tips)-1].Tip
	tips.Tips = tips.Tips[:len(tips.Tips)-1]

	return rmsg
}

func main() {
	var t tips
	for {
		fmt.Println(t.popTip())
	}
}

func httpGet(link string) ([]byte, error) {
	var buf []byte
	cj, err := cookiejar.New(nil)
	tr := &http.Transport{
		TLSHandshakeTimeout:   20 * time.Second,
		ResponseHeaderTimeout: 20 * time.Second,
	}
	client := &http.Client{
		Transport: tr,
		Jar:       cj,
	}

	req, err := http.NewRequest("GET", link, nil)
	if err != nil {
		return []byte{}, err
	}

	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36")

	resp, err := client.Do(req)
	if err != nil {
		return []byte{}, err
	}
	defer resp.Body.Close()

	// 5MiB
	if resp.ContentLength > 5*1024*1024 || resp.ContentLength < 0 {
		buf = make([]byte, 5*1024*1024)
	} else if resp.ContentLength == 0 {
		return []byte{}, nil
	} else {
		buf = make([]byte, resp.ContentLength)
	}

	i, err := io.ReadFull(resp.Body, buf)
	if err == io.ErrUnexpectedEOF {
		buf = buf[:i]
	} else if err != nil {
		return []byte{}, err
	}

	return buf, nil
}