from ircBase import *
from bottle import route, run, template
import apTrackingModule
import MySQLdb as mdb
import time
import datetime
import ConfigParser
from multiprocessing import Process

config = ConfigParser.SafeConfigParser()
config.read('configs/ircBase.conf')

CONST_DB_USER = config.get('MySql', 'username')
CONST_DB_PASSWORD = config.get('MySql', 'password')

CONST_MODULE = None

class AndroidServiceModule(IrcModule):

    @route('/<action>/<name>')
    def index(action,name):
        returnMessage = ''
        if(action=='stats'):
            statsMessage = apTrackingModule.getApStatsForNick(mdb.connect('localhost',CONST_DB_USER, CONST_DB_PASSWORD, 'rafiBot'), name)
            returnMessage = '{'
            if 'drinking' in statsMessage:
                returnMessage += '"currentAP":"true",'
            returnMessage += '"message":"' + statsMessage + '"}'
        if(action=='start'):
            returnMessage = '{"message":"' + apTrackingModule.startTrackingApForNick(mdb.connect('localhost',CONST_DB_USER, CONST_DB_PASSWORD, 'rafiBot'), name) + '"}'
        if(action=='stop'):
            returnMessage = '{"message":"' + apTrackingModule.stopTrackingApForNick(mdb.connect('localhost',CONST_DB_USER, CONST_DB_PASSWORD, 'rafiBot'), name) + '"}'
        return returnMessage

    t = Process(target=run, kwargs=dict(host='0.0.0.0', port=31337))
    t.start()

    def defineResponses(self):
        print self
        print self.ircBot
        global CONST_MODULE
        CONST_MODULE = self

from ircBase import *
from bottle import route, run, template
import apTrackingModule
import time
import datetime
from multiprocessing import Process
 
def setup_server(theHost, thePort, theModule):
    route('/<action>/<name>')(theModule.index)
    run(host=theHost, port=thePort)
 
class AndroidServiceModule(IrcModule):
 
    def defineResponses(self):
        t = Process(target=setup_server, kwargs=dict(theHost='0.0.0.0', thePort=31337, theModule=self))
        t.start()
 
    def index(self, action, name):
 
        #Get the current database connection from the running module
        module_database_connection = self.ircBot.databaseConnection()
 
        #Perform ap tracking action for stats
        returnMessage = ''
        if(action=='stats'):
            statsMessage = apTrackingModule.getApStatsForNick(module_database_connection, name)
            self.ircBot.irc.sendMessage(IrcMessage.newRoomMessage(statsMessage))
            returnMessage = '{'
            if 'drinking' in statsMessage:
                returnMessage += '"currentAP":"true",'
            returnMessage += '"message":"' + statsMessage + '"}'
        
        #Perform ap tracking action for start
        if(action=='start'):
            startMessage = apTrackingModule.startTrackingApForNick(module_database_connection, name)
            self.ircBot.irc.sendMessage(IrcMessage.newRoomMessage(startMessage))
            returnMessage = '{"message":"' + startMessage + '"}'
 
        #Perform ap tracking action for stop
        if(action=='stop'):
            stopMessage = apTrackingModule.stopTrackingApForNick(module_database_connection, name)
            self.ircBot.irc.sendMessage(IrcMessage.newRoomMessage(stopMessage))
            returnMessage = '{"message":"' + stopMessage + '"}'
        return returnMessage

Rewrite from Mov1s.