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}
    def getJSONResponseForArtist(self, artist):
        """
		creates a JSON response for a given artist
		
		parameters
		----------
		artist: name of artist to search for
		
		returns
		-------
		a JSON string consisting of:
			a list of 3 stations.
			each station contains:
				a representative artist
				3 tags representing the station
		"""
        # vars
        sr = ShoutcastWrapper()
        topartists = []
        topstations = []
        toptags = []

        outputlist = []

        # get the data for the artist
        dataset = self.getPlayingStations(artist)

        # error check - if no artists
        if len(dataset["data"]) < 4:
            return json.dumps({"success": "False", "data": []})

            # cluster the data
        clusteredset = self.cluster(dataset)

        print "clustering done"

        # pick the station for each set
        stations = clusteredset["labels"]
        for item in stations:
            stationtoappend = item[0]

            # check if there is a now playing artist
            if stationtoappend[2] == False:
                shoutcastid = stationtoappend[0]
                name = stationtoappend[1]
                currenttrack = sr.getCurrentTrackForStationWithData(name)
                currenttrackname = currenttrack["stationname"]
                bitrate = currenttrack["br"]
                encoding = currenttrack["en"]
                listencount = currenttrack["lc"]
                newstationtuple = (shoutcastid, name, False, currenttrackname, bitrate, encoding, listencount)

                topstations.append(newstationtuple)
            else:
                topstations.append(stationtoappend)

                # append 3 dummy lists to topstations to prevent errors if no stations found
        for i in range(0, 3):
            topstations.append(("", ""))

            # pick the representative artist for each set
        topartists = self.selectRepresentativeArtists(clusteredset["data"], artist)

        # pick the top tags for each station
        toptags = self.selectTopStationTags(topstations)

        for i in range(0, 3):
            outputlist.append([topstations[i], topartists[i], toptags[i]])

        return json.dumps({"success": "True", "data": outputlist})
"""
automatically runs every 10 mins and checks which 
songs each artist is playing. then it writes the output
to the file
"""
import sys
sys.path.insert(0, '/Users/meuse/Meuse/env/meuse/meuse/meuse')
import json
import pickle
from shoutcast_wrapper import ShoutcastWrapper

sr = ShoutcastWrapper()

pickle_filename = "/Users/meuse/Meuse/env/meuse/meuse/meuse/diversity/stations.pickle"
output_filename = "/Users/meuse/Meuse/env/meuse/meuse/meuse/diversity/output.csv"
output_string = None
check_on = False

i (check_on):
	stationslist = pickle.load(open("/Users/meuse/Meuse/env/meuse/meuse/meuse/diversity/stations.pickle"))
	counter = 0

	for station in stationslist:
		counter = counter + 1
		print "Downloading station number " + str(counter)

		#get the playing artist and add to line
		currentartist = sr.getCurrentTrackForStation(station)

		#append the artist to the string
		try: