#!/usr/bin/python # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # # # # Plugin for iSida Jabber Bot # # Copyright (C) diSabler # # # # 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 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 General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see . # # # # --------------------------------------------------------------------------- # global execute, lf_api, lfm_url lfm_url = 'http://ws.audioscrobbler.com/2.0/' def last_text_cnt(text,c): text = reduce_spaces_all(text).split(' ') try: cnt = int(text[1]) except: cnt = GT('lastfm_max_limit') cnt += c text = text[0] return text,cnt def last_check_ascii(type, jid, nick, text): for tmp in text: if tmp > '~' or tmp < ' ': send_msg(type, jid, nick, L('Error!','%s/%s'%(jid,nick))) return True return None def last_time_short(tm): tm = time.localtime(tm) tnow = time.localtime() if tm[0] != tnow[0]: form = '%d.%m.%Y %H:%M' elif tm[1]!=tnow[1] or tm[2]!=tnow[2]: form = '%d.%m %H:%M' else: form = '%H:%M' return str(time.strftime(form,tm)) def last_date_now(body): if 'nowplaying=\"true\"' in body: return 'now' else: try: return last_time_short(int(get_subtag(get_tag_full(body,'date'),'uts'))) except: return 'Unknown' def lastonetrack(type, jid, nick, text): if not text: text = nick if last_check_ascii(type, jid, nick, text): return ms = lf_api('user.getrecenttracks',text, '=2: msg = L('Last track %s: %s - %s %s','%s/%s'%(jid,nick)) % (text,get_tag(ms[1],'artist'),get_tag(ms[1],'name'),'['+last_date_now(ms[1])+']') else: msg = L('Unavailable!','%s/%s'%(jid,nick)) send_msg(type, jid, nick, msg) def lf_api(method, user, splitter): user = reduce_spaces_all(user.lower().encode('utf-8').replace('\\x','%')).replace(' ','%20') link = '%s?method=%s&user=%s&api_key=%s' % (lfm_url,method,user,GT('lfm_api')) return rss_replace(html_encode(load_page(link))).split(splitter) def lasttracks(type, jid, nick, text): if not text: text = nick if last_check_ascii(type, jid, nick, text): return text,cnt = last_text_cnt(text,1) ms = lf_api('user.getrecenttracks',text, ' len(ms): cnt = len(ms) msg = L('Top tracks %s:','%s/%s'%(jid,nick)) % text for a in ms[1:cnt]: b = a.split('= 30]