2015-08-22 20:12:26 +00:00
|
|
|
# encoding: utf8
|
2015-08-14 16:59:08 +00:00
|
|
|
from __future__ import unicode_literals, print_function
|
|
|
|
import plac
|
|
|
|
import codecs
|
2015-08-22 20:12:26 +00:00
|
|
|
import pathlib
|
|
|
|
import random
|
2015-08-14 16:59:08 +00:00
|
|
|
|
2015-08-22 20:12:26 +00:00
|
|
|
import twython
|
2015-08-14 16:59:08 +00:00
|
|
|
import spacy.en
|
|
|
|
|
2015-08-22 20:12:26 +00:00
|
|
|
import _handler
|
2015-08-14 16:59:08 +00:00
|
|
|
|
|
|
|
|
2015-08-22 20:12:26 +00:00
|
|
|
class Connection(twython.TwythonStreamer):
|
|
|
|
def __init__(self, keys_dir, nlp, query):
|
|
|
|
keys_dir = pathlib.Path(keys_dir)
|
|
|
|
read = lambda fn: (keys_dir / (fn + '.txt')).open().read().strip()
|
|
|
|
api_key = map(read, ['key', 'secret', 'token', 'token_secret'])
|
|
|
|
twython.TwythonStreamer.__init__(self, *api_key)
|
2015-08-14 16:59:08 +00:00
|
|
|
self.nlp = nlp
|
2015-08-22 20:12:26 +00:00
|
|
|
self.query = query
|
2015-08-14 16:59:08 +00:00
|
|
|
|
|
|
|
def on_success(self, data):
|
2015-08-22 20:12:26 +00:00
|
|
|
_handler.handle_tweet(self.nlp, data, self.query)
|
|
|
|
if random.random() >= 0.1:
|
|
|
|
reload(_handler)
|
2015-08-14 16:59:08 +00:00
|
|
|
|
|
|
|
|
2015-08-22 20:12:26 +00:00
|
|
|
def main(keys_dir, term):
|
|
|
|
nlp = spacy.en.English()
|
|
|
|
twitter = Connection(keys_dir, nlp, term)
|
|
|
|
twitter.statuses.filter(track=term, language='en')
|
2015-08-14 16:59:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
plac.call(main)
|