~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to scripts/digitallyimported-radio/skyservice.py

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import clementine
2
 
 
3
 
from servicebase import DigitallyImportedServiceBase
4
 
 
5
 
from PyQt4.QtCore    import QSettings, QString, QUrl
6
 
from PyQt4.QtNetwork import QNetworkCookie, QNetworkCookieJar, QNetworkRequest
7
 
 
8
 
import re
9
 
 
10
 
class SkyFmService(DigitallyImportedServiceBase):
11
 
  HOMEPAGE_URL = QUrl("http://www.sky.fm/")
12
 
  HOMEPAGE_NAME = "sky.fm"
13
 
  STREAM_LIST_URL = QUrl("http://listen.sky.fm/")
14
 
  ICON_FILENAME = "icon-sky.png"
15
 
  SERVICE_NAME = "SKY.fm"
16
 
  SERVICE_DESCRIPTION = "SKY.fm"
17
 
 
18
 
  HASHKEY_RE = re.compile(r'hashKey\s*=\s*\'([0-9a-f]+)\'')
19
 
 
20
 
  # These have to be in the same order as in the settings dialog
21
 
  PLAYLISTS = [
22
 
    {"premium": False, "url": "http://listen.sky.fm/public3/%s.pls"},
23
 
    {"premium": True,  "url": "http://listen.sky.fm/premium_high/%s.pls?hash=%s"},
24
 
    {"premium": False, "url": "http://listen.sky.fm/public1/%s.pls"},
25
 
    {"premium": True,  "url": "http://listen.sky.fm/premium_medium/%s.pls?hash=%s"},
26
 
    {"premium": True,  "url": "http://listen.sky.fm/premium/%s.pls?hash=%s"},
27
 
    {"premium": False, "url": "http://listen.sky.fm/public5/%s.asx"},
28
 
    {"premium": True,  "url": "http://listen.sky.fm/premium_wma_low/%s.asx?hash=%s"},
29
 
    {"premium": True,  "url": "http://listen.sky.fm/premium_wma/%s.asx?hash=%s"},
30
 
  ]
31
 
 
32
 
  def __init__(self, model):
33
 
    DigitallyImportedServiceBase.__init__(self, model)
34
 
 
35
 
    self.last_key = None
36
 
 
37
 
  def LoadStation(self, key):
38
 
    # Non-premium streams can just start loading straight away
39
 
    if not self.PLAYLISTS[self.audio_type]["premium"]:
40
 
      self.LoadPlaylist(key)
41
 
      return
42
 
 
43
 
    # Otherwise we have to get the user's hashKey
44
 
    request = QNetworkRequest(QUrl("http://www.sky.fm/configure_player.php"))
45
 
    postdata = "amember_login=%s&amember_pass=%s" % (
46
 
      QUrl.toPercentEncoding(self.username),
47
 
      QUrl.toPercentEncoding(self.password))
48
 
 
49
 
    reply = self.network.post(request, postdata)
50
 
    reply.finished.connect(self.LoadHashKeyFinished)
51
 
 
52
 
    self.last_key = key
53
 
 
54
 
  def LoadHashKeyFinished(self):
55
 
    # Get the QNetworkReply that called this slot
56
 
    reply = self.sender()
57
 
    reply.deleteLater()
58
 
 
59
 
    # Parse the hashKey out of the reply
60
 
    data = QString.fromUtf8(reply.readAll())
61
 
    match = self.HASHKEY_RE.search(data)
62
 
 
63
 
    if match:
64
 
      hash_key = match.group(1)
65
 
    else:
66
 
      clementine.task_manager.SetTaskFinished(self.task_id)
67
 
      self.task_id = None
68
 
      self.StreamError.emit(self.tr("Invalid SKY.fm username or password"))
69
 
      return
70
 
 
71
 
    # Now we can load the playlist
72
 
    self.LoadPlaylist(self.last_key, hash_key)
73
 
 
74
 
  def LoadPlaylist(self, key, hash_key=None):
75
 
    playlist_url = self.PLAYLISTS[self.audio_type]["url"]
76
 
 
77
 
    if hash_key:
78
 
      playlist_url = playlist_url % (key, hash_key)
79
 
    else:
80
 
      playlist_url = playlist_url % key
81
 
 
82
 
    # Start fetching the playlist.  Can't use a SongLoader to do this because
83
 
    # we have to use the cookies we set in ReloadSettings()
84
 
    reply = self.network.get(QNetworkRequest(QUrl(playlist_url)))
85
 
    reply.finished.connect(self.LoadPlaylistFinished)