Skip to content

Instantly share code, notes, and snippets.

@ThijsFeryn
Created June 15, 2015 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThijsFeryn/cad9199e500e57654d72 to your computer and use it in GitHub Desktop.
Save ThijsFeryn/cad9199e500e57654d72 to your computer and use it in GitHub Desktop.
Pass the JoindIn speaker id to this program and it will count the amount of talks given at each location
package main
import (
"strconv"
"io/ioutil"
"log"
"net/http"
"fmt"
"encoding/json"
"time"
"os"
)
func GetPlace(url string) (string) {
bodyByteStream := CallUrl(url)
var bodyInterface interface{}
json.Unmarshal(bodyByteStream, &bodyInterface)
bodyMap := bodyInterface.(map[string]interface{})
eventsMap := bodyMap["events"].([]interface{})
eventMap := eventsMap[0].(map[string]interface{})
return eventMap["location"].(string)
}
func CountCitiesForUser(userId int) (map [string] int) {
bodyByteStream := CallUrl("http://api.joind.in/v2.1/users/" + strconv.Itoa(userId)+"/talks?count=100000&start=0&resultsperpage=100000")
var bodyInterface interface{}
json.Unmarshal(bodyByteStream, &bodyInterface)
bodyMap := bodyInterface.(map[string]interface{})
talksMap := bodyMap["talks"].([]interface{})
var places = make(map[string]int)
var placeChannel chan string = make(chan string)
var counter int = 0
for _, value := range talksMap {
talkMap := value.(map[string]interface{})
go func() {
placeChannel <- GetPlace(talkMap["event_uri"].(string))
} ()
counter++
}
for i := 0; i < counter; i++ {
places[<-placeChannel]++
}
return places
}
func CallUrl(url string) ([] byte) {
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Fatal(resp.Status)
}
bodyByteStream, _ := ioutil.ReadAll(resp.Body)
return bodyByteStream
}
func main() {
var user int
if len(os.Args) == 1 {
user = 1975
} else {
user,_ = strconv.Atoi(os.Args[1])
}
start := time.Now()
events := CountCitiesForUser(user)
fmt.Printf("== Amount of talks by JoindIn user %d at location == \n",user)
for k,v := range events {
fmt.Printf("Talks given in %s: %d\n",k,v)
}
elapsed := time.Since(start)
fmt.Println("========================")
fmt.Printf("Elapsed time: %s\n",elapsed)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment