-
Notifications
You must be signed in to change notification settings - Fork 0
/
chive.go
148 lines (139 loc) · 4.25 KB
/
chive.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
// Copyright 2015 The tgbot-ng Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"io/ioutil"
"math/rand"
"net/http"
"regexp"
"strconv"
"log"
)
type CmdConfigChive struct {
Reg *regexp.Regexp
Enabled bool `json:"enable"`
ApiKey string `json:"api_key"`
}
var chiveURLs []string
var chiveCaptions []string
func NewCmdChive(config *CmdConfigChive) {
config.Reg = regexp.MustCompile(`^/chive(?:(@[a-zA-Z0-9_]{1,20}bot)?( refill)?$)`)
// config.Reg = regexp.MustCompile(`^/chive(?:(@[a-zA-Z0-9_]+bot)?( .+)?$)`)
}
func ChiveRun(in string) (string){
if (in == "/chive refill"){
log.Println("Refilling the Chive pool because of a request")
chiveURLs = []string{}
chiveCaptions = []string{}
}
if len(chiveURLs)==0{
ChiveRefill()
}else if (len(chiveURLs)<6){
log.Println("Refilling the Chive pool...")
go ChiveRefill()
}
if len(chiveURLs)==0{
return "Chive Error: No pics in the pool!"
}
picUrl := chiveURLs[0]
chiveURLs = chiveURLs[1:len(chiveURLs)]
picCaption := chiveCaptions[0]
chiveCaptions = chiveCaptions[1:len(chiveCaptions)]
// resp, err := http.Get(picUrl)
// if err != nil {
// return "", err
// }
// defer resp.Body.Close()
// imgData, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// return "", err
// }
return "Keep calm and chive on... " + picCaption + "\n" + picUrl
}
func ChiveRefill(){
type T_img struct{
URL string
}
type Attachment struct {
Caption string
Image T_img
}
type Item struct {
Attachments []Attachment
}
type t_posts struct {
Items []Item
}
client := &http.Client{}
req, err := http.NewRequest("GET", "https://api4.thechive.com/api4/category?category_id=10665&page=1&key=" + Commandssl.Chive.ApiKey, nil)
if err != nil {
log.Println(err)
return
}
req.Header.Set("User-Agent", "okhttp/4.9.1")
resp, err := client.Do(req)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Println("HTTP error: " + resp.Status + " " + strconv.Itoa(resp.StatusCode))
return
}
repBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return
}
posts := t_posts{}
err = json.Unmarshal(repBody, &posts)
if err != nil {
log.Println(err)
return
}
item_num := rand.Intn(len(posts.Items))
// log.Println("len(post.Items) = ", len(posts.Items), "item_num = ", item_num)
for i := len(posts.Items[item_num].Attachments); i>0; i-- {
attach_num := rand.Intn(i)
// log.Println("attach_num = ", attach_num, " ; i = ", i, " len Attachments = ", len(posts.Items[item_num].Attachments))
item := posts.Items[item_num].Attachments[attach_num]
chiveURLs = append(chiveURLs, item.Image.URL)
chiveCaptions = append(chiveCaptions, item.Caption)
posts.Items[item_num].Attachments[attach_num] = posts.Items[item_num].Attachments[len(posts.Items[item_num].Attachments)-1]
posts.Items[item_num].Attachments = posts.Items[item_num].Attachments[:len(posts.Items[item_num].Attachments)-1]
}
/* //Coger un post aleatorio de la página aleatoria seleccionada
randPost := rand.Intn(40)
if len(category.Posts) < randPost+1 {
log.Println("Posts argument empty!")
return
}
postNum := category.Posts[randPost].Guid
resp, err = http.Get("http://api.thechive.com/api/post/" + strconv.Itoa(postNum) + "?key=" + Commandssl.Chive.ApiKey)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Println("HTTP error: " + resp.Status + " " + strconv.Itoa(resp.StatusCode))
return
}
repBody, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return
}
err = json.Unmarshal(repBody, &post)
if err != nil {
log.Println(err)
return
}
//Cojo todas las fotos
for _, url := range post.Posts[0].Items{
chiveURLs = append(chiveURLs, url.Identity.URL)
}*/
}