#!/usr/bin/python
"""
 Author : Saikiranuppu
 Version: 1.0.0
 Created: 2017-11-27
"""

import MySQLdb
import argparse

"""
    Database Variables
"""
mysql_host = ""
mysql_user = "root"
mysql_pass = "toor"
mysql_database = "malware"

def query(mime,source,severity,author,tags,size_low,size_high):
    try:
        connection = MySQLdb.connect(mysql_host,mysql_user,mysql_pass,mysql_database)
        cursor = connection.cursor()
        sql = "SELECT * from `file_meta` where `mime` = %s and `file_source` = %s and `severity` =%s and `author` = %s and `file_size` BETWEEN %s and %s"
        cursor.execute(sql,(mime,source,severity,author,size_low,size_high))
        rows = cursor.fetchall()
        for row in rows:
            print row[8]
    except Exception as e:
        print str(e)
        import traceback
        traceback.print_exc()



def main():
    parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument("-m" ,
                        "--mime",
                       required=True,
                       help="Mime Type of Files for query")
    parser.add_argument("-s",
                        "--size",type=str,
                        help="Size query of files (Specify Range in MB like 1,2 or 1 )")
    parser.add_argument("-src","--source",required=True,
                        help="Source from which malware is downloaded")
    parser.add_argument("-sev","--severity",required=True,
                        choices=set(('clean','malicious')),
                       help="Benign or Malicious")
    parser.add_argument("-a","--author",required=True,
                        help="Author name")
    parser.add_argument("-t","--tags",default='',
                        help="Tags to classify the samples")
    args = parser.parse_args()
    source = args.source
    severity = args.severity
    author = args.author
    tags = args.tags
    mime = args.mime
    my_list = [float(item) for item in args.size.split(',')]
    try:
        if not my_list[1]:
            pass
    except:
        my_list.append(999999999)
    size_low = my_list[0]*1024*1024
    size_high = my_list[-1]*1024*1024


    query(mime,source,severity,author,tags,size_low,size_high)
if __name__ == '__main__':
    main()
