From 25314fb373c7faca815856b2d7eb9fc2bbf60fce Mon Sep 17 00:00:00 2001 From: Hugo <hnouts@grandlyon.com> Date: Thu, 23 Apr 2020 11:34:52 +0200 Subject: [PATCH] Parsing var url from enedis body response --- main.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 7d4e881..f36ca07 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,61 @@ func main() { if err != nil { fmt.Println(err) } - fmt.Printf("%s\n", string(contents)) + // Get the response body as a string + pageContent := string(contents) + + // Find a substr + codeStartIndex := strings.Index(pageContent, "?code=") + if codeStartIndex == -1 { + fmt.Println("No code found") + } + codeStartIndex += 6 + + // Find the index of the closing tag + codeEndIndex := strings.Index(pageContent, "&state=") + if codeEndIndex == -1 { + fmt.Println("No closing tag for code found.") + } + + stateStartIndex := strings.Index(pageContent, "&state=") + if stateStartIndex == -1 { + fmt.Println("No state found") + } + stateStartIndex += 7 + + stateEndIndex := strings.Index(pageContent, "&usage_point_id=") + if stateEndIndex == -1 { + fmt.Println("No closing tag for state found.") + } + + + usageStartIndex := strings.Index(pageContent, "&usage_point_id=") + if usageStartIndex == -1 { + fmt.Println("No usage found") + } + usageStartIndex += 16 + + usageEndIndex := strings.Index(pageContent, "&usage_point_id=") + if usageEndIndex == -1 { + fmt.Println("No closing tag for usage found.") + } + usageEndIndex += 30 + + pageCode := []byte(pageContent[codeStartIndex:codeEndIndex]) + pageState := []byte(pageContent[stateStartIndex:stateEndIndex]) + pageUsage := []byte(pageContent[usageStartIndex:usageEndIndex]) + + // Print out the result + fmt.Printf("Page code: %s\n", pageCode) + fmt.Printf("Page state: %s\n", pageState) + fmt.Printf("Page usage: %s\n", pageUsage) + + state := strings.Split(pageState, "-")[0] + host := strings.Split(pageState, "-")[1] + + redir := "https://" + host + "/accounts/enedisoauth/redirect?code=" + pageCode + "&state="+ state +"&usage_point_id=" + pageUsage + fmt.Println(redir) + http.Redirect(w, r, redir, 302) } }) -- GitLab