#!/Users/jake/.virtualenvs/tstream/bin/python
import os
import getpass
import argparse

from pymongo import Connection
import yaml
from tweetstream import FilterStream


##___ Configuration ____________________________________________________________
def _configure(config={}, username=None, password=None, words=None):
    if os.path.exists('config.yml') and config == {}:
        config = yaml.load(open('config.yml'))

    username = config.get('username')
    password = config.get('password')
    words = config.get('words')
    collection_name = config.get('collection')

    if username is None:
        username = raw_input("Username: ")
    if password is None:
        password = getpass.getpass()
    if words is None:
        words = raw_input("Words to track (comma seperated): ").split(',')
    if collection_name is None:
        collection_name = '_'.join(words)

    stream = FilterStream(username, password, track=words)

    return stream, collection_name

#______________________________________________________________________________
def scrape_twitter():
    parser = argparse.ArgumentParser(description=':: Archive tweets ::')
    parser.add_argument('--dry-run', dest='dry_run', action='store_const',
                       const=True, default=False,
                       help='Don\'t archive tweets to mongo db, just print \
                             tweets to stdout.')
    args = parser.parse_args()

    stream, collection_name = _configure()

    if args.dry_run is False:
        c = Connection()
        db = c.twitter

    with stream:
        for tweet in stream:
            if args.dry_run is False:
                db[collection_name].insert(tweet)
            print "%s\t%s" % (tweet.get('created_at'), tweet.get('text'))

if __name__ == '__main__':
    scrape_twitter()
