Last active
April 2, 2018 20:03
-
-
Save eef/2b48e21e56301844680ad7fd174b7c68 to your computer and use it in GitHub Desktop.
GoLang script to pull your daily duration coding. You must have your wakatime api key in $HOME/.wakatime.cfg. This will be present if you are using wakatime anywhere else.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"regexp" | |
"strings" | |
"time" | |
) | |
// Duration model | |
type Duration struct { | |
Duration float64 `json:"duration"` | |
IsDebugging bool `json:"is_debugging"` | |
Project string `json:"project"` | |
Time float64 `json:"time"` | |
} | |
// WakatimeResponse model | |
type WakatimeResponse struct { | |
Data []Duration `json:"data"` | |
} | |
func main() { | |
apiKey := getAPIKey() | |
t := time.Now() | |
var totalTime = 0.0 | |
var url = "https://wakatime.com/api/v1/users/current/durations?date=" + t.Format("2006-01-02") + "&api_key=" + apiKey | |
body, err := makeRequest(url) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
s, err := parseDurations(body) | |
for _, elem := range s.Data { | |
totalTime += elem.Duration | |
} | |
fmt.Printf("%.2f\n", secondsToHours(totalTime)) | |
} | |
// makeRequest - Get the json from WakaTime API | |
func makeRequest(url string) ([]byte, error) { | |
res, err := http.Get(url) | |
if err != nil { | |
return nil, err | |
} | |
body, err := ioutil.ReadAll(res.Body) | |
if err != nil { | |
return nil, err | |
} | |
return []byte(body), err | |
} | |
// parseDurations Get the list of durations | |
func parseDurations(body []byte) (*WakatimeResponse, error) { | |
var s = new(WakatimeResponse) | |
err := json.Unmarshal(body, &s) | |
if err != nil { | |
fmt.Println("Error unmarshalling Wakatime JSON:", err) | |
} | |
return s, err | |
} | |
// getAPIKey | |
func getAPIKey() string { | |
lines, err := readConfig() | |
var apiKeyString string | |
if err != nil { | |
log.Fatalf("Error reading config: %s", err) | |
} | |
for i, line := range lines { | |
matched, err := regexp.MatchString("api_key", line) | |
if err != nil { | |
fmt.Printf("Error matching api key") | |
fmt.Printf("%d", i) | |
} | |
if matched { | |
apiKeyString = strings.Split(line, "=")[1] | |
} | |
} | |
return strings.TrimSpace(apiKeyString) | |
} | |
func readConfig() ([]string, error) { | |
file, err := os.Open(os.Getenv("HOME") + "/.wakatime.cfg") | |
if err != nil { | |
return nil, err | |
} | |
defer file.Close() | |
var lines []string | |
scanner := bufio.NewScanner(file) | |
for scanner.Scan() { | |
lines = append(lines, scanner.Text()) | |
} | |
return lines, scanner.Err() | |
} | |
// secondsToHours | |
func secondsToHours(inSeconds float64) float64 { | |
minutes := inSeconds / 60 | |
hours := minutes / 60 | |
return hours | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment