import re
import json

def tweetsToKML(filename, phrase1, phrase2):

    ICONS = {0:'http://cdn4.iconfinder.com/data/icons/soda_pop_caps/PNG/Coca-Cola_128.png',
             1:'http://aux.iconpedia.net/uploads/157038274.png',
             2:'http://files.softicons.com/download/internet-icons/blogblogs-kit-icons-by-icontexto/png/128/icontexto-drink-web20-blogblogs.png'}

    f = open(filename)
    data = f.read()
    f.close()
    print('Reading ' + filename + ' has completed.')

    tweets = digestData(data)
    coordList = getCoordList(tweets)
    iconList = getIconList(tweets, phrase1, phrase2)

    writeMultiplePins(coordList, iconList, ICONS)

    return

   
def writeMultiplePins(coordList, iconList, iconDictionary):
    ''' Create pin.kml (at a convenient location) that puts
    multiple push pins on Earth, with coordinates and
    icons specified by arguments. '''

    # input:

    # coordList is a list of lists, each sublist consisting of
    # two numbers (longitude, latitude). For example, if I
    # want to put 3 push pins at (28,36,0), (32,78,0),
    # (12,33,0), coordList should be [[28,36],
    # [32,78],[12,33]].

    # iconList is a list of numbers, each number indicating an
    # according to the third argument.
    # coordList has to have the same number of subLists as
    # iconList has numbers.

    # iconDictionary is a dictionary.

    # output: none

    HEADER = '''<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>'''

    PART1 = '''  <Placemark>
    <name></name>
    <description></description>
   <Style>
      <IconStyle>
	 <scale>1.0</scale>
	 <Icon>
		<href>'''

    PART2 = '''</href>
	 </Icon>
	 <color>ffffffff</color>
       </IconStyle>
    </Style>
    <Point>
      <coordinates>'''

    PART3 = ''',0</coordinates>
    </Point>
  </Placemark>'''

    ENDING = '''</Document>
</kml>'''

    string = HEADER
    
    LONGITUDE = 0
    LATITUDE = 1
    
    for index in range(0,len(coordList)):
        string += PART1 + iconDictionary[iconList[index]] +\
                  PART2 + str(coordList[index][LONGITUDE]) + ',' + str(coordList[index][LATITUDE]) +\
                  PART3
    
    
    string += ENDING

    pinFile = open('Z:/Windata/Desktop/pin.kml', 'w')   
    pinFile.write(string)
    pinFile.close()

    return


def digestData(data):
    unparsedTweets = re.findall(r'{.*}\n', data)
    print(str(len(unparsedTweets)) + ' tweets found in the file.')
    tweets = []

    for t in unparsedTweets:
        tweets += [json.loads(t)]

    print(str(len(tweets)) +' tweets loaded.')
    return tweets

def getCoordList(tweets):

    # input: list of dictionaries, each representing a tweet
    # output: list of lists, each sublist containing a pair of numbers (longitude, latitude)

    result = []
    for t in tweets:
        if 'place' in t:
            if t['place'] != None:
                if 'bounding_box' in t['place']:
                    if t['place']['bounding_box'] != None:
                        if 'coordinates' in t['place']['bounding_box']:
                            coords = t['place']['bounding_box']['coordinates']
                            longitude = float(coords[0][0][0])
                            latitude = float(coords[0][0][1])
                            pair = [longitude, latitude]
                            result += [pair]
    return result

def getIconList(tweets, p1, p2):

    # input: list of dictionaries (tweets)
    # output: list of numbers

    result = []

    for t in tweets:
        isGeotagged = False
        if 'place' in t:
            if t['place'] != None:
                if 'bounding_box' in t['place']:
                    if t['place']['bounding_box'] != None:
                        if 'coordinates' in t['place']['bounding_box']:
                            isGeotagged = True

        if isGeotagged:
            text = t['text'].lower()

            hasP1 = False
            hasP2 = False
            
            if p1 in text:
                hasP1 = True
            if p2 in text:
                hasP2 = True

            if hasP1 and hasP2:
                result += [2]    # contains both phrases
            else:
                if hasP1:
                    result += [0] # contains phrase 1
                else:
                    result += [1]# contains phrase 2
    print(result)
    return result
                    



    








