musixmatch-master/7digital-python/lockerEndpoint.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """A python interface to 7digital's locker endpoint"""
3 import os
4 from xml.dom.minidom import parseString
5 
6 
7 class Locker(object):
8  def __init__(self, xml_response):
9  self.xml_response = parseString(xml_response)
10 
11  def get_contents(self):
12  results = []
13  locker_items = self.xml_response.getElementsByTagName('lockerRelease')
14 
15  for item in locker_items:
16  results.append(LockerItem(item))
17 
18  return results
19 
20  def get_artists(self):
21  results = []
22  artist_nodes = self.xml_response.getElementsByTagName('artist')
23 
24  for artist in artist_nodes:
25  results.append(LockerArtist(artist))
26 
27  return results
28 
29  def get_releases(self):
30  results = []
31  release_nodes = self.xml_response.getElementsByTagName('release')
32  for release in release_nodes:
33  results.append(LockerRelease(release))
34 
35  return results
36 
37  def get_tracks(self):
38  results = []
39  track_nodes = self.xml_response.getElementsByTagName('lockerTrack')
40  for track in track_nodes:
41  results.append(LockerTrack(track))
42 
43  return results
44 
45 class _LockerBase(object):
46  def extract(self, node, name, index = 0):
47  """Extracts a value from the xml string"""
48  try:
49  nodes = node.getElementsByTagName(name)
50 
51  if len(nodes):
52  if nodes[index].firstChild:
53  return nodes[index].firstChild.data.strip()
54  else:
55  return None
56  except:
57  return None
58 
60  def __init__(self, xml):
61  self.release = LockerRelease(xml.getElementsByTagName('release')[0])
62  self.tracks = self.__get_release_tracks(xml.getElementsByTagName('lockerTrack'))
63 
64  def __get_release_tracks(self, tracks):
65  result = []
66  for track in tracks:
67  result.append(LockerTrack(track))
68  return result
69 
71  def __init__(self, xml):
72  self.track = Track(xml.getElementsByTagName('track')[0])
73  self.remaining_downloads = self.extract(xml, 'remainingDownloads')
74  self.purchaseDate = self.extract(xml, 'purchaseDate')
75  self.download_urls = self.__get_download_urls(xml.getElementsByTagName('downloadUrls'))
76 
77  def __get_download_urls(self, urls):
78  result = []
79  for url in urls:
80  result.append(DownloadUrls(url))
81  return result
82 
84  def __init__(self, xml):
85  self.url = self.extract(xml, 'url')
86  self.format = Format(xml.getElementsByTagName('format')[0])
87 
89  def __init__(self, xml):
90  self.id = xml.getAttribute('id')
91  self.file_format = self.extract(xml, 'fileFormat')
92  self.bit_rate = self.extract(xml, 'bitRate')
93 
95  def __init__(self, xml):
96  self.id = xml.getAttribute('id')
97  self.title = self.extract(xml, 'title')
98  self.version = self.extract(xml, 'version')
99  self.artist = LockerArtist(xml.getElementsByTagName('artist')[0])
100  self.url = self.extract(xml, 'url')
101 
103  def __init__(self, xml):
104  self.id = xml.getAttribute('id')
105  self.title = self.extract(xml, 'title')
106  self.type = self.extract(xml, 'type')
107  self.artist = LockerArtist(xml.getElementsByTagName('artist')[0])
108  self.url = self.extract(xml, 'url')
109  self.image = self.extract(xml, 'image')
110  self.release_date = self.extract(xml, 'releaseDate')
111 
113  def __init__(self, xml):
114  self.id = xml.getAttribute('id')
115  self.name = self.extract(xml, 'name')
116  self.url = self.extract(xml, 'url')
117 
118 
int len