Skip to content

Instantly share code, notes, and snippets.

@linxlunx
Created August 2, 2015 15:51
Show Gist options
  • Save linxlunx/3c97338195500cb47198 to your computer and use it in GitHub Desktop.
Save linxlunx/3c97338195500cb47198 to your computer and use it in GitHub Desktop.
Twitter User Hovercard Endpoint
package main
import (
"fmt"
"log"
"io/ioutil"
"net/http"
"encoding/json"
"regexp"
"strings"
"flag"
"os"
)
type JSONdata struct {
Screen_name string
User_id string
Html string
}
func get_user(username string) []byte{
full_url := fmt.Sprintf("https://twitter.com/i/profiles/popup?screen_name=%s&wants_hovercard=true&_=1438430333863", username)
client := &http.Client{}
req, err := http.NewRequest("GET", full_url, nil)
if err != nil {
log.Fatal(err)
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
user_data, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
return user_data
}
func parse_data(content []byte) {
j := &JSONdata{}
err := json.Unmarshal([]byte(content), &j)
if err != nil {
log.Fatal(err)
}
x := regexp.MustCompile("title=\"(.*) Pengikut\"\n")
follower := x.FindStringSubmatch(j.Html)[1]
y := regexp.MustCompile("title=\"(.*) Mengikuti\"\n")
following := y.FindStringSubmatch(j.Html)[1]
z := regexp.MustCompile("data-name=\"(.*)\" data-protected=\"false\"\u003e\n\n")
fullname := z.FindStringSubmatch(j.Html)[1]
i := regexp.MustCompile("\u003cimg src=\"(.*)\" alt")
image := strings.Replace(i.FindStringSubmatch(j.Html)[1], "_bigger", "", -1)
fmt.Printf("screen_name: %s\n", j.Screen_name)
fmt.Printf("user_id: %s\n", j.User_id)
fmt.Printf("fullname: %s\n", fullname)
fmt.Printf("image: %s\n", image)
fmt.Printf("follower: %s\n", follower)
fmt.Printf("following: %s\n", following)
}
func main() {
flag.Parse()
if flag.NArg() < 1 {
fmt.Fprintf(os.Stderr, "Usage: %s [twitter_user_name]\n", os.Args[0])
os.Exit(1)
}
username := flag.Arg(0)
user := get_user(username)
parse_data(user)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment