__author__ = 'nikhil'
#!flask/bin/python
#from bottle import Flask
import bottle
from bottle import request,route,static_file,run,redirect,response
import data_consolidation as dc
import solution as randomForestRun
import shutil
from boto.s3.connection import S3Connection
from boto.s3.key import Key
import sys

import os
import gzip
import csv
import pymysql
import json
import urllib2



class EnableCors(object):
    name = 'enable_cors'
    api = 2

    def apply(self, fn, context):
        def _enable_cors(*args, **kwargs):
            # set CORS headers
            response.headers['Access-Control-Allow-Origin'] = '*'
            response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
            response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'

            if bottle.request.method != 'OPTIONS':
                # actual request; reply with the actual response
                return fn(*args, **kwargs)

        return _enable_cors


app = bottle.app()


@app.route('/cors', method=['OPTIONS', 'GET'])
def lvambience():
    response.headers['Content-type'] = 'application/json'
    #return mydata

project_path = '/home/voldy/Desktop/MalwareClassify/'

@route('/getJson', method='GET')
def getJson():
    conn = S3Connection('AKIAJUTOZCAQ4FSBEOUA', '2wFlkO2utSRdl9oPKaJsmQHEHeKMN8XibME9sxZN')
    pb = conn.get_bucket('cmpe239.malware')
    key = pb.get_key("file.json")
    jsonString = key.get_contents_as_string()
    #print "-----------------------------------------JSON String "+jsonString
    return  jsonString
@route('/uploads', method='POST')
def results():

    deleteFiles()
    name = request.forms.get("user_id")
    print(name)
    category = request.forms.get('category')
    upload = request.files.get('upload')
    print upload

    save_path = project_path+"data/consolidate_op/test/".format(category=category)
    if not os.path.exists(save_path):
        os.makedirs(save_path)

    file_path = "{path}/{file}".format(path=save_path,file=upload.filename)

    # Save file to local system
    upload.save(file_path)

    if not upload.filename.endswith(".bytes"):
        in_file = open(file_path, "rb") # opening for [r]eading as [b]inary
        data = in_file.read() # if you only wanted to read 512 bytes, do .read(512)
        in_file.close()

        out_file = open(file_path+".bytes", "wb") # open for [w]riting as [b]inary

        out_file.write(data)
        out_file.close()
    #Read file
    inF = file(file_path, 'rb')
    s = inF.read()
    inF.close()

    #Write it in .gz format for further processing
    outF = gzip.GzipFile(file_path+".gz", 'wb')
    outF.write(s)
    outF.close()


    print "File upload success"

    # Consolidate the .gz file to create test and train data
    print save_path
    dc.consolidate(save_path)

    #Run the random Forest
    randomForestRun.randomForest()


    #Database Conenction string
    connection = pymysql.connect("dbinstance-sjsu-amogh.cyht8ykut6xk.us-west-1.rds.amazonaws.com","voldy","voldysjsu","malwareClassify")
    #jsonfile = open('C:/Users/SCS_USER/Desktop/file.json', 'w')
    cursor = connection.cursor()


    csv_data = csv.reader(file(project_path+'data/output/submission.csv'))
    f = open( project_path+'data/output/submission.csv', 'rU' )
    innerLoopCounter=0
    outLoopCounter=0

    out=""
    for row in csv_data:
        print row[1]
        if outLoopCounter==0:
            print "hi"
            outLoopCounter+=1
        else:
            print "In else"
            str ='%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', [row]
            #fieldnames = ("Id", "Prediction1","Prediction2","Prediction3","Prediction4","Prediction5","Prediction6","Prediction7","Prediction8","Prediction9","user_id")
            fieldnames = ("Id","Prediction1","Prediction2","Prediction3","Prediction4","Prediction5","Prediction6","Prediction7","Prediction8","Prediction9")
            reader = csv.DictReader( f, fieldnames)
            #Writing JSON
            for newRow in reader:
                if innerLoopCounter==1:
                    print newRow
                    out = json.dumps(newRow)
                innerLoopCounter+=1
            #print 'INSERT INTO `Prediction` VALUES ( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', [row]
            f = open( project_path+'data/output/file.json', 'w')
            try:
                f.write(out)
                f.close()


                conn = S3Connection('AKIAJUTOZCAQ4FSBEOUA', '<KEY>')
                pb = conn.get_bucket('cmpe239.malware')

                k = Key(pb)
                #https://s3-us-west-1.amazonaws.com/cmpe239.malware/json/submission
                #file_name_to_use_in_s3 = "%s/%s"%('https://s3-us-west-1.amazonaws.com/cmpe239.malware/json', os.path.basename( project_path+'data/output/file.json'))
                k.name = "file.json"
                #print file_name_to_use_in_s3
                k.set_contents_from_filename( project_path+'data/output/file.json')
                #sys.exit(0)
            except RuntimeError:
                print RuntimeError.message
                #Writing to Database
            #Added new
            '''
            with open(project_path+'data/output/{0}file.json'.format(row[0])) as data_file:
                newdata = json.load(data_file)

            req = urllib2.Request('http://localhost:8080/')
            req.add_header('Content-Type', 'application/json')

            response = urllib2.urlopen(req, json.dumps(newdata))
            '''
            #End of newly added file
            cursor.execute("INSERT INTO Prediction( \
    Id, Prediction1,Prediction2,Prediction3,Prediction4,Prediction5,Prediction6,Prediction7,Prediction8,Prediction9,user_id) VALUES ( '{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}','{7}','{8}','{9}','{10}')".format(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9],name))
    #close the connection to the database.

    connection.commit()
    cursor.close()

    redirect("http://localhost:63342/MalwareClassify/Web/result.html")


def deleteFiles():
    #Remove output and test folder
    #shutil.rmtree(project_path+'data/consolidate_op/test')
    #shutil.rmtree(project_path+'data/output')



    shutil.rmtree(os.path.join(project_path,'data/consolidate_op/test'))
    shutil.rmtree(os.path.join(project_path,'data/output'))
    #Re create them
    #os.makedirs(project_path+'data/consolidate_op/test')
    #os.makedirs(project_path+'data/output')


    os.makedirs(os.path.join(project_path,'data/consolidate_op/test'))
    os.makedirs(os.path.join(project_path,'data/output'))





if __name__ == '__main__':

    app.install(EnableCors())

    app.run(port=8080)
    #run(host='localhost',port =8080)

__author__ = 'nikhil'
#!flask/bin/python
#from bottle import Flask
import bottle
from bottle import request,route,static_file,run,redirect,response
import data_consolidation as dc
import solution as randomForestRun
import shutil
from boto.s3.connection import S3Connection
from boto.s3.key import Key
import sys

import os
import gzip
import csv
import pymysql
import json
import urllib2



class EnableCors(object):
    name = 'enable_cors'
    api = 2

    def apply(self, fn, context):
        def _enable_cors(*args, **kwargs):
            # set CORS headers
            response.headers['Access-Control-Allow-Origin'] = '*'
            response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
            response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'

            if bottle.request.method != 'OPTIONS':
                # actual request; reply with the actual response
                return fn(*args, **kwargs)

        return _enable_cors


app = bottle.app()


@app.route('/cors', method=['OPTIONS', 'GET'])
def lvambience():
    response.headers['Content-type'] = 'application/json'
    #return mydata

project_path = '/home/voldy/Desktop/MalwareClassify/'

@route('/getJson', method='GET')
def getJson():
    conn = S3Connection('AKIAJUTOZCAQ4FSBEOUA', '2wFlkO2utSRdl9oPKaJsmQHEHeKMN8XibME9sxZN')
    pb = conn.get_bucket('cmpe239.malware')
    key = pb.get_key("file.json")
    jsonString = key.get_contents_as_string()
    #print "-----------------------------------------JSON String "+jsonString
    return  jsonString
@route('/uploads', method='POST')
def results():

    deleteFiles()
    name = request.forms.get("user_id")
    print(name)
    category = request.forms.get('category')
    upload = request.files.get('upload')
    print upload

    save_path = project_path+"data/consolidate_op/test/".format(category=category)
    if not os.path.exists(save_path):
        os.makedirs(save_path)

    file_path = "{path}/{file}".format(path=save_path,file=upload.filename)

    # Save file to local system
    upload.save(file_path)

    if not upload.filename.endswith(".bytes"):
        in_file = open(file_path, "rb") # opening for [r]eading as [b]inary
        data = in_file.read() # if you only wanted to read 512 bytes, do .read(512)
        in_file.close()

        out_file = open(file_path+".bytes", "wb") # open for [w]riting as [b]inary

        out_file.write(data)
        out_file.close()
    #Read file
    inF = file(file_path, 'rb')
    s = inF.read()
    inF.close()

    #Write it in .gz format for further processing
    outF = gzip.GzipFile(file_path+".gz", 'wb')
    outF.write(s)
    outF.close()


    print "File upload success"

    # Consolidate the .gz file to create test and train data
    print save_path
    dc.consolidate(save_path)

    #Run the random Forest
    randomForestRun.randomForest()


    #Database Conenction string
    connection = pymysql.connect("dbinstance-sjsu-amogh.cyht8ykut6xk.us-west-1.rds.amazonaws.com","voldy","voldysjsu","malwareClassify")
    #jsonfile = open('C:/Users/SCS_USER/Desktop/file.json', 'w')
    cursor = connection.cursor()


    csv_data = csv.reader(file(project_path+'data/output/submission.csv'))
    f = open( project_path+'data/output/submission.csv', 'rU' )
    innerLoopCounter=0
    outLoopCounter=0

    out=""
    for row in csv_data:
        print row[1]
        if outLoopCounter==0:
            print "hi"
            outLoopCounter+=1
        else:
            print "In else"
            str ='%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', [row]
            #fieldnames = ("Id", "Prediction1","Prediction2","Prediction3","Prediction4","Prediction5","Prediction6","Prediction7","Prediction8","Prediction9","user_id")
            fieldnames = ("Id","Prediction1","Prediction2","Prediction3","Prediction4","Prediction5","Prediction6","Prediction7","Prediction8","Prediction9")
            reader = csv.DictReader( f, fieldnames)
            #Writing JSON
            for newRow in reader:
                if innerLoopCounter==1:
                    print newRow
                    out = json.dumps(newRow)
                innerLoopCounter+=1
            #print 'INSERT INTO `Prediction` VALUES ( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', [row]
            f = open( project_path+'data/output/file.json', 'w')
            try:
                f.write(out)
                f.close()


                conn = S3Connection('AKIAJUTOZCAQ4FSBEOUA', '<KEY>')
                pb = conn.get_bucket('cmpe239.malware')

                k = Key(pb)
                #https://s3-us-west-1.amazonaws.com/cmpe239.malware/json/submission
                #file_name_to_use_in_s3 = "%s/%s"%('https://s3-us-west-1.amazonaws.com/cmpe239.malware/json', os.path.basename( project_path+'data/output/file.json'))
                k.name = "file.json"
                #print file_name_to_use_in_s3
                k.set_contents_from_filename( project_path+'data/output/file.json')
                #sys.exit(0)
            except RuntimeError:
                print RuntimeError.message
                #Writing to Database
            #Added new
            '''
            with open(project_path+'data/output/{0}file.json'.format(row[0])) as data_file:
                newdata = json.load(data_file)

            req = urllib2.Request('http://localhost:8080/')
            req.add_header('Content-Type', 'application/json')

            response = urllib2.urlopen(req, json.dumps(newdata))
            '''
            #End of newly added file
            cursor.execute("INSERT INTO Prediction( \
    Id, Prediction1,Prediction2,Prediction3,Prediction4,Prediction5,Prediction6,Prediction7,Prediction8,Prediction9,user_id) VALUES ( '{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}','{7}','{8}','{9}','{10}')".format(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9],name))
    #close the connection to the database.

    connection.commit()
    cursor.close()

    redirect("http://localhost:63342/MalwareClassify/Web/result.html")


def deleteFiles():
    #Remove output and test folder
    #shutil.rmtree(project_path+'data/consolidate_op/test')
    #shutil.rmtree(project_path+'data/output')



    shutil.rmtree(os.path.join(project_path,'data/consolidate_op/test'))
    shutil.rmtree(os.path.join(project_path,'data/output'))
    #Re create them
    #os.makedirs(project_path+'data/consolidate_op/test')
    #os.makedirs(project_path+'data/output')


    os.makedirs(os.path.join(project_path,'data/consolidate_op/test'))
    os.makedirs(os.path.join(project_path,'data/output'))





if __name__ == '__main__':

    app.install(EnableCors())

    app.run(port=8080)
    #run(host='localhost',port =8080)

#!flask/bin/python #from bottle import Flask # set CORS headers # actual request; reply with the actual response #return mydata #print "-----------------------------------------JSON String "+jsonString # Save file to local system # opening for [r]eading as [b]inary # if you only wanted to read 512 bytes, do .read(512) # open for [w]riting as [b]inary #Read file #Write it in .gz format for further processing # Consolidate the .gz file to create test and train data #Run the random Forest #Database Conenction string #jsonfile = open('C:/Users/SCS_USER/Desktop/file.json', 'w') #fieldnames = ("Id", "Prediction1","Prediction2","Prediction3","Prediction4","Prediction5","Prediction6","Prediction7","Prediction8","Prediction9","user_id") #Writing JSON #print 'INSERT INTO `Prediction` VALUES ( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', [row] #https://s3-us-west-1.amazonaws.com/cmpe239.malware/json/submission #file_name_to_use_in_s3 = "%s/%s"%('https://s3-us-west-1.amazonaws.com/cmpe239.malware/json', os.path.basename( project_path+'data/output/file.json')) #print file_name_to_use_in_s3 #sys.exit(0) #Writing to Database #Added new with open(project_path+'data/output/{0}file.json'.format(row[0])) as data_file:                 newdata = json.load(data_file)              req = urllib2.Request('http://localhost:8080/')             req.add_header('Content-Type', 'application/json')              response = urllib2.urlopen(req, json.dumps(newdata)) #End of newly added file #close the connection to the database. #Remove output and test folder #shutil.rmtree(project_path+'data/consolidate_op/test') #shutil.rmtree(project_path+'data/output') #Re create them #os.makedirs(project_path+'data/consolidate_op/test') #os.makedirs(project_path+'data/output') #run(host='localhost',port =8080)