Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break the code in files so it's easier to reason about it
- Loading branch information
Showing
3 changed files
with
109 additions
and
100 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |