2019-06-09 13:05:36 +00:00
|
|
|
package request
|
|
|
|
|
|
|
|
import (
|
2019-06-13 17:11:42 +00:00
|
|
|
"encoding/json"
|
2019-06-20 00:33:48 +00:00
|
|
|
"html"
|
2019-06-13 17:11:42 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2019-08-20 07:16:05 +00:00
|
|
|
"os"
|
2019-08-13 18:57:30 +00:00
|
|
|
"strconv"
|
2019-06-13 17:11:42 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-06-26 01:56:55 +00:00
|
|
|
"0cd.xyz-go/logger"
|
2019-06-13 17:11:42 +00:00
|
|
|
"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
|
2019-06-26 01:56:55 +00:00
|
|
|
func Request(path string) ([]byte, *logger.HTTPError) {
|
2019-06-13 17:11:42 +00:00
|
|
|
req, err := http.NewRequest("GET", url(path), nil)
|
2019-06-09 18:05:18 +00:00
|
|
|
if err != nil {
|
2019-06-26 01:56:55 +00:00
|
|
|
logger.ErrorLog("Error reading request. ", err)
|
2019-07-04 04:27:41 +00:00
|
|
|
return nil, logger.HTTPErr(http.StatusInternalServerError)
|
2019-06-13 17:11:42 +00:00
|
|
|
}
|
2019-06-09 18:05:18 +00:00
|
|
|
|
2019-07-05 15:47:28 +00:00
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2019-08-20 07:16:05 +00:00
|
|
|
if os.Getenv("GOENV") != "production" {
|
|
|
|
resp := API()
|
|
|
|
for _, api := range resp.API {
|
|
|
|
req.Header.Set(api.Name, api.Value)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
req.Header.Set("Api-Key", os.Getenv("GOAPIKEY"))
|
|
|
|
req.Header.Set("Api-Username", os.Getenv("APIUSERNAME"))
|
2019-06-18 03:30:01 +00:00
|
|
|
}
|
2019-06-09 18:05:18 +00:00
|
|
|
|
2019-06-13 17:11:42 +00:00
|
|
|
client := &http.Client{Timeout: time.Second * 10}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2019-06-26 01:56:55 +00:00
|
|
|
logger.ErrorLog("Error reading request. ", err)
|
2019-07-04 04:27:41 +00:00
|
|
|
return nil, logger.HTTPErr(http.StatusInternalServerError)
|
2019-06-13 17:11:42 +00:00
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2019-06-26 01:56:55 +00:00
|
|
|
logger.ErrorLog("Error reading request. ", err)
|
2019-07-04 04:27:41 +00:00
|
|
|
return nil, logger.HTTPErr(http.StatusInternalServerError)
|
2019-06-13 17:11:42 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2019-06-09 18:05:18 +00:00
|
|
|
|
2019-06-13 17:11:42 +00:00
|
|
|
if resp.StatusCode == 200 {
|
2019-06-20 00:33:48 +00:00
|
|
|
return body, nil
|
2019-06-13 17:11:42 +00:00
|
|
|
}
|
2019-06-26 01:56:55 +00:00
|
|
|
logger.GetLog("GET %d %s\n", resp.StatusCode, html.EscapeString(url(path)))
|
2019-07-04 04:27:41 +00:00
|
|
|
return nil, logger.HTTPErr(resp.StatusCode)
|
2019-06-09 13:05:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 01:48:55 +00:00
|
|
|
// Category returns category json data
|
|
|
|
func Category() map[string]interface{} {
|
|
|
|
var category Categories
|
2019-06-20 00:33:48 +00:00
|
|
|
resp, err := Request("categories")
|
|
|
|
if err != nil {
|
|
|
|
m := structs.Map(err)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
json.Unmarshal(resp, &category)
|
2019-06-18 01:48:55 +00:00
|
|
|
m := structs.Map(category)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2019-06-27 00:41:18 +00:00
|
|
|
// CategoryTopic returns category json data
|
2019-06-18 01:48:55 +00:00
|
|
|
func CategoryTopic(path string) map[string]interface{} {
|
|
|
|
var topics CategoryTopics
|
2019-09-11 11:18:12 +00:00
|
|
|
resp, err := Request("c/" + path)
|
2019-06-20 00:33:48 +00:00
|
|
|
if err != nil {
|
|
|
|
m := structs.Map(err)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
json.Unmarshal(resp, &topics)
|
2019-06-18 01:48:55 +00:00
|
|
|
m := structs.Map(topics)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tag returns tags json data
|
|
|
|
func Tag() map[string]interface{} {
|
|
|
|
var tags Tags
|
2019-06-20 00:33:48 +00:00
|
|
|
resp, err := Request("tags")
|
|
|
|
if err != nil {
|
|
|
|
m := structs.Map(err)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
json.Unmarshal(resp, &tags)
|
2019-06-18 01:48:55 +00:00
|
|
|
m := structs.Map(tags)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTopics gets topic list from tag
|
2019-06-26 01:56:55 +00:00
|
|
|
func GetTopics(path string) (topics TagTopics, err *logger.HTTPError) {
|
2019-06-20 00:33:48 +00:00
|
|
|
resp, err := Request("tags/" + path)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
json.Unmarshal(resp, &topics)
|
2019-06-18 01:48:55 +00:00
|
|
|
return
|
|
|
|
}
|
2019-06-09 13:05:36 +00:00
|
|
|
|
2019-08-12 03:23:17 +00:00
|
|
|
// Topics requests topics
|
2019-06-18 01:48:55 +00:00
|
|
|
func Topics(path string) map[string]interface{} {
|
|
|
|
var topic TopicsList
|
2019-06-20 00:33:48 +00:00
|
|
|
resp, err := GetTopics(path)
|
|
|
|
if err != nil {
|
|
|
|
m := structs.Map(err)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
for _, topics := range resp.TopicList.Topics {
|
2019-06-18 01:48:55 +00:00
|
|
|
var t Topic
|
2019-08-13 18:57:30 +00:00
|
|
|
resp, err := Request("t/" + topics.Slug + "/" + strconv.Itoa(topics.ID))
|
2019-06-20 00:33:48 +00:00
|
|
|
if err != nil {
|
|
|
|
m := structs.Map(err)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
json.Unmarshal(resp, &t)
|
2019-06-18 03:30:01 +00:00
|
|
|
for i := 0; i < len(t.PostStream.Posts); i++ {
|
|
|
|
if t.PostStream.Posts[i].PostNumber != 1 {
|
|
|
|
t.PostStream.Posts[i].Cooked = ""
|
2019-06-18 01:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Details.CreatedBy.AvatarTemplate = strings.ReplaceAll(t.Details.CreatedBy.AvatarTemplate, "{size}", "120")
|
|
|
|
s := strings.SplitAfter(t.PostStream.Posts[0].Cooked, "</p>")
|
|
|
|
t.PostStream.Posts[0].Cooked = s[0]
|
|
|
|
r := strings.ReplaceAll(t.PostStream.Posts[0].Cooked, "href=\"/u/", "href=\""+url("")+"u/")
|
|
|
|
t.PostStream.Posts[0].Cooked = r
|
|
|
|
ts, _ := time.Parse("2006-01-02T15:04:05Z07:00", t.CreatedAt)
|
|
|
|
t.CreatedAt = ts.Format("January 2, 2006")
|
|
|
|
topic.Topic = append(topic.Topic, t)
|
|
|
|
}
|
|
|
|
m := structs.Map(topic)
|
|
|
|
return m
|
2019-06-13 17:11:42 +00:00
|
|
|
}
|