diff --git a/main.go b/main.go index 6bb7e0e..a696876 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "time" "golang.org/x/net/context" "golang.org/x/oauth2" @@ -11,16 +12,20 @@ import ( "fmt" "log" + "strconv" "github.com/go-openapi/strfmt" ESI "client" ESIPlanetaryInteraction "client/planetary_interaction" ESISkills "client/skills" + ESIUniverse "client/universe" + + "database/sql" + + _ "github.com/mattn/go-sqlite3" httptransport "github.com/go-openapi/runtime/client" - //"database/sql" - //_ "github.com/mattn/go-sqlite3" ) // Character - Structure to save the verification data. @@ -62,9 +67,12 @@ func main() { client := googleOauthConfig.Client(oauth2.NoContext, cToken) 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 { @@ -86,9 +94,6 @@ func getCharacterInfo(client *http.Client) *Character { return nil } - fmt.Printf("Name: %s\n", m.CharacterName) - fmt.Printf("Id: %d\n", m.CharacterID) - return &m } @@ -101,19 +106,62 @@ func getCharacterSkillQueue(client *http.Client, m *Character) { swaggerclient := ESI.New(transport, strfmt.Default) 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 + 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 { // element is the element from someSlice for where we are - name := "UNK-SKILL" - level := skill.FinishedLevel - id := skill.SkillID - startDate := skill.StartDate - endDate := skill.FinishDate + name := skillNames[*skill.SkillID] - 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 { // element is the element from someSlice for where we are name := "UNK-PLANET" - solarSystemName := "UNK-SYSTEM" - level := planet.UpgradeLevel - id := planet.PlanetID - solarSystem := planet.SolarSystemID - planetType := planet.PlanetType - lastUpdate := planet.LastUpdate - pins := planet.NumPins + pcallParam := ESIPlanetaryInteraction.NewGetCharactersCharacterIDPlanetsPlanetIDParams() + pcallParam.WithCharacterID(m.CharacterID).WithPlanetID(*planet.PlanetID) - 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", - name, *id, - solarSystemName, *solarSystem, - *planetType, - *level, *pins, - lastUpdate, + fmt.Printf(" %s (%d) %s - %s, level %d with %d structures - Updated %s\n", + name, *planet.PlanetID, + solarSystemInfo.SolarSystemName, + *planet.PlanetType, + *planet.UpgradeLevel, *planet.NumPins, + planet.LastUpdate, ) pesiresponse, _ := swaggerclient.PlanetaryInteraction.GetCharactersCharacterIDPlanetsPlanetID(pcallParam, nil) for _, pin := range pesiresponse.Payload.Pins { if pin.ExtractorDetails != nil { - fmt.Printf(" Extractor %d (%s), cycles of %d, %d per cycle\n", - *pin.PinID, + fmt.Printf(" Extractor - cycle started %s, %d per cycle, cycles of %ds\n", pin.LastCycleStart, - *pin.ExtractorDetails.CycleTime, *pin.ExtractorDetails.QtyPerCycle, + *pin.ExtractorDetails.CycleTime, ) } else if pin.SchematicID != 0 { // Get the schematic from ESI and cache it + schematicInfo := getSchematicsInformation(swaggerclient, pin.SchematicID) - fmt.Printf(" Factory %d (%s) %d\n", - *pin.PinID, + fmt.Printf(" Factory - cycle started %s, producing %s, cycles of %ds\n", pin.LastCycleStart, - pin.SchematicID, + schematicInfo.SchematicName, + schematicInfo.CycleTime, ) } 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 { http.HandleFunc("/", handleLogin) http.HandleFunc("/callback", handleAuthenticationCallback) @@ -243,39 +353,76 @@ func handleAuthenticationCallback(w http.ResponseWriter, r *http.Request) { log.Printf("Refresh token is %s\n", token.RefreshToken) + putCacheData("refreshToken", token.RefreshToken) + messages <- token } +func getCachedData(key string) 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) + } + + stmt, err := db.Prepare("SELECT value FROM properties WHERE id = ?") + if err != nil { + log.Fatal(err) + } + + defer stmt.Close() + var response string + + err = stmt.QueryRow(key).Scan(&response) + if err != nil { + return "" + } + + return response +} + +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 { - /* - db, err := sql.Open("sqlite3", "./foo.db") - 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) - } - - stmt, err := db.Prepare("SELECT value FROM properties WHERE id = ?") - if err != nil { - log.Fatal(err) - } - - defer stmt.Close() - var refreshToken string - - err = stmt.QueryRow("refreshToken").Scan(&refreshToken) - if err != nil { - refreshToken = "" - } - fmt.Println("Found token :" + refreshToken) - - return refreshToken - */ - log.Print("Using hardcoded refresh token") + refreshToken := getCachedData("refreshToken") token := new(oauth2.Token) - token.RefreshToken = "4sNssL9aVy6Sqf8JUT6Q1hPQjo1lpzJ0mrPIB417QFdz6YooWl9g78qaH2DkZVwq0" + token.RefreshToken = refreshToken token.TokenType = "Bearer" return token