2019-06-09 13:05:36 +00:00
|
|
|
package request
|
|
|
|
|
|
|
|
import (
|
2019-06-13 17:11:42 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fatih/structs"
|
2019-06-09 13:05:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func url(path string) string {
|
2019-06-13 17:11:42 +00:00
|
|
|
return string("https://forum.0cd.xyz/" + path)
|
2019-06-09 13:05:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 17:11:42 +00:00
|
|
|
// Request sends GET to path
|
|
|
|
func Request(path string) []byte {
|
|
|
|
req, err := http.NewRequest("GET", url(path), nil)
|
2019-06-09 18:05:18 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error reading request. ", err)
|
2019-06-13 17:11:42 +00:00
|
|
|
}
|
2019-06-09 18:05:18 +00:00
|
|
|
|
2019-06-13 17:11:42 +00:00
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("Api-Key", "57dc92f574f7f400e1d12670940c19930a1f85b78485ca25ac96d96590dc7f99")
|
|
|
|
req.Header.Set("Api-Username", "Michael")
|
2019-06-09 18:05:18 +00:00
|
|
|
|
2019-06-13 17:11:42 +00:00
|
|
|
client := &http.Client{Timeout: time.Second * 10}
|
2019-06-09 18:05:18 +00:00
|
|
|
|
2019-06-13 17:11:42 +00:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2019-06-09 18:05:18 +00:00
|
|
|
log.Fatal("Error reading request. ", err)
|
2019-06-13 17:11:42 +00:00
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2019-06-09 18:05:18 +00:00
|
|
|
log.Fatal("Error reading request. ", err)
|
2019-06-13 17:11:42 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2019-06-09 18:05:18 +00:00
|
|
|
|
|
|
|
//fmt.Println("response Status:", resp.Status)
|
|
|
|
//fmt.Println("response Headers:", resp.Header)
|
2019-06-13 17:11:42 +00:00
|
|
|
//fmt.Printf("%s %s %d\n", resp.Request.Method, url("about.json"), resp.StatusCode)
|
2019-06-09 13:05:36 +00:00
|
|
|
|
2019-06-13 17:11:42 +00:00
|
|
|
if resp.StatusCode == 200 {
|
|
|
|
/*var result map[string]interface{}
|
|
|
|
json.Unmarshal([]byte(body), &result)
|
|
|
|
return result*/
|
|
|
|
return body
|
|
|
|
}
|
|
|
|
return nil
|
2019-06-09 13:05:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 17:11:42 +00:00
|
|
|
// About gets json data from about page
|
|
|
|
func About() map[string]interface{} {
|
|
|
|
var about AutoGen
|
|
|
|
json.Unmarshal(Request("about"), &about)
|
|
|
|
for i := 0; i < len(about.About.Admins); i++ {
|
|
|
|
about.About.Admins[i].AvatarTemplate = strings.ReplaceAll(about.About.Admins[i].AvatarTemplate, "{size}", "120")
|
|
|
|
}
|
|
|
|
//return about
|
|
|
|
m := structs.Map(about)
|
|
|
|
return m
|
2019-06-09 13:05:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 17:11:42 +00:00
|
|
|
/*// GetTopics gets topic list from tag
|
|
|
|
func GetTopics(path string) *TagTopics {
|
|
|
|
var topics TagTopics
|
|
|
|
json.Unmarshal(Request(path), &topics)
|
|
|
|
return &topics
|
|
|
|
}*/
|
2019-06-09 13:05:36 +00:00
|
|
|
|
2019-06-13 17:11:42 +00:00
|
|
|
// Topics n/a
|
2019-06-09 13:05:36 +00:00
|
|
|
func Topics(path string) {
|
2019-06-13 17:11:42 +00:00
|
|
|
}
|