forked from christianvozar/itunes-hipchat-status
/
itunes-hipchat-status.go
152 lines (126 loc) · 3.74 KB
/
itunes-hipchat-status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2014, Rogue Ethic, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Contributor(s):
// Christian Vozar (christian@rogueethic.com)
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strings"
)
const (
HipChatApiUrl = "https://api.hipchat.com"
HipChatApiVersion = "v2"
)
type HipChatUser struct {
Name string `json:"name,omitempty"`
Title string `json:"title,omitempty"`
Presence presenceInfo `json:"presence,omitempty"`
MentionName string `json:"mention_name,omitempty"`
Timezone string `json:"timezone,omitempty"`
Email string `json:"email,omitempty"`
IsGroupAdmin bool `json:"is_group_admin"`
}
type presenceInfo struct {
Status string `json:"status, show"`
Show string `json:"show, show"`
}
func main() {
flagVersion := flag.Bool("version", false, "Display application version.")
flagUser := flag.String("user", "", "Atlassian HipChat ID or Email of user to update.")
flagAuthToken := flag.String("token", "", "Atlassian HipChat API v2 authentication token.")
flag.Parse()
// Handle no command-line paramters
if flag.NFlag() == 0 {
flag.PrintDefaults()
os.Exit(0)
}
// Output version
if *flagVersion {
fmt.Println("iTunes to Atlassian HipChat Status", Version)
os.Exit(0)
}
userInformation := viewHipChatUser(*flagUser, *flagAuthToken)
userInformation.Presence.Status = getItunesInformation()
if userInformation.Presence.Show == "" {
userInformation.Presence.Show = "chat"
}
updateHipChatUser(userInformation, *flagUser, *flagAuthToken)
}
func updateHipChatUser(u HipChatUser, e, a string) {
messageUri := fmt.Sprintf("%s/%s/user/%s?auth_token=%s", HipChatApiUrl, HipChatApiVersion, e, a)
messagePayload, err := json.Marshal(u)
if err != nil {
log.Fatal(err)
}
body := strings.NewReader(string(messagePayload))
httpClient := &http.Client{}
req, err := http.NewRequest("PUT", messageUri, body)
req.Header.Add("content-type", "application/json")
_, err = httpClient.Do(req)
if err != nil {
log.Fatal(err)
}
}
func viewHipChatUser(e, a string) HipChatUser {
var hipChatData HipChatUser
messageUri := fmt.Sprintf("%s/%s/user/%s?auth_token=%s", HipChatApiUrl, HipChatApiVersion, e, a)
res, err := http.Get(messageUri)
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(body, &hipChatData)
return hipChatData
}
func getItunesInformation() string {
appleScriptRuntime := "osascript"
arg0 := "-e"
cmd := exec.Command(appleScriptRuntime, arg0, `tell application "iTunes"
if it is running then
set trackname to name of current track
set artistname to artist of current track
set albumname to album of current track
if artistname is null then
set artistshow to ""
else if artistname is "" then
set artistshow to ""
else
set artistshow to " | " & artistname & ""
end if
set output to trackname & artistshow
end if
end tell`)
out, err := cmd.Output()
if err != nil {
return ""
}
iTunesInformation := strings.TrimSpace(string(out))
// HipChat status cannot exceed 50 characters.
if len(iTunesInformation) > 50 {
return iTunesInformation[0:46] + "..."
}
return iTunesInformation
}