Skip to content

Instantly share code, notes, and snippets.

@unixpickle
Created June 20, 2017 21:03
Show Gist options
  • Save unixpickle/a8f0b7e86a9ef7fccab9894f0f2234ab to your computer and use it in GitHub Desktop.
Save unixpickle/a8f0b7e86a9ef7fccab9894f0f2234ab to your computer and use it in GitHub Desktop.
List Famobi games
// Command famobi_games creates a useful JSON file listing
// all Famobi games and their corresponding tags.
//
// With the resulting JSON, you can easily find all the
// games with a given tag using `jq`:
//
// jq '.[] | select(.categories | contains(["Match 3"]))'
//
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
)
func main() {
categoryExpr := regexp.MustCompile("</i>&nbsp;(.*)</a>")
nameExpr := regexp.MustCompile("<h1 itemprop=\"name\">(.*)</h1>")
fmt.Println("[")
for i, url := range gameURLs() {
if i > 0 {
fmt.Println(",")
}
page := fetch(url)
var categories []string
for _, m := range categoryExpr.FindAllStringSubmatch(page, -1) {
categories = append(categories, m[1])
}
name := nameExpr.FindStringSubmatch(page)[1]
obj := map[string]interface{}{
"categories": categories,
"name": name,
"url": url,
}
rawData, _ := json.MarshalIndent(obj, " ", " ")
fmt.Print(" " + string(rawData))
}
fmt.Println()
fmt.Println("]")
}
func gameURLs() []string {
page := fetch("http://html5games.com/All-Games")
expr := regexp.MustCompile("href=\"(/Game/.*)\"")
matches := expr.FindAllStringSubmatch(page, -1)
var res []string
for _, match := range matches {
res = append(res, "http://html5games.com"+match[1])
}
return res
}
func fetch(url string) string {
resp, err := http.Get(url)
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
return string(body)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment