Esempio n. 1
0
    def getPlayingStations(self, artist):
        """
		gets the playing stations from last.fm and gets the 
		stations who have historically played the artist from
		the database.
		Then, merges the sets and gets the artists for each
		station.

		Returns a dictionary with 2 lists. 1 is a list of stations
		as 3-tuples, the other is the list of artist for each
		station

		parameters
		----------
		artist: name of the artist being played
		dataset: dataset of stations who have played the artist.
			dictionary with two keys: 
			'labels' contains the set of station 3-tuples
			'data' contains the list of artists for each station
		"""
        sr = ShoutcastWrapper()
        db = DatabaseWrapper()
        mergedict = {}
        mergelist = []
        artistsetlist = []

        # gets the set of currently playing stations
        playingStationSet = sr.getStationPlayingArtist(artist)

        # gets the set of historically played stations
        historicalStationSet = db.getStationTuplesForArtist(artist)

        # merges the two sets of stations, while preserving order of
        # listen count

        # add all of the historically played stations
        itemcount = 0
        for item in historicalStationSet:
            itemId = item[1]
            itemName = item[0]

            mergedict[itemId] = itemcount
            mergelist.append((itemId, itemName, False))
            # mergelist.append(item)
            itemcount = itemcount + 1

            # add only the unique stations from now playing
        for item in playingStationSet:
            itemId = item[2]
            itemName = item[0]
            itemLC = item[1]
            itemCT = item[3]

            # if the station is already in the list, change
            # status to playing
            if mergedict.has_key(itemId):
                itemnumber = mergedict[itemId]
                mergelist[itemnumber] = item

                # else append the station to the top of the list
                # and add the station to the db
            else:
                # mergelist.insert(0, (itemId, itemName, True, itemCT))
                mergelist.insert(0, item)
                db.addStationForArtist(artist, (itemName, itemId, itemLC))

                # get set of artists for each station
        for item in mergelist:
            stationID = item[0]
            artistset = db.getArtistsForStationID(stationID)
            artistsetlist.append(artistset)

        return {"data": artistsetlist, "labels": mergelist}