Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
172 lines (142 sloc) 4.02 KB
//csvreader reads the "provincia" files on a local directory
//decodes the csv and stores them in a Redis database.
package csvreader
import (
"encoding/csv"
"encoding/json"
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/lagarciag/gopadroneleccr/persona"
"os"
"strconv"
"strings"
)
const (
//IP Addres and port where the Redis Data base can be located.
ADDRESS = "127.0.0.1:6379"
)
var (
c, err = redis.Dial("tcp", ADDRESS)
)
//PadronCsvReader acts as an object container for this package
type PadronCsvReader struct {
provincia string
code persona.CodeElectT
persona persona.PersonaT
electoralMap map[int]persona.CodeElectT
TmpDir string
}
//NewPadronCsvReader creates a reader for the cvs donloaded for each
//"provincia".
func NewPadronCsvReader(provincia string) *PadronCsvReader {
padron := PadronCsvReader{}
padron.provincia = provincia
padron.electoralMap = make(map[int]persona.CodeElectT)
padron.code = persona.CodeElectT{}
return &padron
}
//ReadProvincia reads de csv for a provincia
//and stores the results in a Redis database
func (p *PadronCsvReader) ReadProvincia() {
provinciaPath := p.TmpDir + "/" + p.provincia + "/" + strings.ToUpper(p.provincia) + ".txt"
personaPadron := persona.PersonaT{}
//Open csv file
//*************
csvfile, err := os.Open(provinciaPath)
if err != nil {
fmt.Println(err)
return
}
defer csvfile.Close()
reader := csv.NewReader(csvfile)
reader.FieldsPerRecord = 0 // see the Reader struct information below
rawCSVdata, err := reader.ReadAll()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
/**************
Start Redis
***************/
c, err := redis.Dial("tcp", ":6379")
if err != nil {
panic(err)
}
defer c.Close()
// sanity check, display to standard output
for _, row := range rawCSVdata {
personaPadron.Cedula, err = strconv.Atoi(row[0])
personaPadron.Codelec, err = strconv.Atoi(row[1])
personaPadron.Sexo, err = strconv.Atoi(row[2])
personaPadron.Fechacaduc = row[3]
personaPadron.Junta, err = strconv.Atoi(row[4])
personaPadron.Nombre = row[5]
personaPadron.Apellido1 = row[6]
personaPadron.Apellido2 = row[7]
personaPadron.Provincia = p.electoralMap[personaPadron.Codelec].Provincia
personaPadron.Canton = p.electoralMap[personaPadron.Codelec].Canton
personaPadron.Distrito = p.electoralMap[personaPadron.Codelec].Distrito
/***************
Convert to Json
****************/
b, err := json.Marshal(personaPadron)
if err != nil {
fmt.Println("error:", err)
}
stringToSave := fmt.Sprintf("%s",b)
/**************
Save in Redis
***************/
//set
c.Do("SET",personaPadron.Cedula,stringToSave)
}
//get
world, err := redis.String(c.Do("GET",101520534))
if err != nil {
fmt.Println("key not found")
}
fmt.Println(world)
csvfile.Close()
}
//ReadDistElectoral read the electoral codes from the
//Distelect file and stores the results in a CodeElectT
//struct that is later saved in a map that is eventually used
//by ReadProvincia to decode the eleccode into real places names.
func (p *PadronCsvReader) ReadDistElectoral() {
distElectPath := p.TmpDir + "/" + p.provincia + "/Distelec.txt"
p.electoralMap = make(map[int]persona.CodeElectT)
//**************
//Open csv file
//*************
csvfile, err := os.Open(distElectPath)
if err != nil {
fmt.Println(err)
return
}
defer csvfile.Close()
reader := csv.NewReader(csvfile)
reader.FieldsPerRecord = 0 // see the Reader struct information below
rawCSVdata, err := reader.ReadAll()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// sanity check, display to standard output
for _, each := range rawCSVdata {
//*************************************
//Read codigo electoral first
//**************************************
distritoElect, err := strconv.Atoi(each[0])
if err != nil {
panic("Bad string conversion")
}
code := persona.CodeElectT{
Codelec: uint32(distritoElect),
Provincia: each[1],
Canton: each[2],
Distrito: each[3],
}
p.electoralMap[distritoElect] = code
}
csvfile.Close()
}
You can’t perform that action at this time.