diff --git a/activity.go b/activity.go new file mode 100644 index 0000000..585b4cc --- /dev/null +++ b/activity.go @@ -0,0 +1,63 @@ +package main + +import ( + "bytes" + "fmt" + "io/ioutil" + "net/http" + "time" +) + +type Category struct { + Name string + Score int +} + +type Activity struct { + Name string + Category +} + +func (a Activity) Store() error { + b := bytes.NewBufferString(fmt.Sprintf("activity,category=%s,score=%d value=\"%s\"", a.Category.Name, a.Category.Score, a.Name)) + req, err := http.NewRequest("POST", "http://localhost:8086/write", b) + if err != nil { + return err + } + req.Header.Set("Content-Type", "") + req.Header.Set("User-Agent", "lucapette/t") + + params := req.URL.Query() + params.Set("db", "me") + req.URL.RawQuery = params.Encode() + + c := &http.Client{Timeout: 100 * time.Millisecond} + resp, err := c.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + + if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusOK { + var err = fmt.Errorf(string(body)) + return err + } + + return nil +} + +func findCategory(name string) Category { + return Category{Name: "Stuff", Score: 42} +} + +func NewActivity(name string) Activity { + return Activity{ + Name: name, + Category: findCategory(name), + } +} diff --git a/main.go b/main.go index fc3e5a4..4a66898 100644 --- a/main.go +++ b/main.go @@ -1,117 +1,26 @@ package main import ( - "bytes" - "fmt" - "io/ioutil" "log" - "net/http" "os" - "os/exec" "os/signal" - "strings" "time" ) -var script = ` -tell application "System Events" - set frontApp to name of first application process whose frontmost is true - - set activeURL to "" - if frontApp is "Google Chrome" then - tell application "Google Chrome" - set normalWindows to (windows whose mode is not "incognito") - - if length of normalWindows is greater than 0 then - set activeURL to (get URL of active tab of (first item of normalWindows)) - end if - end tell - end if -end tell - -frontApp & "," & (activeURL) as String -` - -type category struct { - name string - score int -} - -type activity struct { - name string - category -} - -func (a activity) store() error { - b := bytes.NewBufferString(fmt.Sprintf("activity,category=%s,score=%d value=\"%s\"", a.category.name, a.category.score, a.name)) - req, err := http.NewRequest("POST", "http://localhost:8086/write", b) - if err != nil { - return err - } - req.Header.Set("Content-Type", "") - req.Header.Set("User-Agent", "lucapette/t") - - params := req.URL.Query() - params.Set("db", "me") - req.URL.RawQuery = params.Encode() - - c := &http.Client{Timeout: 100 * time.Millisecond} - resp, err := c.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return err - } - - if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusOK { - var err = fmt.Errorf(string(body)) - return err - } - - return nil -} - -func findCategory(activity string) category { - return category{name: "Stuff", score: 42} -} - -func currentActivity() (ac activity) { - cmd := exec.Command("osascript", "-") - cmd.Stdin = bytes.NewBufferString(script) - output, err := cmd.Output() - if err != nil { - log.Fatal(err) - } - - values := strings.Split(string(output), ",") - - for i := range values { - values[i] = strings.Replace(values[i], "\n", "", -1) - } - - if len(values[1]) == 0 { - ac.name = values[0] - } else { - ac.name = values[1] - } - - ac.category = findCategory(ac.name) - - return ac -} - func main() { go func() { tick := time.Tick(1 * time.Second) for range tick { - a := currentActivity() - err := a.store() + name, err := GetActivityName() + if err != nil { + log.Println(err) + } + + a := NewActivity(name) + + err = a.Store() if err != nil { - log.Fatal(err) + log.Println(err) } } }() diff --git a/script.go b/script.go new file mode 100644 index 0000000..4b1ce1c --- /dev/null +++ b/script.go @@ -0,0 +1,37 @@ +package main + +import ( + "bytes" + "os/exec" +) + +const script = ` +tell application "System Events" + set frontApp to name of first application process whose frontmost is true + + set activeURL to "" + if frontApp is "Google Chrome" then + tell application "Google Chrome" + set normalWindows to (windows whose mode is not "incognito") + + if length of normalWindows is greater than 0 then + set activeURL to (get URL of active tab of (first item of normalWindows)) + end if + end tell + end if +end tell + +if activeURL is not "" then + activeURL +else + frontApp +end if +` + +func GetActivityName() (string, error) { + cmd := exec.Command("osascript", "-") + cmd.Stdin = bytes.NewBufferString(script) + output, err := cmd.Output() + + return string(output), err +}