Created
March 1, 2013 03:42
-
-
Save paddycarver/5062315 to your computer and use it in GitHub Desktop.
Find out who retweeted your tweet, sorted by how many followers they have.
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" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"sort" | |
"strconv" | |
) | |
type User struct { | |
ScreenName string `json:"screen_name"` | |
Followers int `json:"followers_count"` | |
} | |
type Users []User | |
func (u Users) Len() int { return len(u) } | |
func (u Users) Swap(i, j int) { u[i], u[j] = u[j], u[i] } | |
func (u Users) Less(i, j int) bool { return u[i].Followers > u[j].Followers } | |
func getRetweeters(id uint64, page int) (Users, error) { | |
resp, err := http.Get("http://api.twitter.com/1/statuses/" + strconv.FormatUint(id, 10) + "/retweeted_by.json?count=100&page=" + strconv.Itoa(page)) | |
if err != nil { | |
return Users{}, err | |
} | |
respBytes, err := ioutil.ReadAll(resp.Body) | |
resp.Body.Close() | |
if err != nil { | |
return Users{}, err | |
} | |
var users Users | |
err = json.Unmarshal(respBytes, &users) | |
if err != nil { | |
return Users{}, err | |
} | |
return users, nil | |
} | |
func main() { | |
var id uint64 | |
flag.Uint64Var(&id, "id", 0, "id of the tweet") | |
flag.Parse() | |
if id == 0 { | |
panic("ID must be set.") | |
} | |
responses := 1 | |
page := 1 | |
users := Users{} | |
for responses > 0 { | |
retweeters, err := getRetweeters(id, page) | |
if err != nil { | |
panic(err) | |
} | |
users = append(users, retweeters...) | |
responses = len(retweeters) | |
page = page + 1 | |
} | |
sort.Sort(users) | |
followers := 0 | |
for _, rt := range users { | |
fmt.Printf("@%s\t%d followers\thttps://twitter.com/%s\n", rt.ScreenName, rt.Followers, rt.ScreenName) | |
followers = followers + rt.Followers | |
} | |
fmt.Printf("Retweeted by %d people with a total of %d followers.\n", len(users), followers) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment