|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "time" |
5 | 10 |
|
| 11 | + "github.com/alexeyco/simpletable" |
6 | 12 | "github.com/spf13/cobra" |
7 | 13 | ) |
8 | 14 |
|
| 15 | +const ( |
| 16 | + rootAPIURL = "https://hashnode.com/ajax/posts" |
| 17 | +) |
| 18 | + |
| 19 | +// API URL's |
| 20 | +var ( |
| 21 | + hotPostsAPI = fmt.Sprintf("%s/%s", rootAPIURL, "hot") |
| 22 | + newsAPI = fmt.Sprintf("%s/%s", rootAPIURL, "news") |
| 23 | +) |
| 24 | + |
| 25 | +// flags |
| 26 | +var ( |
| 27 | + hot bool |
| 28 | + news bool |
| 29 | +) |
| 30 | + |
9 | 31 | // postsCmd represents the posts command |
10 | 32 | var postsCmd = &cobra.Command{ |
11 | 33 | Use: "posts", |
12 | 34 | Short: "Lists posts", |
13 | 35 | Run: func(cmd *cobra.Command, args []string) { |
14 | | - fmt.Println("posts called") |
| 36 | + switch { |
| 37 | + case news: |
| 38 | + getNews() |
| 39 | + case hot: |
| 40 | + getHotPosts() |
| 41 | + default: |
| 42 | + log.Println("Specify what posts to get") |
| 43 | + } |
15 | 44 | }, |
16 | 45 | } |
17 | 46 |
|
18 | 47 | func init() { |
19 | 48 | rootCmd.AddCommand(postsCmd) |
| 49 | + |
| 50 | + postsCmd.PersistentFlags().BoolVar(&hot, "hot", false, "get hot posts") |
| 51 | + postsCmd.PersistentFlags().BoolVar(&news, "news", false, "get news") |
| 52 | +} |
| 53 | + |
| 54 | +func getHotPosts() { |
| 55 | + b, err := makeRequest(hotPostsAPI) |
| 56 | + if err != nil { |
| 57 | + log.Println(err) |
| 58 | + } |
| 59 | + var hotposts HotPosts |
| 60 | + err = json.Unmarshal(b, &hotposts) |
| 61 | + if err != nil { |
| 62 | + log.Println(err) |
| 63 | + } |
| 64 | + table := simpletable.New() |
| 65 | + table.Header = &simpletable.Header{ |
| 66 | + Cells: []*simpletable.Cell{ |
| 67 | + {Align: simpletable.AlignCenter, Text: "#"}, |
| 68 | + {Align: simpletable.AlignCenter, Text: "Title"}, |
| 69 | + {Align: simpletable.AlignCenter, Text: "Link"}, |
| 70 | + }, |
| 71 | + } |
| 72 | + for ind, post := range hotposts.Posts { |
| 73 | + r := []*simpletable.Cell{ |
| 74 | + {Align: simpletable.AlignRight, Text: fmt.Sprintf("%d", ind+1)}, |
| 75 | + {Align: simpletable.AlignRight, Text: post.Title}, |
| 76 | + {Align: simpletable.AlignRight, Text: post.Slug}, |
| 77 | + } |
| 78 | + table.Body.Cells = append(table.Body.Cells, r) |
| 79 | + } |
| 80 | + table.SetStyle(simpletable.StyleCompactLite) |
| 81 | + fmt.Println(table.String()) |
| 82 | +} |
| 83 | + |
| 84 | +func getNews() { |
| 85 | + b, err := makeRequest(newsAPI) |
| 86 | + if err != nil { |
| 87 | + log.Println(err) |
| 88 | + } |
| 89 | + m := make(map[string]interface{}) |
| 90 | + json.Unmarshal(b, &m) |
| 91 | + fmt.Println(m) |
| 92 | +} |
| 93 | + |
| 94 | +func makeRequest(url string) ([]byte, error) { |
| 95 | + client := http.Client{ |
| 96 | + Timeout: time.Duration(1 * time.Minute), |
| 97 | + } |
| 98 | + resp, err := client.Get(url) |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + |
| 103 | + defer resp.Body.Close() |
| 104 | + |
| 105 | + if resp.StatusCode == http.StatusOK { |
| 106 | + // read response body |
| 107 | + body, err := ioutil.ReadAll(resp.Body) |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + return body, nil |
| 112 | + } |
| 113 | + |
| 114 | + return nil, fmt.Errorf("Not Found") |
| 115 | + |
| 116 | +} |
| 117 | + |
| 118 | +type HotPosts struct { |
| 119 | + Posts []struct { |
| 120 | + ID string `json:"_id"` |
| 121 | + FollowersCount int `json:"followersCount"` |
| 122 | + Author struct { |
| 123 | + ID string `json:"_id"` |
| 124 | + Role interface{} `json:"role"` |
| 125 | + NumFollowing int `json:"numFollowing"` |
| 126 | + NumFollowers int `json:"numFollowers"` |
| 127 | + Name string `json:"name"` |
| 128 | + Tagline string `json:"tagline"` |
| 129 | + Photo string `json:"photo"` |
| 130 | + Username string `json:"username"` |
| 131 | + Appreciations []struct { |
| 132 | + Badge string `json:"badge"` |
| 133 | + ID string `json:"_id"` |
| 134 | + Count int `json:"count"` |
| 135 | + } `json:"appreciations"` |
| 136 | + DateJoined time.Time `json:"dateJoined"` |
| 137 | + SocialMedia struct { |
| 138 | + Website string `json:"website"` |
| 139 | + Twitter string `json:"twitter"` |
| 140 | + Github string `json:"github"` |
| 141 | + Linkedin string `json:"linkedin"` |
| 142 | + Google string `json:"google"` |
| 143 | + Facebook string `json:"facebook"` |
| 144 | + } `json:"socialMedia"` |
| 145 | + StoriesCreated []string `json:"storiesCreated"` |
| 146 | + Location string `json:"location"` |
| 147 | + CoverImage string `json:"coverImage"` |
| 148 | + BadgesAwarded []interface{} `json:"badgesAwarded"` |
| 149 | + TotalUpvotesReceived int `json:"totalUpvotesReceived"` |
| 150 | + IsEvangelist bool `json:"isEvangelist"` |
| 151 | + NumReactions int `json:"numReactions"` |
| 152 | + } `json:"author"` |
| 153 | + Cuid string `json:"cuid"` |
| 154 | + Slug string `json:"slug"` |
| 155 | + Title string `json:"title"` |
| 156 | + Type string `json:"type"` |
| 157 | + ReactionsByCurrentUser []interface{} `json:"reactionsByCurrentUser"` |
| 158 | + TotalReactions int `json:"totalReactions"` |
| 159 | + Reactions []struct { |
| 160 | + ID string `json:"_id"` |
| 161 | + Image string `json:"image"` |
| 162 | + Name string `json:"name"` |
| 163 | + } `json:"reactions"` |
| 164 | + BookmarkedIn []interface{} `json:"bookmarkedIn"` |
| 165 | + HasReward bool `json:"hasReward"` |
| 166 | + Contributors []struct { |
| 167 | + User struct { |
| 168 | + ID string `json:"_id"` |
| 169 | + Username string `json:"username"` |
| 170 | + Name string `json:"name"` |
| 171 | + Photo string `json:"photo"` |
| 172 | + Tagline string `json:"tagline"` |
| 173 | + BadgesAwarded []interface{} `json:"badgesAwarded"` |
| 174 | + Appreciations []struct { |
| 175 | + Badge string `json:"badge"` |
| 176 | + ID string `json:"_id"` |
| 177 | + Count int `json:"count"` |
| 178 | + } `json:"appreciations"` |
| 179 | + DateJoined time.Time `json:"dateJoined"` |
| 180 | + SocialMedia struct { |
| 181 | + Twitter string `json:"twitter"` |
| 182 | + Github string `json:"github"` |
| 183 | + Stackoverflow string `json:"stackoverflow"` |
| 184 | + Linkedin string `json:"linkedin"` |
| 185 | + Google string `json:"google"` |
| 186 | + Website string `json:"website"` |
| 187 | + } `json:"socialMedia"` |
| 188 | + StoriesCreated []string `json:"storiesCreated"` |
| 189 | + NumFollowing int `json:"numFollowing"` |
| 190 | + NumFollowers int `json:"numFollowers"` |
| 191 | + Location string `json:"location"` |
| 192 | + Role interface{} `json:"role"` |
| 193 | + CoverImage string `json:"coverImage"` |
| 194 | + TotalUpvotesReceived int `json:"totalUpvotesReceived"` |
| 195 | + IsEvangelist bool `json:"isEvangelist"` |
| 196 | + NumReactions int `json:"numReactions"` |
| 197 | + } `json:"user"` |
| 198 | + Stamp string `json:"stamp"` |
| 199 | + ID string `json:"_id"` |
| 200 | + } `json:"contributors"` |
| 201 | + IsActive bool `json:"isActive"` |
| 202 | + ResponseCount int `json:"responseCount"` |
| 203 | + DateAdded time.Time `json:"dateAdded"` |
| 204 | + Tags []struct { |
| 205 | + ID string `json:"_id"` |
| 206 | + Name string `json:"name"` |
| 207 | + Slug string `json:"slug"` |
| 208 | + MergedWith interface{} `json:"mergedWith,omitempty"` |
| 209 | + IsApproved bool `json:"isApproved"` |
| 210 | + IsActive bool `json:"isActive"` |
| 211 | + } `json:"tags"` |
| 212 | + Downvotes int `json:"downvotes"` |
| 213 | + Upvotes int `json:"upvotes"` |
| 214 | + TotalPollVotes int `json:"totalPollVotes"` |
| 215 | + PollOptions []interface{} `json:"pollOptions"` |
| 216 | + HasPolls bool `json:"hasPolls"` |
| 217 | + Brief string `json:"brief"` |
| 218 | + CoverImage string `json:"coverImage"` |
| 219 | + Views int `json:"views"` |
| 220 | + IsAnonymous bool `json:"isAnonymous"` |
| 221 | + DateUpdated time.Time `json:"dateUpdated"` |
| 222 | + IndexVotedByCurrentUser int `json:"indexVotedByCurrentUser"` |
| 223 | + IsFollowing bool `json:"isFollowing"` |
| 224 | + DateFeatured time.Time `json:"dateFeatured,omitempty"` |
| 225 | + } `json:"posts"` |
20 | 226 | } |
0 commit comments