request/api.go

160 lines
3.7 KiB
Go
Raw Normal View History

2019-06-09 13:05:36 +00:00
package request
import (
"encoding/json"
2019-06-20 00:33:48 +00:00
"fmt"
"html"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/fatih/structs"
2019-06-09 13:05:36 +00:00
)
2019-06-20 00:33:48 +00:00
type HTTPError struct {
Status string
StatusCode int
}
2019-06-09 13:05:36 +00:00
func url(path string) string {
return string("https://forum.0cd.xyz/" + path)
2019-06-09 13:05:36 +00:00
}
// Request sends GET to path
2019-06-20 00:33:48 +00:00
func Request(path string) ([]byte, *HTTPError) {
req, err := http.NewRequest("GET", url(path), nil)
if err != nil {
2019-06-20 00:33:48 +00:00
log.Println("Error reading request. ", err)
return nil, &HTTPError{Status: "500 Internal Server Error", StatusCode: 500}
}
2019-06-20 00:33:48 +00:00
head, er := Header()
if er != nil {
return nil, er
}
for _, headers := range head.Headers {
req.Header.Set(headers.Name, headers.Value)
}
client := &http.Client{Timeout: time.Second * 10}
resp, err := client.Do(req)
if err != nil {
2019-06-20 00:33:48 +00:00
log.Println("Error reading request. ", err)
return nil, &HTTPError{Status: "500 Internal Server Error", StatusCode: 500}
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
2019-06-20 00:33:48 +00:00
log.Println("Error reading request. ", err)
return nil, &HTTPError{Status: "500 Internal Server Error", StatusCode: 500}
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
2019-06-20 00:33:48 +00:00
return body, nil
}
2019-06-20 00:33:48 +00:00
fmt.Printf("Request Error: GET %d %s\n", resp.StatusCode, html.EscapeString(url(path)))
return nil, &HTTPError{Status: resp.Status, StatusCode: 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
}
// Category returns category json data
func CategoryTopic(path string) map[string]interface{} {
var topics CategoryTopics
2019-06-20 00:33:48 +00:00
resp, err := Request("/c/" + path)
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
}
// About gets json data from about page
func About() map[string]interface{} {
var about AutoGen
2019-06-20 00:33:48 +00:00
resp, err := Request("about")
if err != nil {
m := structs.Map(err)
return m
}
json.Unmarshal(resp, &about)
for i := 0; i < len(about.About.Admins); i++ {
about.About.Admins[i].AvatarTemplate = strings.ReplaceAll(about.About.Admins[i].AvatarTemplate, "{size}", "120")
}
m := structs.Map(about)
return m
2019-06-09 13:05:36 +00:00
}
2019-06-18 01:48:55 +00:00
// 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-20 00:33:48 +00:00
func GetTopics(path string) (topics TagTopics, err *HTTPError) {
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
// Topics n/a
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-06-20 00:33:48 +00:00
resp, err := Request("/t/" + topics.Slug)
if err != nil {
m := structs.Map(err)
return m
}
json.Unmarshal(resp, &t)
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
}