overhaul of request and http system

This commit is contained in:
Michael 2019-06-18 02:48:55 +01:00
parent 889a4e7a8b
commit a6e5336913
2 changed files with 180 additions and 22 deletions

66
api.go
View File

@ -39,19 +39,28 @@ func Request(path string) []byte {
}
defer resp.Body.Close()
//fmt.Println("response Status:", resp.Status)
//fmt.Println("response Headers:", resp.Header)
//fmt.Printf("%s %s %d\n", resp.Request.Method, url("about.json"), resp.StatusCode)
if resp.StatusCode == 200 {
/*var result map[string]interface{}
json.Unmarshal([]byte(body), &result)
return result*/
return body
}
return nil
}
// Category returns category json data
func Category() map[string]interface{} {
var category Categories
json.Unmarshal(Request("categories"), &category)
m := structs.Map(category)
return m
}
// Category returns category json data
func CategoryTopic(path string) map[string]interface{} {
var topics CategoryTopics
json.Unmarshal(Request("/c/"+path), &topics)
m := structs.Map(topics)
return m
}
// About gets json data from about page
func About() map[string]interface{} {
var about AutoGen
@ -59,18 +68,45 @@ func About() map[string]interface{} {
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
}
/*// GetTopics gets topic list from tag
func GetTopics(path string) *TagTopics {
var topics TagTopics
json.Unmarshal(Request(path), &topics)
return &topics
}*/
// Tag returns tags json data
func Tag() map[string]interface{} {
var tags Tags
json.Unmarshal(Request("tags"), &tags)
m := structs.Map(tags)
return m
}
// GetTopics gets topic list from tag
func GetTopics(path string) (topics TagTopics) {
json.Unmarshal(Request("/tags/"+path), &topics)
return
}
// Topics n/a
func Topics(path string) {
func Topics(path string) map[string]interface{} {
topics := GetTopics(path)
var topic TopicsList
for i := 0; i < len(topics.TopicList.Topics); i++ {
var t Topic
json.Unmarshal(Request("/t/"+topics.TopicList.Topics[i].Slug), &t)
for j := 0; j < len(t.PostStream.Posts); j++ {
if t.PostStream.Posts[j].PostNumber != 1 {
t.PostStream.Posts[j].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
}

136
data.go
View File

@ -10,6 +10,7 @@ type Contacts struct {
Name string `json:"name"`
Address string `json:"address"`
Icon string `json:"icon"`
Image string `json:"image"`
} `json:"contacts"`
}
@ -56,6 +57,15 @@ type AutoGen struct {
} `json:"about"`
}
type Tags struct {
Tags []struct {
ID string `json:"id"`
Text string `json:"text"`
Count int `json:"count"`
PmCount int `json:"pm_count"`
} `json:"tags"`
}
// TagTopics list of topics via tag
type TagTopics struct {
TopicList struct {
@ -65,19 +75,24 @@ type TagTopics struct {
} `json:"topic_list"`
}
type TopicsList struct {
Topic []Topic
}
// Topic data
type Topic struct {
PostStream struct {
Posts []struct {
Cooked string `json:"cooked"`
UserTitle string `json:"user_title"`
Cooked string `json:"cooked"`
UserTitle string `json:"user_title"`
PostNumber int `json:"post_number"`
} `json:"posts"`
} `json:"post_stream"`
Tags []string `json:"tags"`
ID int `json:"id"`
Title string `json:"title"`
CreatedAt time.Time `json:"created_at"`
Slug string `json:"slug"`
Tags []string `json:"tags"`
ID int `json:"id"`
Title string `json:"title"`
CreatedAt string `json:"created_at"`
Slug string `json:"slug"`
Details struct {
CreatedBy struct {
ID int `json:"id"`
@ -87,3 +102,110 @@ type Topic struct {
} `json:"created_by"`
} `json:"details"`
}
type Categories struct {
CategoryList struct {
CanCreateCategory bool `json:"can_create_category"`
CanCreateTopic bool `json:"can_create_topic"`
Draft interface{} `json:"draft"`
DraftKey string `json:"draft_key"`
DraftSequence int `json:"draft_sequence"`
Categories []struct {
ID int `json:"id"`
Name string `json:"name"`
Color string `json:"color"`
TextColor string `json:"text_color"`
Slug string `json:"slug"`
TopicCount int `json:"topic_count"`
PostCount int `json:"post_count"`
Position int `json:"position"`
Description string `json:"description"`
DescriptionText string `json:"description_text"`
TopicURL string `json:"topic_url"`
ReadRestricted bool `json:"read_restricted"`
Permission int `json:"permission"`
NotificationLevel int `json:"notification_level"`
CanEdit bool `json:"can_edit"`
TopicTemplate string `json:"topic_template"`
HasChildren bool `json:"has_children"`
SortOrder string `json:"sort_order"`
SortAscending interface{} `json:"sort_ascending"`
ShowSubcategoryList bool `json:"show_subcategory_list"`
NumFeaturedTopics int `json:"num_featured_topics"`
DefaultView string `json:"default_view"`
SubcategoryListStyle string `json:"subcategory_list_style"`
DefaultTopPeriod string `json:"default_top_period"`
MinimumRequiredTags int `json:"minimum_required_tags"`
NavigateToFirstPostAfterRead bool `json:"navigate_to_first_post_after_read"`
TopicsDay int `json:"topics_day"`
TopicsWeek int `json:"topics_week"`
TopicsMonth int `json:"topics_month"`
TopicsYear int `json:"topics_year"`
TopicsAllTime int `json:"topics_all_time"`
DescriptionExcerpt string `json:"description_excerpt"`
UploadedLogo interface{} `json:"uploaded_logo"`
UploadedBackground interface{} `json:"uploaded_background"`
SubcategoryIds []int `json:"subcategory_ids,omitempty"`
} `json:"categories"`
} `json:"category_list"`
}
type CategoryTopics struct {
Users []struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
AvatarTemplate string `json:"avatar_template"`
} `json:"users"`
PrimaryGroups []interface{} `json:"primary_groups"`
TopicList struct {
CanCreateTopic bool `json:"can_create_topic"`
Draft interface{} `json:"draft"`
DraftKey string `json:"draft_key"`
DraftSequence int `json:"draft_sequence"`
PerPage int `json:"per_page"`
TopTags []string `json:"top_tags"`
Topics []struct {
ID int `json:"id"`
Title string `json:"title"`
FancyTitle string `json:"fancy_title"`
Slug string `json:"slug"`
PostsCount int `json:"posts_count"`
ReplyCount int `json:"reply_count"`
HighestPostNumber int `json:"highest_post_number"`
ImageURL interface{} `json:"image_url"`
CreatedAt time.Time `json:"created_at"`
LastPostedAt time.Time `json:"last_posted_at"`
Bumped bool `json:"bumped"`
BumpedAt time.Time `json:"bumped_at"`
Unseen bool `json:"unseen"`
LastReadPostNumber int `json:"last_read_post_number"`
Unread int `json:"unread"`
NewPosts int `json:"new_posts"`
Pinned bool `json:"pinned"`
Unpinned bool `json:"unpinned"`
Visible bool `json:"visible"`
Closed bool `json:"closed"`
Archived bool `json:"archived"`
NotificationLevel int `json:"notification_level"`
Bookmarked bool `json:"bookmarked"`
Liked bool `json:"liked"`
Tags []interface{} `json:"tags"`
Views int `json:"views"`
LikeCount int `json:"like_count"`
HasSummary bool `json:"has_summary"`
Archetype string `json:"archetype"`
LastPosterUsername string `json:"last_poster_username"`
CategoryID int `json:"category_id"`
PinnedGlobally bool `json:"pinned_globally"`
FeaturedLink interface{} `json:"featured_link"`
Posters []struct {
Extras interface{} `json:"extras"`
Description string `json:"description"`
UserID int `json:"user_id"`
PrimaryGroupID interface{} `json:"primary_group_id"`
} `json:"posters"`
BookmarkedPostNumbers []int `json:"bookmarked_post_numbers,omitempty"`
} `json:"topics"`
} `json:"topic_list"`
}