Some caches and tests to get names from skill list. Currently crashing in PostUniverseNames

This commit is contained in:
Thomas Schwery 2017-01-30 18:43:03 +01:00
parent 428eca4b5a
commit c1f48b56ed

235
main.go
View file

@ -2,6 +2,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"time"
"golang.org/x/net/context" "golang.org/x/net/context"
"golang.org/x/oauth2" "golang.org/x/oauth2"
@ -11,16 +12,20 @@ import (
"fmt" "fmt"
"log" "log"
"strconv"
"github.com/go-openapi/strfmt" "github.com/go-openapi/strfmt"
ESI "client" ESI "client"
ESIPlanetaryInteraction "client/planetary_interaction" ESIPlanetaryInteraction "client/planetary_interaction"
ESISkills "client/skills" ESISkills "client/skills"
ESIUniverse "client/universe"
"database/sql"
_ "github.com/mattn/go-sqlite3"
httptransport "github.com/go-openapi/runtime/client" httptransport "github.com/go-openapi/runtime/client"
//"database/sql"
//_ "github.com/mattn/go-sqlite3"
) )
// Character - Structure to save the verification data. // Character - Structure to save the verification data.
@ -62,9 +67,12 @@ func main() {
client := googleOauthConfig.Client(oauth2.NoContext, cToken) client := googleOauthConfig.Client(oauth2.NoContext, cToken)
m := getCharacterInfo(client) m := getCharacterInfo(client)
fmt.Printf("Character id is %d\n", m.CharacterID) fmt.Printf("Name: %s\n", m.CharacterName)
fmt.Printf("Id: %d\n", m.CharacterID)
getCharacterPlanets(client, m) //getCharacterPlanets(client, m)
getCharacterSkillQueue(client, m)
} }
func getCharacterInfo(client *http.Client) *Character { func getCharacterInfo(client *http.Client) *Character {
@ -86,9 +94,6 @@ func getCharacterInfo(client *http.Client) *Character {
return nil return nil
} }
fmt.Printf("Name: %s\n", m.CharacterName)
fmt.Printf("Id: %d\n", m.CharacterID)
return &m return &m
} }
@ -101,19 +106,62 @@ func getCharacterSkillQueue(client *http.Client, m *Character) {
swaggerclient := ESI.New(transport, strfmt.Default) swaggerclient := ESI.New(transport, strfmt.Default)
charIDSkilqueue := ESISkills.NewGetCharactersCharacterIDSkillqueueParams().WithCharacterID(m.CharacterID) charIDSkilqueue := ESISkills.NewGetCharactersCharacterIDSkillqueueParams().WithCharacterID(m.CharacterID)
skillqueueresp, _ := swaggerclient.Skills.GetCharactersCharacterIDSkillqueue(charIDSkilqueue, nil) skillqueueresp, skillerr := swaggerclient.Skills.GetCharactersCharacterIDSkillqueue(charIDSkilqueue, nil)
if skillerr != nil {
log.Fatalf("Error on GetCharactersCharacterIDSkillqueue\n%s\n", skillerr)
}
skillqueue := skillqueueresp.Payload skillqueue := skillqueueresp.Payload
skillNames := make(map[int32]string)
skillMissingNames := make(map[int32]string)
for _, skill := range skillqueue {
skillName := getCachedData(fmt.Sprintf("%d.name", skill.SkillID))
if skillName != "" {
skillNames[*skill.SkillID] = skillName
} else {
skillMissingNames[*skill.SkillID] = ""
}
}
snIds := make([]int32, 0, len(skillMissingNames))
for skillID := range skillMissingNames {
snIds = append(snIds, skillID)
}
var sncallParamBody ESIUniverse.PostUniverseNamesBody
sncallParamBody.Ids = snIds
sncallParam := ESIUniverse.NewPostUniverseNamesParams()
sncallParam.SetIds(sncallParamBody)
skillNameResp, skillNameErr := swaggerclient.Universe.PostUniverseNames(sncallParam)
if skillNameErr != nil {
log.Fatalf("Error on PostUniverseNames\n%s\n", skillNameErr)
}
for _, searchResult := range skillNameResp.Payload {
itemName := searchResult.Name
itemID := searchResult.ID
log.Printf("Got search result %d -> %s", *itemID, *itemName)
putCacheData(fmt.Sprintf("%d.name", *itemID), *itemName)
skillNames[*itemID] = *itemName
}
for _, skill := range skillqueue { for _, skill := range skillqueue {
// element is the element from someSlice for where we are // element is the element from someSlice for where we are
name := "UNK-SKILL" name := skillNames[*skill.SkillID]
level := skill.FinishedLevel
id := skill.SkillID
startDate := skill.StartDate
endDate := skill.FinishDate
fmt.Printf(" %s: %d (%d) - %s to %s\n", name, id, *level, startDate, endDate) fmt.Printf(" %s: %d, level %d - %s to %s\n",
name,
*skill.SkillID,
*skill.FinishedLevel,
time.Time(skill.StartDate).Format("_2 Jan 2006, 15:04"),
time.Time(skill.FinishDate).Format("_2 Jan 2006, 15:04"))
} }
} }
@ -133,42 +181,37 @@ func getCharacterPlanets(client *http.Client, m *Character) {
for _, planet := range planets { for _, planet := range planets {
// element is the element from someSlice for where we are // element is the element from someSlice for where we are
name := "UNK-PLANET" name := "UNK-PLANET"
solarSystemName := "UNK-SYSTEM"
level := planet.UpgradeLevel pcallParam := ESIPlanetaryInteraction.NewGetCharactersCharacterIDPlanetsPlanetIDParams()
id := planet.PlanetID pcallParam.WithCharacterID(m.CharacterID).WithPlanetID(*planet.PlanetID)
solarSystem := planet.SolarSystemID
planetType := planet.PlanetType
lastUpdate := planet.LastUpdate
pins := planet.NumPins
pcallParam := ESIPlanetaryInteraction.NewGetCharactersCharacterIDPlanetsPlanetIDParams().WithCharacterID(m.CharacterID).WithPlanetID(*id) solarSystemInfo := getSolarSystemInformation(swaggerclient, *planet.SolarSystemID)
fmt.Printf(" %s (%d) %s (%d) - %s, level %d with %d structures - %s\n", fmt.Printf(" %s (%d) %s - %s, level %d with %d structures - Updated %s\n",
name, *id, name, *planet.PlanetID,
solarSystemName, *solarSystem, solarSystemInfo.SolarSystemName,
*planetType, *planet.PlanetType,
*level, *pins, *planet.UpgradeLevel, *planet.NumPins,
lastUpdate, planet.LastUpdate,
) )
pesiresponse, _ := swaggerclient.PlanetaryInteraction.GetCharactersCharacterIDPlanetsPlanetID(pcallParam, nil) pesiresponse, _ := swaggerclient.PlanetaryInteraction.GetCharactersCharacterIDPlanetsPlanetID(pcallParam, nil)
for _, pin := range pesiresponse.Payload.Pins { for _, pin := range pesiresponse.Payload.Pins {
if pin.ExtractorDetails != nil { if pin.ExtractorDetails != nil {
fmt.Printf(" Extractor %d (%s), cycles of %d, %d per cycle\n", fmt.Printf(" Extractor - cycle started %s, %d per cycle, cycles of %ds\n",
*pin.PinID,
pin.LastCycleStart, pin.LastCycleStart,
*pin.ExtractorDetails.CycleTime,
*pin.ExtractorDetails.QtyPerCycle, *pin.ExtractorDetails.QtyPerCycle,
*pin.ExtractorDetails.CycleTime,
) )
} else if pin.SchematicID != 0 { } else if pin.SchematicID != 0 {
// Get the schematic from ESI and cache it // Get the schematic from ESI and cache it
schematicInfo := getSchematicsInformation(swaggerclient, pin.SchematicID)
fmt.Printf(" Factory %d (%s) %d\n", fmt.Printf(" Factory - cycle started %s, producing %s, cycles of %ds\n",
*pin.PinID,
pin.LastCycleStart, pin.LastCycleStart,
pin.SchematicID, schematicInfo.SchematicName,
schematicInfo.CycleTime,
) )
} else { } else {
/* /*
@ -183,6 +226,73 @@ func getCharacterPlanets(client *http.Client, m *Character) {
} }
} }
// SolarSystemInfo - Structure to store and cache the Solar System information from ESI
type SolarSystemInfo struct {
SolarSystemName string
}
func getSolarSystemInformation(swaggerclient *ESI.App, solarSystemID int32) SolarSystemInfo {
systemName := getCachedData(fmt.Sprintf("%d.name", solarSystemID))
if systemName == "" {
scallParams := ESIUniverse.NewGetUniverseSystemsSystemIDParams()
scallParams.WithSystemID(solarSystemID)
sesiresponse, _ := swaggerclient.Universe.GetUniverseSystemsSystemID(scallParams)
solarSystemESIInfo := sesiresponse.Payload
var solarSystemInfo SolarSystemInfo
solarSystemInfo.SolarSystemName = *solarSystemESIInfo.SolarSystemName
putCacheData(fmt.Sprintf("%d.name", solarSystemID), solarSystemInfo.SolarSystemName)
return solarSystemInfo
}
var solarSystemInfo SolarSystemInfo
solarSystemInfo.SolarSystemName = systemName
return solarSystemInfo
}
// SchematicInfo - Structure to store and cache the schematics information from ESI
type SchematicInfo struct {
CycleTime int32
SchematicName string
}
func getSchematicsInformation(swaggerclient *ESI.App, schematicID int32) SchematicInfo {
schematicName := getCachedData(fmt.Sprintf("%d.name", schematicID))
schematicCycle := getCachedData(fmt.Sprintf("%d.cycle", schematicID))
if schematicName == "" {
scallParams := ESIPlanetaryInteraction.NewGetUniverseSchematicsSchematicIDParams()
scallParams.WithSchematicID(schematicID)
sesiresponse, _ := swaggerclient.PlanetaryInteraction.GetUniverseSchematicsSchematicID(scallParams)
schematicsESIInfo := sesiresponse.Payload
var schematicInfo SchematicInfo
schematicInfo.CycleTime = *schematicsESIInfo.CycleTime
schematicInfo.SchematicName = *schematicsESIInfo.SchematicName
putCacheData(fmt.Sprintf("%d.name", schematicID), schematicInfo.SchematicName)
putCacheData(fmt.Sprintf("%d.cycle", schematicID), fmt.Sprintf("%d", schematicInfo.CycleTime))
return schematicInfo
}
var schematicInfo SchematicInfo
pCycleTime, _ := strconv.ParseInt(schematicCycle, 10, 32)
schematicInfo.CycleTime = int32(pCycleTime)
schematicInfo.SchematicName = schematicName
return schematicInfo
}
func getNewAuthorizationToken() *oauth2.Token { func getNewAuthorizationToken() *oauth2.Token {
http.HandleFunc("/", handleLogin) http.HandleFunc("/", handleLogin)
http.HandleFunc("/callback", handleAuthenticationCallback) http.HandleFunc("/callback", handleAuthenticationCallback)
@ -243,12 +353,17 @@ func handleAuthenticationCallback(w http.ResponseWriter, r *http.Request) {
log.Printf("Refresh token is %s\n", token.RefreshToken) log.Printf("Refresh token is %s\n", token.RefreshToken)
putCacheData("refreshToken", token.RefreshToken)
messages <- token messages <- token
} }
func getDatabaseToken() *oauth2.Token { func getCachedData(key string) string {
/*
db, err := sql.Open("sqlite3", "./foo.db") db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
log.Fatal(err)
}
defer db.Close() defer db.Close()
_, err = db.Exec("CREATE TABLE IF NOT EXISTS properties (id text NOT NULL PRIMARY KEY, value TEXT);") _, err = db.Exec("CREATE TABLE IF NOT EXISTS properties (id text NOT NULL PRIMARY KEY, value TEXT);")
@ -262,20 +377,52 @@ func getDatabaseToken() *oauth2.Token {
} }
defer stmt.Close() defer stmt.Close()
var refreshToken string var response string
err = stmt.QueryRow("refreshToken").Scan(&refreshToken) err = stmt.QueryRow(key).Scan(&response)
if err != nil { if err != nil {
refreshToken = "" return ""
} }
fmt.Println("Found token :" + refreshToken)
return refreshToken return response
*/ }
log.Print("Using hardcoded refresh token")
func putCacheData(key string, value string) {
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec("CREATE TABLE IF NOT EXISTS properties (id text NOT NULL PRIMARY KEY, value TEXT);")
if err != nil {
log.Fatal(err)
}
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
stmt, err := tx.Prepare("INSERT OR REPLACE INTO properties(id, value) values(?, ?)")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
_, err = stmt.Exec(key, value)
if err != nil {
log.Fatal(err)
}
tx.Commit()
}
func getDatabaseToken() *oauth2.Token {
refreshToken := getCachedData("refreshToken")
token := new(oauth2.Token) token := new(oauth2.Token)
token.RefreshToken = "4sNssL9aVy6Sqf8JUT6Q1hPQjo1lpzJ0mrPIB417QFdz6YooWl9g78qaH2DkZVwq0" token.RefreshToken = refreshToken
token.TokenType = "Bearer" token.TokenType = "Bearer"
return token return token