Skip to content

Instantly share code, notes, and snippets.

@gmaliar
Last active November 17, 2016 12:35
Show Gist options
  • Select an option

  • Save gmaliar/320cad4b5baa86bb7e3b7e3c6742b25a to your computer and use it in GitHub Desktop.

Select an option

Save gmaliar/320cad4b5baa86bb7e3b7e3c6742b25a to your computer and use it in GitHub Desktop.
Usage: ./socialblade_crawler <Twitch username>
BINARY=socialblade_crawler
VERSION=1.0.0
BUILD=`git rev-parse HEAD`
LDFLAGS=-ldflags "-X main.Version=${VERSION} -X main.Build=${BUILD}"
all: deps clean build
deps:
go get
build:
GOOS=windows GOARCH=amd64 go build ${LDFLAGS} -v -o pkg/${BINARY}.exe
GOOS=linux GOARCH=amd64 go build ${LDFLAGS} -v -o pkg/${BINARY}_linux
GOOS=darwin GOARCH=amd64 go build ${LDFLAGS} -v -o pkg/${BINARY}_mac
clean:
if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi
.PHONY: all deps build clean
package main
import (
"strings"
"bytes"
"fmt"
"net/http"
"os"
"golang.org/x/net/html"
"github.com/go-xmlpath/xmlpath"
)
var (
Version string
Build string
)
var (
TotalFollowers string = "//*[@id=\"stats-top-data-module-wrap\"]/div[2]/div[2]"
TotalViews string = "//*[@id=\"stats-top-data-module-wrap\"]/div[3]/div[2]"
Last30DaysViews string = "/html/body/div[7]/div[2]/div[1]/div[3]/div[2]/div/span"
)
func main() {
user := os.Args[1]
fmt.Printf("Fetching data for %v\n", user)
resp, err := http.Get(fmt.Sprintf("http://socialblade.com/twitch/user/%v", user))
if err != nil {
panic(err)
}
root, err := html.Parse(resp.Body)
if err != nil {
panic(err)
}
var b bytes.Buffer
html.Render(&b, root)
html := b.String()
reader := strings.NewReader(html)
xmlRoot, err := xmlpath.ParseHTML(reader)
if err != nil {
panic(err)
}
totalFollowersPath := xmlpath.MustCompile(TotalFollowers)
totalViewsPath := xmlpath.MustCompile(TotalViews)
mothlyViewsPath := xmlpath.MustCompile(Last30DaysViews)
if value, ok := totalFollowersPath.String(xmlRoot); ok {
fmt.Printf("Total Followers: %v\n", value)
}
if value, ok := totalViewsPath.String(xmlRoot); ok {
fmt.Printf("Total Views: %v\n", value)
}
if value, ok := mothlyViewsPath.String(xmlRoot); ok {
fmt.Printf("Montly Views: %v\n", value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment