Skip to content

Instantly share code, notes, and snippets.

@kyrylo
Created February 9, 2017 21:27
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 kyrylo/c33cecf566c12d05a4d93d74ec0d53d5 to your computer and use it in GitHub Desktop.
Save kyrylo/c33cecf566c12d05a4d93d74ec0d53d5 to your computer and use it in GitHub Desktop.
package main
import (
"database/sql"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"path"
"sync"
_ "github.com/mattn/go-sqlite3"
)
var dbpath = flag.String("dbpath", "openra.db", "path to OpenRA database")
type Oramap []map[string]*json.RawMessage
func worker(ch chan string, wg *sync.WaitGroup) {
defer wg.Done()
for u := range ch {
resp, err := http.Get(u)
if err != nil {
fmt.Printf("%s: %s\n", u, err)
continue
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
fmt.Printf("%s (%d)\n", u, resp.StatusCode)
continue
}
body, err := ioutil.ReadAll(resp.Body)
var oramap Oramap
err = json.Unmarshal(body, &oramap)
if err != nil || oramap == nil {
continue
}
var title string
err = json.Unmarshal(*oramap[0]["title"], &title)
if err != nil {
log.Println(err)
}
ch <- fmt.Sprintf("%s - %s", title)
}
}
func main() {
flag.Parse()
db, err := sql.Open("sqlite3", *dbpath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
rows, err := db.Query("SELECT map, played_counter FROM map_stats;")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
ch := make(chan string)
wg := new(sync.WaitGroup)
for i := 0; i < 5; i++ {
wg.Add(1)
go worker(ch, wg)
}
for rows.Next() {
var mapHash string
var playedCounter string
err = rows.Scan(&mapHash, &playedCounter)
if err != nil {
log.Fatal(err)
}
u, err := url.Parse("http://resource.openra.net/map/hash/")
if err != nil {
log.Fatal(err)
}
u.Path = path.Join(u.Path, mapHash)
ch <- u.String()
}
close(ch)
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment