# Google Earth Program

# This program takes a list of tweets and makes a KML file of pins.

# Import Statements
import csv
import re

# CONSTANTS: these variables never change
TWEET_FILE = 'brown.out'

def getBrownTweets():
    ''' main function: reads tweets & writes KML file called 'BrownTweets.kml'
    INPUTS: none
    OUTPUTS: none'''

    tweetList = readTweets()
    print 'There are',len(tweetList),'tweets in this file.'
    writeKMLFile(tweetList,'BrownTweets.kml');

    return

def readTweets():
    '''Reads Tweets and returns a list of tweets.
    INPUT: none
    OUTPUT: list of lists'''
  
    # open file (use csv reader)
    myFile = open(TWEET_FILE,'r')
    myCSVReader = csv.reader(myFile,delimiter=',',quotechar='"')

    # read lines from file and put in a list
    myCSVList = []
    for row in myCSVReader: # iterator object that returns lists

        if float(row[5]) != 0.0 and float(row[6]) != 0.0 and len(row) == 13:

            # replace invalid XML characters in name and tweet
            row[3] = re.sub('&','and',row[3])
            row[3] = re.sub('>',' ',row[3])
            row[3] = re.sub('<',' ',row[3])
            row[12] = re.sub('&','and',row[12])
            row[12] = re.sub('>',' ',row[12])
            row[12] = re.sub('<',' ',row[12])

            # finally, add tweet to list.
            myCSVList = myCSVList + [row]

    # done with file: close it
    myFile.close()

    return myCSVList

def writeKMLFile(tweetList,filename):
    '''Writes a KML file of tweets.
    INPUTS: list of lists, output filename (string)
    OUTPUTS:  none'''

    # FILL THIS IN
  
    return
