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 sample_exp(): # making data from origin format to libfm format #db = Database('movielens', 718, 8928, 'ratings.csv', 'movies.attr') db = Database('amazon', 1000, 5000, 'cands_book.rating', 'cands_book.item') db.load_data() db.make_train_test_matrix() libfm = LibFM() # step 1: classification to predict interest db.dump_libfm_data('train_step1.libfm', 'test_step1.libfm', add_negative=True, binary=True) libfm.run('c', 'train_step1.libfm', 'test_step1.libfm', 'pred.libfm', iter_num=1) step1_pred_list = db.load_pred_list('pred.libfm', 'c') # TODO: merge accuracy accessing into libfm.py and call it here # step 2: regression to predict possible rating db.dump_libfm_data('train_step2.libfm', 'test_step2.libfm', add_negative=False, binary=False) libfm.run('r', 'train_step1.libfm', 'test_step1.libfm', 'pred.libfm', iter_num=1) step2_pred_list = db.load_pred_list('pred.libfm', 'r')
import json from libfm import LibFM from libfm import LibFMError # create a handler instance with API key and application secret lib_fm = LibFM('9b763a9a320e5719c1c9851fc7d71850', 'a10aa8e3f09af99b71d728953c6bd755') def get_artist_info(artist): info = lib_fm.read('artist.getInfo', artist=artist) bio = info["artist"]["bio"] name = info["artist"]["name"] tags = info["artist"]["tags"] image = info["artist"]["image"] similar = info["artist"]["similar"] taglist = list() for tag in tags["tag"]: taglist.append(tag["name"]) #print taglist print json.dumps(taglist) artist_list = list() for artist in similar["artist"]: artist_list.append(artist["name"]) print json.dumps(artist_list)
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 Count import count_words_at_url from random import randint import spotipy from libfm import LibFM import pymysql as sql spotify = spotipy.Spotify() lib_fm = LibFM('9b763a9a320e5719c1c9851fc7d71850', 'a10aa8e3f09af99b71d728953c6bd755') lib_fm.create_mobile_session('weixc1234', 'wxc16888') db = sql.connect(host='cs4111.cnjryr7wwjlv.us-west-2.rds.amazonaws.com', user='******', passwd='musicrecommendation', db='cs4111', cursorclass=sql.cursors.DictCursor) cursor = db.cursor() tags_list = list() ''' test Spotify API and down the data using APIs ''' def get_artist_id_by_name(name): results = spotify.search(q='artist:' + name, type='artist') return results["artists"]["items"][0]["uri"]
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