-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added storage adapter that reads tweets from twitter api.
- Loading branch information
1 parent
1b91031
commit 5b55dbc
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
build | ||
dist | ||
database.db | ||
settings.py | ||
*.pyc | ||
*.swp | ||
*.egg-info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .storage import StorageAdapter | ||
from .jsondatabase import JsonDatabaseAdapter | ||
from .mongodb import MongoDatabaseAdapter | ||
from .twitter import TwitterAdapter | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from chatterbot.adapters.storage import DatabaseAdapter | ||
from chatterbot.conversation import Statement | ||
from application_only_auth import Client | ||
|
||
|
||
class TwitterAdapter(DatabaseAdapter): | ||
|
||
def __init__(self, **kwargs): | ||
super(TwitterAdapter, self).__init__(**kwargs) | ||
|
||
CONSUMER_KEY = kwargs["consumer_key"] | ||
CONSUMER_SECRET = kwargs["consumer_secret"] | ||
|
||
self.client = Client(CONSUMER_KEY, CONSUMER_SECRET) | ||
|
||
def search(self, q, count=1, result_type="recent"): | ||
import urllib | ||
|
||
# Remove non-ascii characters from the search string | ||
cleaned = ''.join( | ||
[i if ord(i) < 128 else ' ' for i in q] | ||
) | ||
|
||
query = urllib.quote(cleaned) | ||
|
||
url = "https://api.twitter.com/1.1/search/tweets.json" | ||
url += "?q=" + query | ||
url += "&result_type=" + result_type | ||
url += "&count=" + str(count) | ||
|
||
return self.client.request(url) | ||
|
||
def count(self): | ||
return 1 | ||
|
||
def find(self, statement_text): | ||
data = self.search(statement_text) | ||
|
||
for d in data['statuses']: | ||
print ">>", d['in_reply_to_status_id_str'] | ||
|
||
result_text = data['statuses'][0]['text'] | ||
|
||
return Statement(result_text) | ||
|
||
def filter(self, **kwargs): | ||
""" | ||
Returns a list of statements in the database | ||
that match the parameters specified. | ||
""" | ||
statement_text = kwargs.get('text') | ||
|
||
if not statement_text: | ||
statement_text = kwargs.get('in_response_to__contains') | ||
|
||
if not statement_text: | ||
# If no text parameter was given get a selection of recent tweets | ||
statements = [] | ||
data = self.search('none', count=20) | ||
for item in data['statuses']: | ||
statements.append( | ||
Statement(item['text']) | ||
) | ||
return statements | ||
|
||
data = self.search(statement_text) | ||
result_text = data['statuses'][0]['text'] | ||
|
||
statement = Statement(result_text) | ||
|
||
return [statement] | ||
|
||
def update(self, statement): | ||
return statement | ||
|
||
def get_random(self): | ||
""" | ||
Returns a random statement from the api. | ||
""" | ||
#data = self.search("random") | ||
data = self.client.request('https://stream.twitter.com/1.1/statuses/sample.json') | ||
|
||
# TODO: Choose one of the statuses at random: | ||
|
||
options = ["electronic", "brains", "robot", "zombie", "party", "cool"] | ||
|
||
print "MULTULE:", data['statuses'] | ||
|
||
statement_text = data['statuses'][0]['text'] | ||
|
||
return Statement(statement_text) | ||
|
||
def drop(self): | ||
""" | ||
Twitter is only a simulated data source in | ||
this case so it cannot be removed. | ||
""" | ||
pass | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from chatterbot import ChatBot | ||
from settings import TWITTER | ||
|
||
|
||
chatbot = ChatBot("ChatterBot", | ||
storage_adapter="chatterbot.adapters.storage.TwitterAdapter", | ||
logic_adapter="chatterbot.adapters.logic.ClosestMatchAdapter", | ||
io_adapter="chatterbot.adapters.io.TerminalAdapter", | ||
database="../database.db", | ||
consumer_key=TWITTER["CONSUMER_KEY"], | ||
consumer_secret=TWITTER["CONSUMER_SECRET"] | ||
) | ||
|
||
''' | ||
Respond to mentions on twitter. | ||
The bot will follow the user who mentioned it and | ||
favorite the post in which the mention was made. | ||
''' | ||
|
||
print chatbot.storage.get_random() | ||
|
||
|
||
|
||
user_input = "Type something to begin..." | ||
|
||
print(user_input) | ||
|
||
while True: | ||
try: | ||
user_input = chatbot.get_input() | ||
|
||
bot_input = chatbot.get_response(user_input) | ||
|
||
except (KeyboardInterrupt, EOFError, SystemExit): | ||
break | ||
|