request/api.go

218 lines
5.3 KiB
Go
Raw Normal View History

2019-06-09 13:05:36 +00:00
package request
import (
"encoding/json"
2019-07-04 04:27:41 +00:00
"fmt"
2019-06-20 00:33:48 +00:00
"html"
"io/ioutil"
"net/http"
"strings"
"time"
2019-06-26 01:56:55 +00:00
"0cd.xyz-go/logger"
"github.com/fatih/structs"
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
}
2019-07-04 04:27:41 +00:00
// HTTPResp Async HTTP Request
type HTTPResp struct {
ID int
Resp []byte
Err logger.HTTPError
}
// AsyncGet sends Async GET requests
func AsyncGet(urls map[int]string) ([]*HTTPResp, *logger.HTTPError) {
ch := make(chan *HTTPResp)
response := []*HTTPResp{}
for id, url := range urls {
go func(i int, u string) {
resp, err := Request("t/" + u)
err = logger.HTTPErr(http.StatusOK)
ch <- &HTTPResp{i, resp, *err}
}(id, url)
}
loop:
for {
select {
case r := <-ch:
response = append(response, r)
if len(response) == len(urls) {
break loop
}
case <-time.After(120 * time.Millisecond):
fmt.Printf(".")
//return nil, logger.HTTPErr(http.StatusRequestTimeout)
}
}
return response, nil
}
// Request sends GET to path
2019-06-26 01:56:55 +00:00
func Request(path string) ([]byte, *logger.HTTPError) {
req, err := http.NewRequest("GET", url(path), nil)
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-07-05 15:47:28 +00:00
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
ap := Option().Options
for _, api := range ap.API {
req.Header.Set(api.Name, api.Value)
}
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
fmt.Println(err)
return nil, logger.HTTPErr(http.StatusInternalServerError)
}
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
fmt.Println(err)
return nil, logger.HTTPErr(http.StatusInternalServerError)
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
2019-06-20 00:33:48 +00:00
return body, nil
}
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-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
}
// 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-07-04 04:27:41 +00:00
// GetTopics2 gets topic list from tag
func GetTopics2(path string) (m map[int]string) {
resp, err := Request("tags/" + path)
if err != nil {
return
}
var topics TagTopics
json.Unmarshal(resp, &topics)
m = make(map[int]string)
for _, topics := range topics.TopicList.Topics {
m[topics.ID] = topics.Slug
}
return
}
// AsyncTopics request for topics using async
func AsyncTopics(path string) map[string]interface{} {
var topic TopicsList
resp, err := AsyncGet(GetTopics2(path))
if err != nil {
m := structs.Map(err)
return m
}
for _, topics := range resp {
var t Topic
json.Unmarshal(topics.Resp, &t)
for i := 0; i < len(t.PostStream.Posts); i++ {
if t.PostStream.Posts[i].PostNumber != 1 {
t.PostStream.Posts[i].Cooked = ""
}
}
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
}
// 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
}