Search code examples
pythonautomationpyautogui

Alternative to pyautogui for desktop automation


I have created a software that processes images to do automation of desktop application Itunes. It is working fine but I want it to run on servers. But it would not have visual data while on server. The code I wrote is below

import pyautogui
import time


def locate(icon):
        time.sleep(1)
        while True:
            coord = pyautogui.locateOnScreen(icon)

            if coord == None:
                continue
            else:
                break

        return coord

def locateCenter(icon):
    while True:
        coord = pyautogui.locateCenterOnScreen(icon)

        if coord == None:
            continue
        else:
            break

    return coord
def locateChance(icon):
    time.sleep(3)
    ct = 0
    while True:
        if icon == "data\\play.png":
            coord = pyautogui.locateOnScreen(icon)
            ct = ct +1
            if ct == 4:
                coord = (0,0,0,0)
                break
        else:
            coord = pyautogui.locateCenterOnScreen(icon)
            ct = ct +1
            if ct == 4:
                coord = (0,0)
                break
        if coord == None:
            continue
        else:
            break

    return coord

def sign_out():

    acnt_coord = locate("data\\acnt_tab.png")
    pyautogui.click(acnt_coord)
    time.sleep(0.5)
    so_coord = pyautogui.locateOnScreen("data\\sign_out.png")
    if so_coord == None:
        pyautogui.press("return")
        time.sleep(1)
        pyautogui.leftClick()
        time.sleep(0.5)
        so_coord = pyautogui.locateOnScreen("data\\sign_out.png")
        

    pyautogui.click(so_coord)


#---------------------------------------------------------opening itunes---------------------------------------------------------------
pyautogui.press("win")
time.sleep(1)
pyautogui.typewrite("itune")
time.sleep(1)
pyautogui.press("return")
time.sleep(4)


data = open("data.csv","r").readlines()

#---------------------------------------------------------main loop--------------------------------------------------------------------
for i in data:
    
    i = i.strip()

    if i == data[0].strip() or i.strip() == "":
        continue
    print(f"Processing ID#{i[0]}")
    j = i.split(",")
    app_id = j[1]
    pswd = j[2]
    redeem_code = j[3]
    song = j[4]


#--------------------------------------------------Clicking store and open radeem------------------------------------------------------
    while True:
        st_coord = pyautogui.locateOnScreen("data\\store_icon.png")
        if st_coord == None:
            st_coord = pyautogui.locateOnScreen("data\\store_icon_2.png")
        if st_coord == None:
            continue
        else:
            break
    pyautogui.click(st_coord)
    time.sleep(5)
    rd_coord = locate("data\\redeem_icon.png")
    pyautogui.click(rd_coord)
#------------------------------------------------------------SignIn--------------------------------------------------------------------
    time.sleep(3)
    x, y = locateCenter("data\\ap_id3.png")
    pyautogui.moveTo(x,y)
    pyautogui.click(x, y-135 , clicks=2)
    pyautogui.press("backspace")
    pyautogui.typewrite(app_id)
    time.sleep(1)
    pyautogui.press("tab")
    pyautogui.typewrite(pswd)
    si_coord = locate("data\\sign_in.png")
    pyautogui.click(si_coord)

#------------------------------------------------------------Redeem Code--------------------------------------------------------------
    rd_btn_coord = locate("data\\rd_btn.png")
    pyautogui.moveTo(rd_btn_coord)
    pyautogui.dragRel(-100,0)
    pyautogui.click()
    time.sleep(0.5)
    pyautogui.typewrite(redeem_code)
    time.sleep(1)
    pyautogui.click(rd_btn_coord)
    time.sleep(1)
    x, y = locateChance("data\\rd_error.png")
    if x != 0 and y != 0:
        print(f"ID#{data.index(i)} Invalid Redeem Code. Continuing to next ID...")
        sign_out()
        continue

#-------------------------------------------------------Remove payment method--------------------------------------------------------
    st_coord = locate("data\\store_icon.png")
    pyautogui.click(st_coord)
    x,y = locateCenter("data\\redeem_icon.png")
    pyautogui.click(x+120, y)
    time.sleep(2)
    pyautogui.click(locateCenter("data\\manage_payment.png"))
    time.sleep(1)
    locate("data\\wait1.png")
    x, y = locateChance("data\\remove.png")
    if x != 0 and y != 0:
        pyautogui.click(x, y)
    time.sleep(2)
    print("completed")
#-----------------------------------------------------------searching song------------------------------------------------------------
    srch_coord = locate("data\\search.png")
    print(srch_coord)
    pyautogui.click(srch_coord)
    time.sleep(1)
    pyautogui.typewrite(song)
    time.sleep(1)
    pyautogui.press("return")
#-----------------------------------------------------------Purchasing song-----------------------------------------------------------
    x,y = locateCenter("data\\price.png")
    chord = locateChance("data\\play.png")

    print(x,y)
    if x > (chord[0]-50) and x < (chord[0] + chord[2]+50) and (y+37) > (chord[1]-20) and (y+37) < (chord[1] + chord[3]+20):
        print(f"ID#{data.index(i)} Song already purchased. Continuing to the next ID...")
        sign_out()
        continue
    else:
        pyautogui.click(x+10 , y+37)
    
    x, y = locateCenter("data\\buy.png")
    pyautogui.click(x, y-128 , clicks=2)

    pyautogui.press("backspace")
    pyautogui.typewrite(app_id)
    time.sleep(1)
    pyautogui.press("tab")
    pyautogui.typewrite(pswd)

    buyCoord = locateCenter("data\\buy2.png")
    pyautogui.click(buyCoord)
    time.sleep(6)
    
    x, y =locateChance("data\\buy3.png")
    if x != 0 and y != 0:
        pyautogui.click(x, y)
    time.sleep(6)

    x, y = locateChance("data\\no_auto_download.png")
    if x != 0 and y != 0:
        pyautogui.click(x,y)
    time.sleep(6)

    x, y = locateChance("data\\always.png")
    if x !=0 and y !=0:
        pyautogui.click(x,y)

    ##############################################can be signin#####################################
    

    #signout in the end
    sign_out()

pyautogui.keyDown("alt")
pyautogui.press("F4")
pyautogui.keyUp("alt")
    
print(f"Task Completed!\n{str(len(data)-1)} ID(s) Processed")
time.sleep(2)
print("Exiting...")
time.sleep(2)

The data.csv file would be like

#,ID,password,redeem code,song
1,fani@gmail.com,password,X4TRYZ92K7LXPGYM,alone

Question: So the problem is I can not run it on server. Can any one tell if it is possible to run it on AWS. Also if not is there a way to create an automation script that can run on servers. Would there be a possibility of using multithreading. I have done the work I wanted. This question is not for spamming itunes but only to get knowledge. As these questions came into my mind when I completed the script. I have not attached the images of button because the purpose of code is to give idea of what I want to do.


Solution

  • you need have a windows desktop session on aws vm, you need keep the desptop on aws, don't lock screen automatically, you can find one always on machine, rdp to the aws vm. keep rdp connneciton on.

    for more about windows session, station, desktop, you can refer to: https://techcommunity.microsoft.com/t5/ask-the-performance-team/sessions-desktops-and-windows-stations/ba-p/372473#:~:text=A%20desktop%20is%20a%20session-specific%20paged%20pool%20area,and%20each%20windows%20station%20can%20have%20multiple%20desktops.