Exemplo n.º 1
0
func GetDB() (*sql.DB, error) {
	p, err := ovgdb.GetDBPath()
	if err != nil {
		return nil, err
	}
	err = mkDir(p)
	var version string
	if err != nil {
		return nil, err
	}
	fp := path.Join(p, fileName)
	mp := path.Join(p, metaName)
	if exists(fp) && exists(mp) {
		b, err := ioutil.ReadFile(mp)
		if err != nil {
			return nil, err
		}
		version = strings.Trim(string(b[:]), "\n\r")
	}
	err = updateDB(version, p)
	if err != nil {
		return nil, err
	}
	return sql.Open("sqlite3", fp)
}
Exemplo n.º 2
0
// GetMap gets the mapping of hashes to IDs.
func GetHashMap() (*HashMap, error) {
	ret := &HashMap{Data: make(map[string][]string)}
	var f io.ReadCloser
	var err error
	if *hashFile != "" {
		f, err = os.Open(*hashFile)
		if err != nil {
			return ret, err
		}
	} else {
		p, err := ovgdb.GetDBPath()
		if err != nil {
			return ret, err
		}
		err = mkDir(p)
		if err != nil {
			return ret, err
		}
		fp := path.Join(p, hashName)
		mp := path.Join(p, hashMeta)
		var version string
		if exists(fp) && exists(mp) {
			b, err := ioutil.ReadFile(mp)
			if err != nil {
				return nil, err
			}
			version = strings.Trim(string(b[:]), "\n\r")
		}
		err = updateHash(version, p)
		if err != nil {
			return ret, err
		}
		f, err = os.Open(fp)
		if err != nil {
			return ret, err
		}
	}
	defer f.Close()
	c := csv.NewReader(f)
	r, err := c.ReadAll()
	if err != nil {
		return ret, err
	}
	for _, v := range r {
		ret.Data[strings.ToLower(v[0])] = []string{v[1], v[3]}
	}
	return ret, nil
}
Exemplo n.º 3
0
func main() {
	db, err := GetDB()
	if err != nil {
		log.Fatal(err)
	}
	games, err := queryDB(db, query)
	if err != nil {
		log.Fatal(err)
	}
	p, err := ovgdb.GetDBPath()
	if err != nil {
		log.Fatal(err)
	}
	os.RemoveAll(path.Join(p, dbName))
	if err != nil {
		log.Fatal(err)
	}
	ldb, err := leveldb.OpenFile(path.Join(p, dbName), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer ldb.Close()
	for _, g := range games {
		b, err := json.Marshal(g.ToSlice())
		if err != nil {
			log.Fatal(err)
		}
		batch := new(leveldb.Batch)
		i := []byte(g.ReleaseID)
		h := []byte(g.Hash)
		fn := []byte(strings.ToLower(g.FileName))
		batch.Put(i, b)
		batch.Put(h, i)
		batch.Put(fn, i)
		err = ldb.Write(batch, nil)
		if err != nil {
			log.Fatal(err)
		}
	}
}