本文整理匯總了Python中scrobbler.Scrobbler.now_playing方法的典型用法代碼示例。如果您正苦於以下問題:Python Scrobbler.now_playing方法的具體用法?Python Scrobbler.now_playing怎麽用?Python Scrobbler.now_playing使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scrobbler.Scrobbler
的用法示例。
在下文中一共展示了Scrobbler.now_playing方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from scrobbler import Scrobbler [as 別名]
# 或者: from scrobbler.Scrobbler import now_playing [as 別名]
#.........這裏部分代碼省略.........
f2 = open('channels.json', 'w')
json.dump({
'user_name': self.douban.user_name,
'user_id': self.douban.user_id,
'expire': self.douban.expire,
'token': self.douban.token
}, f)
json.dump(self.channels, f2)
except IOError:
raise Exception("Unable to write cache file")
def get_channels(self):
if self.channels is None:
self.channels = self.douban.get_channels()
return self.channels
def _choose_channel(self, channel):
self.current_channel = channel
self.current_play_list = deque(self.douban.get_new_play_list(self.current_channel))
def _play_track(self):
_song = self.current_play_list.popleft()
self.current_song = Song(_song)
self.song_change_alarm = self.main_loop.set_alarm_in(self.current_song.length_in_sec,
self.next_song, None);
self.selected_button.set_text(self.selected_button.text[0:7].strip())
heart = u'\N{WHITE HEART SUIT}';
if self.current_song.like:
heart = u'\N{BLACK HEART SUIT}'
self.selected_button.set_text(self.selected_button.text + ' ' + heart + ' ' +
self.current_song.artist + ' - ' +
self.current_song.title)
if self.scrobbling:
self.scrobbler.now_playing(self.current_song.artist, self.current_song.title,
self.current_song.album_title, self.current_song.length_in_sec)
self.player.stop()
self.player.play(self.current_song)
# Currently playing the second last song in queue
if len(self.current_play_list) == 1:
playing_list = self.douban.get_playing_list(self.current_song.sid, self.current_channel)
self.current_play_list.extend(deque(playing_list))
def next_song(self, loop, user_data):
# Scrobble the track if scrobbling is enabled
# and total playback time of the track > 30s
if self.scrobbling and self.current_song.length_in_sec > 30:
self.scrobbler.submit(self.current_song.artist, self.current_song.title,
self.current_song.album_title, self.current_song.length_in_sec)
self.douban.end_song(self.current_song.sid, self.current_channel)
if self.song_change_alarm:
self.main_loop.remove_alarm(self.song_change_alarm)
self._play_track()
def skip_current_song(self):
self.douban.skip_song(self.current_song.sid, self.current_channel)
if self.song_change_alarm:
self.main_loop.remove_alarm(self.song_change_alarm)
self._play_track()
def rate_current_song(self):
self.douban.rate_song(self.current_song.sid, self.current_channel)
self.selected_button.set_text(self.selected_button.text.replace(u'\N{WHITE HEART SUIT}', u'\N{BLACK HEART SUIT}'))
示例2: Doubanfm
# 需要導入模塊: from scrobbler import Scrobbler [as 別名]
# 或者: from scrobbler.Scrobbler import now_playing [as 別名]
class Doubanfm(object):
def __init__(self):
self.login_data = {}
self.lastfm = True # lastfm 登陸
def init_login(self):
print LOGO
self.douban_login() # 登陸
self.lastfm_login() # 登陸 last.fm
print '\033[31m♥\033[0m Get channels ',
self.get_channels() # 獲取頻道列表
print '[\033[32m OK \033[0m]'
# 存儲的default_channel是行數而不是真正發送數據的channel_id
# 這裏需要進行轉化一下
self.set_channel(self.default_channel)
print '\033[31m♥\033[0m Check PRO ',
# self.is_pro()
print '[\033[32m OK \033[0m]'
def win_login(self):
'''登陸界麵'''
email = raw_input('Email: ')
password = getpass.getpass('Password: ')
return email, password
def lastfm_login(self):
'''Last.fm登陸'''
# username & password
self.last_fm_username = \
self.login_data['last_fm_username'] if 'last_fm_username' in self.login_data\
else None
self.last_fm_password = \
self.login_data['last_fm_password'] if 'last_fm_password' in self.login_data\
else None
if len(sys.argv) > 1 and sys.argv[1] == 'last.fm':
from hashlib import md5
username = raw_input('Last.fm username: ') or None
password = getpass.getpass('Last.fm password :') or None
if username and password:
self.last_fm_username = username
self.last_fm_password = md5(password).hexdigest()
with open(config.PATH_TOKEN, 'r') as f:
data = pickle.load(f)
with open(config.PATH_TOKEN, 'w') as f:
data['last_fm_username'] = username
data['last_fm_password'] = self.last_fm_password
pickle.dump(data, f)
# login
if self.lastfm and self.last_fm_username and self.last_fm_password:
self.scrobbler = Scrobbler(
self.last_fm_username, self.last_fm_password)
r, err = self.scrobbler.handshake()
if r:
logger.info("Last.fm login succeeds!")
print '\033[31m♥\033[0m Last.fm logged in: %s' % self.last_fm_username
else:
logger.error("Last.fm login fails: " + err)
self.lastfm = False
else:
self.lastfm = False
def __last_fm_account_required(func):
'''裝飾器,用於需要登錄Last.fm後才能使用的接口'''
@wraps(func)
def wrapper(self, *args, **kwds):
if not self.lastfm:
return
# Disable pylint callable check due to pylint's incompability
# with using a class method as decorator.
# Pylint will consider func as "self"
return func(self, *args, **kwds) # pylint: disable=not-callable
return wrapper
@__last_fm_account_required
def submit_current_song(self):
'''提交播放過的曲目'''
# Submit the track if total playback time of the track > 30s
if self.playingsong['length'] > 30:
self.scrobbler.submit(
self.playingsong['artist'],
self.playingsong['title'],
self.playingsong['albumtitle'],
self.playingsong['length']
)
@__last_fm_account_required
def scrobble_now_playing(self):
'''提交當前正在播放曲目'''
self.scrobbler.now_playing(
self.playingsong['artist'],
self.playingsong['title'],
self.playingsong['albumtitle'],
self.playingsong['length']
)
def douban_login(self):
'''登陸douban.fm獲取token'''
if os.path.exists(config.PATH_TOKEN):
# 已登陸
#.........這裏部分代碼省略.........
示例3: __init__
# 需要導入模塊: from scrobbler import Scrobbler [as 別名]
# 或者: from scrobbler.Scrobbler import now_playing [as 別名]
#.........這裏部分代碼省略.........
if self.channels is None:
self.channels = deque(self.douban.get_channels())
def _choose_channel(self, channel):
self.current_channel = channel
self.current_play_list = deque(
self.douban.get_new_play_list(self.current_channel))
def _play_track(self):
_song = self.current_play_list.popleft()
self.current_song = Song(_song)
logger.debug('Playing Track')
logger.debug('Artist: ' + self.current_song.artist)
logger.debug('Title: ' + self.current_song.song_title)
logger.debug('Album: ' + self.current_song.album_title)
logger.debug('Length: ' + self.current_song.length_in_str)
logger.debug('Sid: ' + self.current_song.sid)
logger.debug(
'{0} tracks remaining in the playlist'.format(len(self.current_play_list)))
self.song_change_alarm = self.main_loop.set_alarm_in(self.current_song.length_in_sec,
self.next_song, None)
self.selected_button.set_text(self.selected_button.text[0:11].strip())
heart = u'\N{WHITE HEART SUIT}'
if self.current_song.like:
heart = u'\N{BLACK HEART SUIT}'
if not self.douban_account:
heart = ' '
self.selected_button.set_text(self.selected_button.text + ' ' + heart + ' ' +
self.current_song.artist + ' - ' +
self.current_song.song_title)
if self.scrobbling:
self.scrobbler.now_playing(self.current_song.artist, self.current_song.song_title,
self.current_song.album_title, self.current_song.length_in_sec)
self.player.stop()
self.player.play(self.current_song)
# Currently playing the second last song in queue
if len(self.current_play_list) == 1:
# Extend the playing list
playing_list = self.douban.get_playing_list(
self.current_song.sid, self.current_channel)
logger.debug('Got {0} more tracks'.format(len(playing_list)))
self.current_play_list.extend(deque(playing_list))
def next_song(self, loop, user_data):
# Scrobble the track if scrobbling is enabled
# and total playback time of the track > 30s
if self.scrobbling and self.current_song.length_in_sec > 30:
self.scrobbler.submit(self.current_song.artist, self.current_song.song_title,
self.current_song.album_title, self.current_song.length_in_sec)
if self.douban_account:
r, err = self.douban.end_song(
self.current_song.sid, self.current_channel)
if r:
logger.debug('End song OK')
else:
logger.error(err)
if self.song_change_alarm:
self.main_loop.remove_alarm(self.song_change_alarm)
self._play_track()
def skip_current_song(self):
if self.douban_account:
示例4: Doubanfm
# 需要導入模塊: from scrobbler import Scrobbler [as 別名]
# 或者: from scrobbler.Scrobbler import now_playing [as 別名]
class Doubanfm(object):
def __init__(self):
self.login_data = {}
self.lastfm = True # lastfm 登陸
def init_login(self):
print '''
──╔╗─────╔╗────────╔═╗
──║║─────║║────────║╔╝
╔═╝╠══╦╗╔╣╚═╦══╦═╗╔╝╚╦╗╔╗
║╔╗║╔╗║║║║╔╗║╔╗║╔╗╬╗╔╣╚╝║
║╚╝║╚╝║╚╝║╚╝║╔╗║║║╠╣║║║║║
╚══╩══╩══╩══╩╝╚╩╝╚╩╩╝╚╩╩╝
'''
self.douban_login() # 登陸
self.login_lastfm() # 登陸 last.fm
print '\033[31m♥\033[0m Get channels ',
# self.get_channels() # 獲取頻道列表
print '[\033[32m OK \033[0m]'
# self.get_channellines() # 重構列表用以顯示
print '\033[31m♥\033[0m Check PRO ',
# self.is_pro()
print '[\033[32m OK \033[0m]'
def win_login(self):
'''登陸界麵'''
email = raw_input('Email:')
password = getpass.getpass('Password:')
return email, password
def login_lastfm(self):
'''Last.fm登陸'''
if self.lastfm and self.last_fm_username and self.last_fm_password:
self.scrobbler = Scrobbler(
self.last_fm_username, self.last_fm_password)
r, err = self.scrobbler.handshake()
if r:
logger.info("Last.fm login succeeds!")
print '\033[31m♥\033[0m Last.fm logged in: %s' % self.last_fm_username
else:
logger.error("Last.fm login fails: " + err)
self.lastfm = False
else:
self.lastfm = False
def last_fm_account_required(fun):
'''裝飾器,用於需要登錄Last.fm後才能使用的接口'''
@wraps(fun)
def wrapper(self, *args, **kwds):
if not self.lastfm:
return
return fun(self, *args, **kwds)
return wrapper
@last_fm_account_required
def submit_current_song(self):
'''提交播放過的曲目'''
# Submit the track if total playback time of the track > 30s
if self.playingsong['length'] > 30:
self.scrobbler.submit(
self.playingsong['artist'],
self.playingsong['title'],
self.playingsong['albumtitle'],
self.playingsong['length']
)
@last_fm_account_required
def scrobble_now_playing(self):
'''提交當前正在播放曲目'''
self.scrobbler.now_playing(
self.playingsong['artist'],
self.playingsong['title'],
self.playingsong['albumtitle'],
self.playingsong['length']
)
def douban_login(self):
'''登陸douban.fm獲取token'''
path_token = os.path.expanduser('~/.douban_token.txt')
if os.path.exists(path_token):
# 已登陸
logger.info("Found existing Douban.fm token.")
with open(path_token, 'r') as f:
self.login_data = pickle.load(f)
self.token = self.login_data['token']
self.user_name = self.login_data['user_name']
self.user_id = self.login_data['user_id']
self.expire = self.login_data['expire']
self.default_volume = int(self.login_data['volume'])\
if 'volume' in self.login_data else 50
self.default_channel = int(self.login_data['channel'])\
if 'channel' in self.login_data else 1
# 存儲的default_channel是行數而不是真正發送數據的channel_id
# 這裏需要進行轉化一下
self.set_channel(self.default_channel)
print '\033[31m♥\033[0m Get local token - Username: \033[33m%s\033[0m' % self.user_name
else:
# 未登陸
#.........這裏部分代碼省略.........