-
Notifications
You must be signed in to change notification settings - Fork 1
/
Growl.plugin.php
180 lines (153 loc) · 5.71 KB
/
Growl.plugin.php
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
/**
*
* LICENSE: GNU Affero General Public License, version 3 (AGPLv3)
* Copyright 2001 - 2015 Ampache.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
class Ampachegrowl
{
public $name = 'Growl';
public $categories = 'scrobbling';
public $description = 'Send your played songs notification to Growl';
public $url = 'http://growl.info';
public $version ='000001';
public $min_ampache ='360003';
public $max_ampache ='999999';
// These are internal settings used by this class, run this->load to
// fill them out
private $address;
private $password;
private $message;
/**
* Constructor
* This function does nothing...
*/
public function __construct()
{
if (!@include_once(AmpConfig::get('prefix') . '/lib/vendor/jamiebicknell/Growl-GNTP/growl.gntp.php')) {
throw new Exception('Missing Growl-GNTP dependency.');
}
return true;
} // constructor
/**
* install
* This is a required plugin function. It inserts our preferences
* into Ampache
*/
public function install()
{
if (Preference::exists('growl_address')) {
return false;
}
Preference::insert('growl_address','Growl server address','127.0.0.1','25','string','plugins');
Preference::insert('growl_pass','Growl password','','25','string','plugins');
Preference::insert('growl_message','Growl notification message','%user now listening %artist - %title','25','string','plugins');
Preference::insert('growl_registered_address','Growl registered address','','25','string','internal');
return true;
} // install
/**
* uninstall
* This is a required plugin function. It removes our preferences from
* the database returning it to its original form
*/
public function uninstall()
{
Preference::delete('growl_address');
Preference::delete('growl_pass');
Preference::delete('growl_message');
Preference::delete('growl_registered_address');
return true;
} // uninstall
/**
* upgrade
* This is a recommended plugin function
*/
public function upgrade()
{
return true;
} // upgrade
/**
* save_songplay
* This takes care of queueing and then submitting the tracks.
*/
public function save_mediaplay($song)
{
// Only support songs
if (strtolower(get_class($song)) != 'song') {
return false;
}
// Before we start let's pull the last song submitted by this user
$previous = Stats::get_last_song($this->user_id);
$user = new User($this->user_id);
$diff = time() - $previous['date'];
// Make sure it wasn't within the last 10sec
if ($diff < 10) {
debug_event($this->name, 'Last song played within ' . $diff . ' seconds, not recording stats', '3');
return false;
}
// Make sure there's actually a server address before we keep going
if (!$this->address) {
debug_event($this->name, 'Server address missing', '3');
return false;
}
$message = str_replace("%user", $user->fullname, $this->message);
$message = str_replace("%artist", $song->f_artist_full, $message);
$message = str_replace("%album", $song->f_album_full, $message);
$message = str_replace("%title", $song->title, $message);
$growl = $this->get_growl();
$growl->notify('Now Playing', $message);
debug_event($this->name, 'Submission Successful', '5');
return true;
} // submit
public function get_growl()
{
$growl = new Growl($this->address, $this->password);
$growl->setApplication('Ampache', 'Now Playing');
return $growl;
}
/**
* load
* This loads up the data we need into this object, this stuff comes
* from the preferences.
*/
public function load($user)
{
$user->set_preferences();
$data = $user->prefs;
if (strlen(trim($data['growl_address']))) {
$this->address = trim($data['growl_address']);
} else {
debug_event($this->name,'No server address, not scrobbling','3');
return false;
}
$this->password = trim($data['growl_pass']);
$this->message = trim($data['growl_message']);
$registered_address = trim($data['growl_registered_address']);
$confhash = md5($this->address . ':' . $this->password);
if ($registered_address != $confhash) {
$growl = $this->get_growl();
$icon = AmpConfig::get('theme_path') . '/images/ampache.png';
$growl->registerApplication($icon);
debug_event($this->name, 'Growl registered.', '5');
Preference::update('growl_registered_address', $user->id, $confhash);
}
$this->user_id = $user->id;
return true;
} // load
} // end Ampachegrowl
?>