# imports for twitter access
import twitter
import credentials

api = twitter.Api(consumer_key = credentials.consumer_key,
                  consumer_secret = credentials.consumer_secret,
                  access_token_key = credentials.access_token_key,
                  access_token_secret = credentials.access_token_secret)

def get_tweets(lat, long, radius, term=" flu ", count=100, since_id = None):
    data = []
    
    tweets = api.GetSearch(
        term = term,
        geocode = [lat, long, str(radius) + "mi"],
        count = count,
        max_id = since_id,
        result_type = "recent"
    )

    for tweet in tweets:
        if not tweet.place and not tweet.user.location:
            tweets.remove(tweet)
            continue

        try:
            data.append({
                'id'        : tweet.id,
                'text'      : tweet.text,
                'time'      : tweet.created_at,
                'latitude'  : tweet.place['bounding_box']['coordinates'][0][0][1],
                'longitude' : tweet.place['bounding_box']['coordinates'][0][0][0],
                'location'   : None,
                'has_flu'   : None
            })
        except:
            data.append({
                'id'        : tweet.id,
                'text'      : tweet.text,
                'time'      : tweet.created_at,
                'latitude'  : None,
                'longitude' : None,
                'location'  : tweet.user.location,
                'has_flu'   : None
            })

    return data
