import sys
import pickle
import tweet
import time
import classify


lat = float(sys.argv[1])
long = float(sys.argv[2])
radius = float(sys.argv[3])

print('\n\nRunning classifier on new tweets')

# Store the tweets here
combined = []

try:
    since_id = sys.argv[4]
except:
    since_id = None

# Open classifier
classifier = pickle.load(open("classifier.pickle", "rb"))

start = time.time()
# Get the tweets
if since_id is None:
    tweets = tweet.get_tweets(lat=lat, long=long, radius=radius, term=" flu ", count=1000)
else:
    tweets = tweet.get_tweets(lat=lat, long=long, radius=radius, term=" flu ", count=1000, since_id=since_id)

print(str(len(tweets)) + ' new tweets have been pulled')
print('Running classifier')

# Classify the tweets
classifications = classifier.classify_many([classify.get_features(x['text']) for x in tweets])

print('All tweets have been classified')

# Converting the tweets to the form for the mapper
for i in range(len(tweets)):
    temp = {}
    temp['time'] = tweets[i]['time']
    temp['latitude'] = tweets[i]['latitude']
    temp['longitude'] = tweets[i]['longitude']
    temp['location'] = tweets[i]['location']
    temp['tag'] = bool(int(classifications[i]))
    combined.append(temp)

# Save the tweets to a file for the python 2 interpreter to open and map
with open('tagged_data.bin', 'wb') as file:
    pickle.dump(combined, file, 0)

print('\n\nTime elapsed: ' + str(time.time() - start) + ' seconds')
