class TestLibFM(unittest.TestCase): def setUp(self): self.libFM = LibFM('2b3268cef9e10b81c4359cd03b4e6373', 'f935eb35203409a4fc3f7fc37318ab3e') #for generating not-found errors self.fake_user = '******' self.fake_artist = 'wdehgehrhrhrh' #see http://www.last.fm/api/desktopauth for how to generate your own sk self.libFM.session_key = None def session(method): """Skip methods that require authentication when sk is not hardcoded""" def wrapper(self, *args, **kwargs): if self.libFM.session_key != None: return method(self, *args, **kwargs) else: return return wrapper def test_named_parameters(self): response = self.libFM.read('user.getRecentTracks', limit=5, user='******') self.assertTrue('recenttracks' in response, 'Named parameters should work on generated methods') def test_no_parameters(self): """Making sure methods w/o parameters work fine""" response = self.libFM.read('geo.getMetroWeeklyChartlist') self.assertTrue('weeklychartlist' in response, 'Error in method w/o \ parameters') def test_response_error_handling(self): """Checking exceptions raised from error codes""" self.assertRaises(LibFMError, self.libFM.read, 'user.getShouts', user=self.fake_user) @session def test_write_method(self): response = self.libFM.write('artist.addTags', artist='Pearl Jam', tags='Grunge') if 'status' in response: if response['status'] == 'ok': return self.assertTrue(False, 'Method failed to write') def test_xml_eq_json_normal_response(self): """Check if XML and JSON responses are seamless""" xml_response = self.libFM.read('artist.getTopFans', artist='Pearl Jam') self.libFM.force_xml_responses = True json_response = self.libFM.read('artist.getTopFans', artist='Pearl Jam') self.assertEqual(xml_response, json_response, 'XML and JSON requests produce different results') def test_xml_eq_json_error_response(self): """Check if error containing XML and JSON responses are seamless""" try: self.libFM.read('artist.getTopTracks', artist=self.fake_artist) self.fail('Call of artist.getTopTracks with fake artist name %s \ should have raised an error.' % self.fake_artist) except LibFMError, xml_error: self.libFM.force_xml_responses = True try: self.libFM.read('artist.getTopTracks', artist=self.fake_artist) self.fail('Call of artist.getTopTracks with fake artist name \ %s should have raised an error.' % self.fake_artist) except LibFMError, json_error: self.assertEqual(xml_error, json_error, 'XML and JSON responses raise different errors')
from libfm import LibFM from libfm import LibFMError # create a handler instance with API key and application secret lib_fm = LibFM('api key...', 'application secret...') try: lib_fm.create_mobile_session('username...', 'password...') # simple read info = lib_fm.read('artist.getInfo', artist='Pink Floyd') print info['artist']['url'] for tag in info['artist']['tags']['tag']: print tag['name'] # simple write lib_fm.write('artist.addTags', artist='Black Sabbath', tags='metal, classic rock') except LibFMError, err: print err