2011-01-15 01:57:43 +01:00
|
|
|
import clementine
|
|
|
|
|
|
|
|
from PyQt4.QtCore import QSettings, QUrl
|
|
|
|
from PyQt4.QtGui import QAction, QDesktopServices, QIcon, QMenu, \
|
|
|
|
QStandardItem
|
2011-01-15 16:48:09 +01:00
|
|
|
from PyQt4.QtNetwork import QNetworkRequest
|
2011-01-15 01:57:43 +01:00
|
|
|
import PyQt4.QtCore
|
|
|
|
|
|
|
|
import json
|
|
|
|
import operator
|
|
|
|
import os.path
|
|
|
|
|
2011-04-28 17:10:28 +02:00
|
|
|
class DigitallyImportedUrlHandler(clementine.UrlHandler):
|
2011-04-29 23:07:47 +02:00
|
|
|
def __init__(self, url_scheme, service):
|
2011-04-28 17:10:28 +02:00
|
|
|
clementine.UrlHandler.__init__(self, service)
|
2011-04-29 23:07:47 +02:00
|
|
|
self.url_scheme = url_scheme
|
2011-04-28 17:10:28 +02:00
|
|
|
self.service = service
|
|
|
|
|
|
|
|
self.last_original_url = None
|
|
|
|
self.task_id = None
|
|
|
|
|
|
|
|
def scheme(self):
|
2011-04-29 23:07:47 +02:00
|
|
|
return self.url_scheme
|
2011-04-28 17:10:28 +02:00
|
|
|
|
|
|
|
def StartLoading(self, original_url):
|
|
|
|
result = clementine.UrlHandler.LoadResult()
|
|
|
|
|
|
|
|
if self.task_id is not None:
|
|
|
|
return result
|
|
|
|
if self.service.PLAYLISTS[self.service.audio_type]["premium"] and \
|
|
|
|
(len(self.service.username) == 0 or len(self.service.password) == 0):
|
|
|
|
self.service.StreamError.emit(self.tr("You have selected a Premium-only audio type but do not have any account details entered"))
|
|
|
|
return result
|
|
|
|
|
|
|
|
key = original_url.host()
|
|
|
|
self.service.LoadStation(key)
|
|
|
|
|
|
|
|
# Save the original URL so we can emit it in the finished signal later
|
|
|
|
self.last_original_url = original_url
|
|
|
|
|
|
|
|
# Tell the user what's happening
|
|
|
|
self.task_id = clementine.task_manager.StartTask(self.tr("Loading stream"))
|
|
|
|
|
|
|
|
result.type_ = clementine.UrlHandler.LoadResult.WillLoadAsynchronously
|
|
|
|
result.original_url_ = original_url
|
|
|
|
return result
|
|
|
|
|
|
|
|
def LoadPlaylistFinished(self, reply):
|
|
|
|
reply.deleteLater()
|
|
|
|
|
|
|
|
if self.task_id is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Stop the spinner in the status bar
|
|
|
|
clementine.task_manager.SetTaskFinished(self.task_id)
|
|
|
|
self.task_id = None
|
|
|
|
|
|
|
|
# Try to parse the playlist
|
|
|
|
parser = clementine.PlaylistParser(clementine.library)
|
|
|
|
songs = parser.LoadFromDevice(reply)
|
|
|
|
|
|
|
|
# Failed to get the playlist?
|
|
|
|
if len(songs) == 0:
|
|
|
|
self.service.StreamError.emit("Error loading playlist '%s'" % reply.url().toString())
|
|
|
|
return
|
|
|
|
|
|
|
|
result = clementine.UrlHandler.LoadResult()
|
|
|
|
result.original_url_ = self.last_original_url
|
|
|
|
|
|
|
|
# Take the first track in the playlist
|
|
|
|
result.type_ = clementine.UrlHandler.LoadResult.TrackAvailable
|
|
|
|
result.media_url_ = songs[0].url()
|
|
|
|
|
|
|
|
self.AsyncLoadComplete.emit(result)
|
|
|
|
|
|
|
|
|
2011-01-15 16:48:09 +01:00
|
|
|
class DigitallyImportedServiceBase(clementine.RadioService):
|
|
|
|
# Set these in subclasses
|
|
|
|
HOMEPAGE_URL = None
|
|
|
|
HOMEPAGE_NAME = None
|
|
|
|
STREAM_LIST_URL = None
|
|
|
|
ICON_FILENAME = None
|
|
|
|
SERVICE_NAME = None
|
|
|
|
SERVICE_DESCRIPTION = None
|
|
|
|
PLAYLISTS = []
|
2011-04-29 23:07:47 +02:00
|
|
|
URL_SCHEME = None
|
2011-01-15 16:48:09 +01:00
|
|
|
|
|
|
|
SETTINGS_GROUP = "digitally_imported"
|
2011-01-15 01:57:43 +01:00
|
|
|
|
|
|
|
SettingsDialogRequested = PyQt4.QtCore.pyqtSignal()
|
|
|
|
|
|
|
|
def __init__(self, model):
|
|
|
|
clementine.RadioService.__init__(self, self.SERVICE_NAME, model)
|
|
|
|
|
2011-04-29 23:07:47 +02:00
|
|
|
self.url_handler = DigitallyImportedUrlHandler(self.URL_SCHEME, self)
|
2011-04-28 19:50:45 +02:00
|
|
|
clementine.player.RegisterUrlHandler(self.url_handler)
|
2011-04-28 17:10:28 +02:00
|
|
|
|
2011-01-15 01:57:43 +01:00
|
|
|
self.network = clementine.NetworkAccessManager(self)
|
|
|
|
self.path = os.path.dirname(__file__)
|
|
|
|
|
|
|
|
self.audio_type = 0
|
2011-01-15 16:48:09 +01:00
|
|
|
self.username = ""
|
|
|
|
self.password = ""
|
2011-01-15 14:59:58 +01:00
|
|
|
|
2011-01-15 01:57:43 +01:00
|
|
|
self.context_index = None
|
|
|
|
self.menu = None
|
|
|
|
self.root = None
|
|
|
|
self.task_id = None
|
|
|
|
|
|
|
|
self.ReloadSettings()
|
|
|
|
|
|
|
|
def ReloadSettings(self):
|
|
|
|
settings = QSettings()
|
2011-01-15 16:48:09 +01:00
|
|
|
settings.beginGroup(self.SETTINGS_GROUP)
|
2011-01-15 01:57:43 +01:00
|
|
|
|
|
|
|
self.audio_type = int(settings.value("audio_type", 0).toPyObject())
|
2011-01-15 16:48:09 +01:00
|
|
|
self.username = unicode(settings.value("username", "").toPyObject().toUtf8())
|
|
|
|
self.password = unicode(settings.value("password", "").toPyObject().toUtf8())
|
2011-01-15 01:57:43 +01:00
|
|
|
|
|
|
|
def CreateRootItem(self):
|
2011-01-15 16:48:09 +01:00
|
|
|
self.root = QStandardItem(QIcon(os.path.join(self.path, self.ICON_FILENAME)),
|
|
|
|
self.SERVICE_DESCRIPTION)
|
2011-01-15 01:57:43 +01:00
|
|
|
self.root.setData(True, clementine.RadioModel.Role_CanLazyLoad)
|
|
|
|
return self.root
|
|
|
|
|
|
|
|
def LazyPopulate(self, parent):
|
|
|
|
if parent == self.root:
|
|
|
|
# Download the list of streams the first time the user expands the root
|
|
|
|
self.RefreshStreams()
|
|
|
|
|
|
|
|
def ShowContextMenu(self, index, global_pos):
|
|
|
|
if not self.menu:
|
|
|
|
self.menu = QMenu()
|
|
|
|
|
2011-02-10 23:24:17 +01:00
|
|
|
for action in self.GetPlaylistActions():
|
|
|
|
self.menu.addAction(action)
|
2011-01-15 01:57:43 +01:00
|
|
|
self.menu.addAction(clementine.IconLoader.Load("download"),
|
2011-01-15 16:48:09 +01:00
|
|
|
self.tr("Open " + self.HOMEPAGE_NAME + " in browser"), self.Homepage)
|
2011-01-15 01:57:43 +01:00
|
|
|
self.menu.addAction(clementine.IconLoader.Load("view-refresh"),
|
|
|
|
self.tr("Refresh streams"), self.RefreshStreams)
|
|
|
|
|
|
|
|
self.menu.addSeparator()
|
|
|
|
|
|
|
|
self.menu.addAction(clementine.IconLoader.Load("configure"),
|
2011-01-15 16:48:09 +01:00
|
|
|
self.tr("Configure..."), self.SettingsDialogRequested.emit)
|
2011-01-15 01:57:43 +01:00
|
|
|
|
|
|
|
self.context_index = index
|
|
|
|
self.menu.popup(global_pos)
|
|
|
|
|
2011-02-10 23:24:17 +01:00
|
|
|
def GetCurrentIndex(self):
|
|
|
|
return self.context_index
|
2011-02-09 18:51:59 +01:00
|
|
|
|
2011-01-15 01:57:43 +01:00
|
|
|
def Homepage(self):
|
|
|
|
QDesktopServices.openUrl(self.HOMEPAGE_URL)
|
|
|
|
|
|
|
|
def RefreshStreams(self):
|
|
|
|
if self.task_id is not None:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Request the list of stations
|
|
|
|
reply = self.network.get(QNetworkRequest(self.STREAM_LIST_URL))
|
|
|
|
reply.finished.connect(self.RefreshStreamsFinished)
|
|
|
|
|
|
|
|
# Give the user some indication that we're doing something
|
|
|
|
self.task_id = clementine.task_manager.StartTask(self.tr("Getting streams"))
|
|
|
|
|
|
|
|
def RefreshStreamsFinished(self):
|
|
|
|
# Get the QNetworkReply that called this slot
|
|
|
|
reply = self.sender()
|
|
|
|
reply.deleteLater()
|
|
|
|
|
|
|
|
if self.task_id is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Stop the spinner in the status bar
|
|
|
|
clementine.task_manager.SetTaskFinished(self.task_id)
|
|
|
|
self.task_id = None
|
|
|
|
|
|
|
|
# Read the data and parse the json object inside
|
|
|
|
json_data = reply.readAll().data()
|
|
|
|
streams = json.loads(json_data)
|
|
|
|
|
|
|
|
# Sort by name
|
|
|
|
streams = sorted(streams, key=operator.itemgetter("name"))
|
|
|
|
|
|
|
|
# Now we have the list of streams, so clear any existing items in the list
|
|
|
|
# and insert the new ones
|
|
|
|
if self.root.hasChildren():
|
|
|
|
self.root.removeRows(0, self.root.rowCount())
|
|
|
|
|
|
|
|
for stream in streams:
|
2011-04-27 00:06:58 +02:00
|
|
|
song = clementine.Song()
|
|
|
|
song.set_title(stream["name"])
|
|
|
|
song.set_artist(self.SERVICE_DESCRIPTION)
|
2011-04-29 23:07:47 +02:00
|
|
|
song.set_url(QUrl("%s://%s" % (self.URL_SCHEME, stream["key"])))
|
2011-04-27 00:06:58 +02:00
|
|
|
|
2011-01-15 01:57:43 +01:00
|
|
|
item = QStandardItem(QIcon(":last.fm/icon_radio.png"), stream["name"])
|
|
|
|
item.setData(stream["description"], PyQt4.QtCore.Qt.ToolTipRole)
|
|
|
|
item.setData(clementine.RadioModel.PlayBehaviour_SingleItem, clementine.RadioModel.Role_PlayBehaviour)
|
2011-04-27 00:06:58 +02:00
|
|
|
item.setData(song, clementine.RadioModel.Role_SongMetadata)
|
2011-01-15 01:57:43 +01:00
|
|
|
self.root.appendRow(item)
|
|
|
|
|
|
|
|
def playlistitem_options(self):
|
2011-04-28 17:10:28 +02:00
|
|
|
return clementine.PlaylistItem.Options(clementine.PlaylistItem.PauseDisabled)
|
2011-01-15 01:57:43 +01:00
|
|
|
|
2011-01-15 16:48:09 +01:00
|
|
|
def LoadStation(self, key):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2011-01-15 14:59:58 +01:00
|
|
|
def LoadPlaylistFinished(self):
|
2011-04-28 17:10:28 +02:00
|
|
|
self.url_handler.LoadPlaylistFinished(self.sender())
|