Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[bcd9d0]: tags / 0.2.1 / yulastfm.pl Maximize Restore History

Download this file

yulastfm.pl    233 lines (203 with data), 6.9 kB

  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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/perl -w
# YuLastfm - Update Pidgin TUNE status from Last.fm
# Author: Chih-Chun Lin
# Project Homepage:
# https://sourceforge.net/projects/yulastfm/
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
use warnings;
use Purple;
#program version
my $VERSION="0.2.1";
#global variable
our $plugin;
our $lastfm_account;
our $url;
our $api_key = "7aed2418de442e85a6e7af89f325d8f6";
our $trk_limit = "1";
our $url_pre = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=";
our $url_suf = "&api_key=" . $api_key . "&limit=" . $trk_limit;
our $refresh_rate = "60";
our $presence;
our $timeout = 0;
our $cur_artist;
our $cur_title;
our $cur_album;
our $cur_date;
%PLUGIN_INFO = (
perl_api_version => 2,
name => "YuLastfm",
version => $VERSION,
summary => "This perl script is a plugin which help you update your " .
"\"now playing\" information from the Last.fm musical " .
"social network.",
description => "Updater for Pidgin TUNE status from Last.FM",
author => "Lin, Chih-Chun",
url => "https://sourceforge.net/projects/yulastfm/",
load => "plugin_load",
unload => "plugin_unload",
plugin_action_sub => "plugin_actions_cb",
prefs_info => "prefs_info_cb"
);
sub plugin_init {
return %PLUGIN_INFO;
}
sub plugin_load {
$plugin = shift;
dbg_msg( "plguin: " . $plugin);
Purple::Prefs::add_none( "/plugins/core/yulastfm");
Purple::Prefs::add_string( "/plugins/core/yulastfm/username", "");
Purple::Prefs::add_int( "/plugins/core/yulastfm/refreshing_rate", 60);
Purple::Prefs::connect_callback(
$plugin,
"/plugins/core/yulastfm/refreshing_rate",
\&pref_rate_cb,
"refresh rate"
);
refresh_tune();
dbg_msg("yulastfm loaded");
}
sub plugin_unload {
my @accounts = Purple::Accounts::get_all();
my $i = 0;
while ( $i<=$#accounts) {
if ( $account = $accounts[$i]) {
if( $presence = Purple::Account::get_presence($account)) {
Purple::Util::set_current_song( "", "", "");
}
}
$i++;
}
if ($timeout) {
dbg_msg( "remove existing timeout: " . $timeout);
Purple::timeout_remove($timeout);
$timeout = 0;
}
dbg_msg("yulastfm unloaded");
}
sub plugin_actions_cb {
my @actions = ("Refresh Tune");
}
%plugin_actions = (
"Refresh Tune" => \&refresh_tune
);
sub prefs_info_cb {
my $frame = Purple::PluginPref::Frame->new();
my $ppref = Purple::PluginPref->new_with_label("YuLastfm");
$ppref->set_type(2);
$frame->add($ppref);
$ppref = Purple::PluginPref->new_with_name_and_label(
"/plugins/core/yulastfm/username",
"Last.fm User");
$ppref->set_type(3); # PURPLE_PLUGIN_PREF_STRING_FORMAT
$frame->add($ppref);
$ppref = Purple::PluginPref->new_with_name_and_label(
"/plugins/core/yulastfm/refreshing_rate",
"Refresh Rate (per sec.)");
$ppref->set_type(3);
# If no bounds are assigned, the value will be restriced to 0
$ppref->set_bounds( 10, 300);
# this line shows how to call "etan1" patched get_bounds()
# ( $min_v, $max_v) = $ppref->get_bounds();
$frame->add($ppref);
return $frame;
}
sub pref_rate_cb {
dbg_msg("pref_rate_cb: \"refresh rate\" changed");
if ($timeout) {
Purple::timeout_remove($timeout);
$timeout = 0;
}
refresh_tune();
}
sub refresh_tune {
$lastfm_account = Purple::Prefs::get_string("/plugins/core/yulastfm/username");
if (!$lastfm_account) { # no account present
dbg_msg("No Last.fm account present!");
Purple::Notify::message(
$plugin,
1, # Warning Type
"YuLastfm",
"A valid Last.fm account is required:",
"This warning message usually implies that you either " .
"haven't done the configuration yet or you have given " .
"an invalid Last.fm account. You have to give a valid " .
"Last.fm account to make YuLastfm work. You can remedy " .
"it in plugins preference setting interface of pidgin. ".
"It locates on menu path\n\n\tTools->Plugins.\n\nFind " .
"YuLastfm, and click Configure Plugin button below to " .
"do further configuration. After configuration, either " .
"reloading the plugin or refreshing it can activate the " .
"lastest configuration. You can refresh it by click " .
"menu path\n\n\tTools->YuLastfm->Refresh Tune.",
NULL, NULL
);
$timeout = 0;
dbg_msg("timeout stoped.");
return 0;
} elsif ( length($lastfm_account) < 2 || length($lastfm_account) > 15) {
dbg_msg("Invalid Last.fm account! (ID Length must between 2-15)");
$timeout = 0;
dbg_msg("timeout stoped.");
return 0;
} else {
$url = $url_pre . $lastfm_account . $url_suf;
Purple::Util::fetch_url( $plugin, $url, TURE, "Mozilla/5.0", TURE, \&parse_xml);
$refresh_rate = Purple::Prefs::get_int("/plugins/core/yulastfm/refreshing_rate");
if (!$timeout) {
$timeout = Purple::timeout_add( $plugin, $refresh_rate, \&refresh_tune, $plugin);
}
dbg_msg("timeout continues.");
return 1;
}
}
sub parse_xml {
# dbg_msg($_[0]); # dump raw xml
dbg_msg("parsing xml");
if (!$_[0]){
dbg_msg("Cannot get anything from Last.fm!");
return;
}
my $rootXmlnode = Purple::XMLNode::from_str($_[0]);
my $lfmStatus = $rootXmlnode->get_attrib("status");
dbg_msg( "lfm status: " . $lfmStatus);
if ( $lfmStatus == "ok") {
dbg_msg("web query status: ok");
my $trackXmlnode = $rootXmlnode->get_child("recenttracks")->get_child("track");
if ( $trackXmlnode->get_attrib("nowplaying") ) {
# now playing
$cur_artist = $trackXmlnode->get_child("artist")->get_data();
$cur_title = $trackXmlnode->get_child("name")->get_data();
$cur_album = $trackXmlnode->get_child("album")->get_data();
$cur_date = $trackXmlnode->get_child("date")->get_attrib("uts");
dbg_msg("artist: " . $cur_artist);
dbg_msg("name: " . $cur_title);
dbg_msg("album: " . $cur_album);
dbg_msg("date: " . $cur_date);
Purple::Util::set_current_song( $cur_title, $cur_artist, $cur_album);
} else {
# no music is playing
# TODO something need to take care in this situation
dbg_msg("no music is playing");
Purple::Util::set_current_song( "", "", "");
}
} elsif ( $lfmStatus == "failed") {
dbg_msg("web query status: failed");
} else {
dbg_msg( "unkown web query status:" . $lfmStatus);
}
}
sub dbg_msg {
Purple::Debug::info( "yulastfm: ", $_[0] . "\n");
}