Skip to content

Commit

Permalink
Break the code in files so it's easier to reason about it
Browse files Browse the repository at this point in the history
  • Loading branch information
lucapette committed Feb 1, 2017
1 parent d6d7e63 commit 3ef4db8
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 100 deletions.
63 changes: 63 additions & 0 deletions 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),
}
}
109 changes: 9 additions & 100 deletions 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)
}
}
}()
Expand Down
37 changes: 37 additions & 0 deletions 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
}

0 comments on commit 3ef4db8

Please sign in to comment.