Created
September 27, 2014 05:51
-
-
Save ciarand/b6ee5863692b6cd4b4ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"errors" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"os" | |
) | |
func main() { | |
flag.Parse() | |
username := flag.Arg(0) | |
if username == "" { | |
ExitWithError(errors.New("need to provide a username")) | |
} | |
resp, err := http.Get("https://api.app.net/users/@" + flag.Arg(0)) | |
if err != nil { | |
ExitWithError(err) | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
ExitWithError(err) | |
} | |
var record UserResponse | |
err = json.Unmarshal(body, &record) | |
if err != nil { | |
ExitWithError(err) | |
} | |
fmt.Printf("@%s has %d followers, has starred %d posts, and has this to say about themselves: %s\n", | |
record.Data.Username, | |
record.Data.Counts.Followers, | |
record.Data.Counts.Stars, | |
record.Data.Description.Text) | |
} | |
func ExitWithError(err error) { | |
fmt.Println(err) | |
os.Exit(1) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
type UserResponse struct { | |
Data User `json:"data"` | |
Meta Meta `json:"meta"` | |
} | |
type Annotation struct { | |
Type string `json:"type"` | |
Value map[string]string `json:"value"` | |
} | |
type Description struct { | |
Text string `json:"text"` | |
Html string `json:"html"` | |
Entities Entities `json:"entities"` | |
} | |
type UserCounts struct { | |
Following int `json:"following"` | |
Followers int `json:"followers"` | |
Posts int `json:"posts"` | |
Stars int `json:"stars"` | |
} | |
type Image struct { | |
Height int `json:"height"` | |
Width int `json:"width"` | |
IsDefault bool `json:"is_default"` | |
Url string `json:"url"` | |
} | |
type User struct { | |
Id string `json:"id"` | |
Username string `json:"username"` | |
Name string `json:"name"` | |
Description Description `json:"description"` | |
Timezone string `json:"timezone"` | |
Locale string `json:"locale"` | |
AvatarImage Image `json:"avatar_image"` | |
CoverImage Image `json:"cover_image"` | |
Type string `json:"type"` | |
CreatedAt string `json:"created_at"` | |
Counts UserCounts `json:"counts"` | |
FollowsYou bool `json:"follows_you"` | |
YouBlocked bool `json:"you_blocked"` | |
YouFollow bool `json:"you_follow"` | |
YouMuted bool `json:"you_muted"` | |
YouCanSubscribe bool `json:"you_can_subscribe"` | |
YouCanFollow bool `json:"you_can_follow"` | |
VerifiedDomain string `json:"verified_domain"` | |
Annotations []Annotation `json:"annotations"` | |
CanonicalUrl string `json:"canonical_url"` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment