"""Coordinates the twitter api with the markov chain models"""
import json

from tweepy import Stream, OAuthHandler, API
from tweepy.streaming import StreamListener

from holy_markov import OldTestaPassagesMarkov
from twitter_secrets import api_tokens as at


class HolyListener(StreamListener):

    old_testa = OldTestaPassagesMarkov()

    def on_connect(self):
        print("Connection established!!")

    def on_disconnect(self, notice):
        print("Connection lost!! : ", notice)

    def on_direct_message(self, status):
        dm = status._json['direct message']
        screen_name = dm['sender_screen_name']

        print("Entered on_direct_message().", 'Passage sent to @' + screen_name)

        passage = self.old_testa.twitter_message(line_length=(140 - len(screen_name) - 2))
        tweet = ''.join(['@', screen_name, ' ', passage])
        try:
            self.api.update_status(tweet)
            return True
        except BaseException as e:
            print("Failed on_direct_message()", str(e))

    def on_event(self, status):
        print('Entered on_event()')
        print(status)

    def on_error(self, status):
        print('Entered on_error()')
        print(status)


def main():
    """The main event loop for the holy twitter bot

    It watches for twitter events, and posts randomly generated holy text to twitter.
    """
    try:
        auth = OAuthHandler(at['CONSUMER_KEY'], at['CONSUMER_SECRET'])
        auth.secure = True
        auth.set_access_token(at['ACCESS_KEY'], at['ACCESS_SECRET'])

        api = API(auth)

        # If the authentication was successful, you should
        # see the name of the account print out
        print(api.me().name)

        stream = Stream(auth, HolyListener(api=api))

        stream.userstream()

    except BaseException as e:
        print("Error in main()", e)


if __name__ == '__main__':
    main()

@@ -67,16 +67,42 @@
 ort json
+%0Afrom pprint import pprint
 %0A%0Afrom t
@@ -361,246 +361,81 @@
 v()%0A
-%0A
     
-def on_connect(self):%0A        print(%22Connection established!!%22)%0A%0A    def on_disconnect(self, notice):%0A        print(%22Connection lost!! : %22, notice)%0A%0A    def on_direct_message(self, status):%0A        dm = status._json%5B'direct message'%5D
+bot_name = 'HolyStupidArt'%0A%0A    def send_passage(self, screen_name):%0A
 %0A   
@@ -439,16 +439,19 @@
         
+if 
 screen_n
@@ -458,82 +458,44 @@
 ame 
+!
 = 
-dm%5B'sender_screen_name'%5D%0A%0A        print(%22Entered on_direct_message().%22, 
+self.bot_name:%0A            print(
 'Pas
@@ -521,25 +521,28 @@
 creen_name)%0A
-%0A
+    
         pass
@@ -620,16 +620,20 @@
 ) - 2))%0A
+    
         
@@ -686,20 +686,8 @@
 e%5D)%0A
-        try:
 %0A   
@@ -729,31 +729,331 @@
 et)%0A
+%0A
     
-        return True
+def on_connect(self):%0A        print(%22Connection established!!%22)%0A%0A    def on_disconnect(self, notice):%0A        print(%22Connection lost!! : %22, notice)%0A%0A    def on_direct_message(self, status):%0A        try:%0A            dm = status._json%5B'direct_message'%5D%0A            self.send_passage(screen_name=dm%5B'sender_screen_name'%5D)
 %0A   
@@ -1139,16 +1139,70 @@
  str(e))
+%0A            pprint(status._json)%0A%0A        return True
 %0A%0A    de

Refactor on_direct_message