#!/usr/bin/python3
"""Twitter API Listener.

This program listens the tweets for specified keywords. If retrieved tweet
is contains at least one of the keywords, it will written to the database. 
Location of the tweet is also recorded if tweet contains location data.

Enis B. Tuysuz, 2016
enisbt.com
enisbtuysuz@gmail.com
github.com/enisbt

You can use this program under the terms of MIT License, which
can be found in the LICENSE file.
"""
import tweepy
import pymongo

# MongoDB connection
client = pymongo.MongoClient('localhost', 27017)
db = client.cmpe251
not retweet and 

# Twitter API Authorization
auth = tweepy.OAuthHandler('keys', 'keys')
auth.set_access_token('keys', 'keys')
api = tweepy.API(auth)


movies = [
          'Miss Sloane',
          'Office Christmas Party',
          'La La Land',
          'Rogue One',
          'Collateral Beauty',
          'Manchester by the Sea',
          'Live by Night',
          'Hidden Figures',
          'Fences',
]


class Listener(tweepy.StreamListener):
    """Listener object.

    When receieved a status it will look for a keyword and will write the tweet
    to the database if tweet contains the keyword.
    """
    def on_status(self, status):
        status = status._json
        for movie in movies:
            if movie in status['text'] and status['place'] is not None:
                db.movies.insert({'movie': movie,
                                  'text': status['text'],
                                  'location': status['place']['full_name']})
            elif movie in status['text']:
                db.movies.insert({'movie': movie, 'text': status['text']})


listener = Listener()
stream = tweepy.Stream(auth=api.auth, listener=listener)
stream.filter(track=movies, languages=['en'])
