data_sender.py 3.63 KB
import os.path
import sys
import requests
import sqlite3
import os

def create_adb_info_db():
	path = os.path.expanduser('~')+"\\Desktop\\batch_file\\"
	if(os.path.isfile(path+'adb_info.db')):
		os.remove(path+'adb_info.db')
	conn=sqlite3.connect('adb_info.db')
	c = conn.cursor()
	c.execute("CREATE TABLE process (UID TEXT, PID TEXT, PPID TEXT, C TEXT, STIME TEXT, TTY TEXT, TIME TEXT, CMD TEXT)")
	c.execute("CREATE TABLE info (android_version TEXT, sdk_version TEXT, device TEXT, model TEXT, uptime TEXT)")
	c.execute("CREATE TABLE alarm (TIME TEXT, TYPE TEXT, DETAIL TEXT)")
	conn.commit()
	c.close()

def insert_alarm_db_file(info):
	conn=sqlite3.connect('adb_info.db')
	c = conn.cursor()
	c.execute(
		'insert into alarm(TIME, TYPE, DETAIL) values (?, ?, ?)'\
		, (info[0], info[1], info[2])
	)
	conn.commit()
	c.close()

def insert_info_db_file(info):
	conn=sqlite3.connect('adb_info.db')
	c = conn.cursor()
	c.execute(
		'insert into info(android_version, sdk_version, device, model, uptime) values (?, ?, ?, ?, ?)'\
		, (info[0], info[1], info[2], info[3], info[4])
	)
	conn.commit()
	c.close()

def insert_process_db_file(data_path):
	conn=sqlite3.connect('adb_info.db')
	c = conn.cursor()
	with open(data_path) as f:
		file_data = f.readlines()
		for item in file_data:
			info = item.strip().split()
			c.execute(
				'insert into process(UID, PID, PPID, C, STIME, TTY, TIME, CMD) values (?, ?, ?, ?, ?, ?, ?, ?)'\
				, (info[0], info[1], info[2], info[3], info[4], info[5], info[6], info[7])
			)
	conn.commit()
	c.close()

def data_processing(data_path, info):
	while(True):
		if(os.path.isfile(data_path)):
			print("file")
			break
		else:
			print("x")
			continue
	if(data_path.endswith('android_version.txt') or data_path.endswith('sdk_version.txt') or data_path.endswith('model.txt') or data_path.endswith('uptime.txt')):
		with open(data_path) as f:
			file_data = f.readlines()
			info.append(file_data[0].strip())
			print(info)
		if(data_path.endswith('uptime.txt')):
			insert_info_db_file(info)
	elif(data_path.endswith('device.txt')):
		with open(data_path) as f:
			f.readline()
			file_data = f.readlines()
			info.append((file_data[0].strip().split())[0])
			print(info)
	elif(data_path.endswith('process.txt')):
		insert_process_db_file(data_path)
	elif(data_path.endswith('alarm.txt')):
		with open(data_path) as f:
			f.readline()
			file_data = f.readlines()
			for i in file_data:
				if(i.endswith("wakeups:\n")):
					continue
				else:
					if("[set]" in i or "[trigger]" in i  or "[remove]" in i):
						alarm_list = []
						temp = i.strip().split('[') #time
						temp2 = temp[1].split(']')  #set 정보, detail
						alarm_list.append(temp[0])
						alarm_list.extend(temp2)
						print(alarm_list)
						insert_alarm_db_file(alarm_list)

def data_transfer(data_path):
	with open(data_path, 'rb') as files:
		header = {'Authorization':'..',}
		upload = {'file':files}
		response = requests.post('http://13.209.3.132/extractions', files=upload, headers=header)
		print(response.text)

def main():
	try:
		info = []
		path = os.path.expanduser('~')+"\\Desktop\\batch_file\\"
		data_tup = [path+"android_version.txt", path+"sdk_version.txt", path+"device.txt", path+"model.txt", path+"uptime.txt", path+"process.txt", path+"alarm.txt", path+"InnerDatabase_test.db", path+"networkDatabase_test.db"]
		create_adb_info_db()
		for i in data_tup:
			print(i)
			data_processing(i, info)
		trans_data_tup = [path+"adb_info.db", path+"InnerDatabase_test.db", path+"networkDatabase_test.db"]
		for i in trans_data_tup:
			print(i)
			data_transfer(i)
	except Exception as e:
		exc_type, exc_obj, tb = sys.exc_info()
		print('[error line No = {}]'.format(tb.tb_lineno))
		print(e)

main()