示例#1
0
class TweetListener(StreamListener):
    def __init__(self, host, port):
        self.sock = socket.socket()
        self.sock.connect((host, port))
        self.queryEngine = QueryEngine()

    def checkWords(self, wordList, text):
        for word in wordList:
            if word in text:
                return True
        return False
    def on_data(self, data):
        #print data
        print 'START'
        newdata = json.loads(data)
        keywords = Constants.KEYWORDS
        if "text" in newdata:
            text = newdata["text"]
            
            #print 'DATA', data
            timestamp = newdata["timestamp_ms"]
            keywordMap = {}
            text = text.replace("'", "")
            # print 'TEXT', text
            for word in keywords:
                #print "word", word
                #print 'text', text
                wordList = keywords[word]

                if self.checkWords(wordList,text):
                    if not word in keywordMap:
                        keywordMap[word]=1
                    else:
                        keywordMap[word]+=1
                # print "keyword map", keywordMap
            currMax = 0
            maxKeyWord = ''
            for word in keywordMap.keys():
                if keywordMap[word] > currMax:
                    currMax = keywordMap[word]
                    maxKeyWord = word
            print keywordMap
            
            queryString = self.queryEngine.serialize([QueryType.INSERT, timestamp, maxKeyWord, 1])
            print queryString
            self.sock.send(queryString)


     #       df1= json.loads(data)
     #       print "df1", df1
     #       df = DataFrame(df1)
            #print "df", df
     #       df.to_excel('twitter-excel.xlsx', sheet_name='sheet1', index=False)
            return True

    def on_error(self, status):
        print status

    def close(self):
        self.sock.close()
示例#2
0
def clientthread(conn):
    qe = QueryEngine()
    #Sending message to connected client
    #conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string

    #infinite loop so that function do not terminate and thread do not end.
    while True:
        #Receiving from client
        reply = ''
        data = conn.recv(1024)
        #print data
        
        params = qe.deserialize(data)
        param_type = params[0]
        print param_type
        print 'PARAMS', params
        if param_type == QueryType.SELECT:
            reply = db.select(msToSec(params[1]), params[2])

        elif param_type == QueryType.INSERT:
            if params[2]=='':
                pass
            else:
                db.insert(msToSec(params[1]),params[2], params[3])
                reply = "inserting " + data
            
            #reply = db.insert(params[1:])
        elif param_type == QueryType.UPDATE:
            reply = "updating"
            #reply = db.update(params[1:])
        elif param_type == QueryType.SELECTRANGE:
            #dbResponse = list(db.selectRangeForDisplay(msToSec(params[1]), msToSec(params[2]), params[3]))
            dbResponse = list(db.selectRangeAndInterval(msToSec(params[1]), msToSec(params[2]), params[3],params[4]))
            dbResponse.insert(0, QueryType.SERVER_REPLY)
            reply = qe.serialize(dbResponse)
        elif param_type == QueryType.INC_AVG:
            reply = db.getRunningAverage(params[1]);
        elif param_type == QueryType.INC_COUNT:
            reply = db.getAggregateCountInRange(msToSec(params[1]), msToSec(params[2]), params[3])
        elif param_type == QueryType.INC_VAR:
	    reply = db.getRunningVariance(params[1]);
	else:
            # throw exception
            reply = "Invalid arguments, should be start with SELECT, INSERT, or UPDATE"
        print reply
        conn.sendall(reply)
    
        
            #conn.close()
    conn.close()