Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cyclic import and #806 #807

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 1 addition & 23 deletions mps_youtube/commands/lastfm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import time
import datetime

try:
import pylast
has_pylast = True
Expand All @@ -10,6 +7,7 @@
from .. import g, util, config
from . import command


@command(r'lastfm_connect')
def init_network(verbose=True):
""" Initialize the global pylast network variable """
Expand Down Expand Up @@ -39,23 +37,3 @@ def init_network(verbose=True):
except (pylast.WSError, pylast.MalformedResponseError, pylast.NetworkError):
if verbose:
g.message = "Last.fm connection error: %s" % (str(e))

def scrobble_track(artist, album, track):
""" Scrobble a track to the user's Last.fm account """
if not g.lastfm_network:
return
unix_timestamp = int(time.mktime(datetime.datetime.now().timetuple()))
try:
g.lastfm_network.scrobble(artist=artist, title=track, album=album,
timestamp=unix_timestamp)
except (pylast.WSError, pylast.MalformedResponseError, pylast.NetworkError):
return

def set_now_playing(artist, track):
""" Set the current track as "now playing" on the user's Last.fm account """
if not g.lastfm_network:
return
try:
g.lastfm_network.update_now_playing(artist=artist, title=track)
except (pylast.WSError, pylast.MalformedResponseError, pylast.NetworkError):
return
17 changes: 7 additions & 10 deletions mps_youtube/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import subprocess
import socket
from urllib.error import HTTPError, URLError
from abc import ABCMeta, abstractmethod


from . import g, screen, c, streams, history, content, config, util
from .commands import lastfm


mswin = os.name == "nt"
Expand Down Expand Up @@ -66,7 +64,7 @@ def play(self, songlist, shuffle=False, repeat=False, override=False):
self.softrepeat = repeat and len(self.songlist) == 1

if g.scrobble:
lastfm.set_now_playing(g.artist, g.scrobble_queue[self.song_no])
util.lastfm_set_now_playing(g.artist, g.scrobble_queue[self.song_no])

try:
self.video, self.stream = stream_details(self.song,
Expand Down Expand Up @@ -97,8 +95,6 @@ def play(self, songlist, shuffle=False, repeat=False, override=False):

# To be defined by subclass based on being cmd player or library
# When overriding next and previous don't forget to add the following
# if g.scrobble:
# lastfm.scrobble_track(g.artist, g.album, g.scrobble_queue[self.song_no])
def next(self):
pass

Expand Down Expand Up @@ -241,21 +237,22 @@ class CmdPlayer(BasePlayer):

def next(self):
if g.scrobble:
lastfm.scrobble_track(g.artist, g.album,
g.scrobble_queue[self.song_no])
util.lastfm_scrobble_track(g.artist, g.album,
g.scrobble_queue[self.song_no])
self.terminate_process()
self.song_no += 1

def previous(self):
if g.scrobble:
lastfm.scrobble_track(g.artist, g.album,
g.scrobble_queue[self.song_no])
util.lastfm_scrobble_track(g.artist, g.album,
g.scrobble_queue[self.song_no])
self.terminate_process()
self.song_no -= 1

def stop(self):
self.terminate_process()
self.song_no = len(self.songlist)
raise KeyboardInterrupt
# self.song_no = len(self.songlist)

def terminate_process(self):
self.p.terminate()
Expand Down
1 change: 0 additions & 1 deletion mps_youtube/players/mplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def launch_player(self, cmd):
stderr=subprocess.STDOUT, bufsize=1)
self._player_status(self.songdata + "; ", self.song.length)
returncode = self.p.wait()
print(returncode)

if returncode == 42:
self.previous()
Expand Down
26 changes: 26 additions & 0 deletions mps_youtube/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

from importlib import import_module

try:
import pylast
except ImportError:
pass

mswin = os.name == "nt"
not_utf8_environment = mswin or "UTF-8" not in sys.stdout.encoding
Expand Down Expand Up @@ -573,3 +577,25 @@ def complete_command(self, text, state):
else:
results = [x for x in self.COMMANDS if x.startswith(text)] + [None]
return results[state]


def lastfm_scrobble_track(artist, album, track):
""" Scrobble a track to the user's Last.fm account """
if not g.lastfm_network:
return
unix_timestamp = int(time.mktime(datetime.datetime.now().timetuple()))
try:
g.lastfm_network.scrobble(artist=artist, title=track, album=album,
timestamp=unix_timestamp)
except (pylast.WSError, pylast.MalformedResponseError, pylast.NetworkError):
return


def lastfm_set_now_playing(artist, track):
""" Set the current track as "now playing" on the user's Last.fm account """
if not g.lastfm_network:
return
try:
g.lastfm_network.update_now_playing(artist=artist, title=track)
except (pylast.WSError, pylast.MalformedResponseError, pylast.NetworkError):
return