My First Python Script


Uncategorized

Updated Aug 12th, 2021

Back in 2019, I wanted to auto-generate NPS reports from inMoment (previously called Mindshare) and have them sent to my email. In their application you could email reports but not set up “subscriptions.” You had to manually run the reports yourself. I needed the previous day’s results along with the month-to-date results. Easy enough. But I needed this for three brands and the location overall. So I needed to run 8 reports in total.

So I dug into Python a bit after reading “Automate the Boring Stuff with Python: Practical Programming for Total Beginners.” On May 12, 2019 I created my first Python script that had a terminal ask me a question and respond with a simple dynamic answer:

#this program says hello, asks for your name and age

print("Hello there.")
print("what is your name?") #ask for their name
myName=input()
print("it's good to meet you , " + myName)
print("The length of your name is: "), print(len(myName))
print ("what is your age?") #asks for their age
myAge=input()
print("Wow! " + myAge +". You will be " + str(int(myAge)+1)+ " in a year...")

print("Your Name is " + myName + " huh? like the three eyed raven " + myName + "?")

I then learned about Web Scraping with Beautiful Soup, the selenium headless browser and the chrome browser driver. I couldn’t grab all the elements needed without bringing in a library called “pyautogui” which allowed you to automatically control the mouse and keyboard with x/y coordinates. I didn’t know about asynchronous programming so I used sleep timers to make sure one task didn’t finish before the next.

It was terrible code but it worked.

Month’s after creating this, the company switched to Tableau for sales reporting. This platform had a “subscribe” feature that would auto-generate a report and send it to your email. This seemed like a feature that should have been built in to the customer-service scores platform.

#05.13.19
# note: this script file must be saved in the directory with the chrome browser driver executable (currently desktop/python)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import requests
from bs4 import BeautifulSoup


###requests info below this line
result = requests.get("https://www.inmoment.com/focus/#/units/3738970/views/2455?page=LegacyReports&reportId=139959")
src = result.content
print(src)

soup = BeautifulSoup(src, "lxml")

links = soup.find_all("a")
print(links)
print("\n")

Was able to automatically download and email reports:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import os
import pyautogui

# create a new instance of chrome driver
driver = webdriver.Chrome()

#set waits variable
z=1

# go to a specified url
driver.get("https://www.inmoment.com/report/app?service=page&page=Login")

# providelogincreds
usernameStr = 'myloginusername'
passwordStr = 'mypassword'

# fill in username
username = driver.find_element_by_id('username')
username.send_keys(usernameStr)

# fill in password
password = driver.find_element_by_id('password')
password.send_keys(passwordStr)

# click the next button to complete login
signInButton = driver.find_element_by_id('Any_3')
signInButton.click()

# maximize browser window
driver.maximize_window()
time.sleep(3)

#------------------------------Run MTD NPS Report-------------------------------------#

#after login go to default reporting page
driver.get(('https://www.inmoment.com/focus/#/units/3738970/views/2455?page=LegacyReports&reportId=139959'))
time.sleep(12)

#add 00775
pyautogui.keyDown('shift')
pyautogui.click(x=519, y=487)
pyautogui.keyUp('shift')
time.sleep(1)

pyautogui.click(x=810, y=428)#click date of survey
time.sleep(z)

pyautogui.click(x=810, y=348)#click current month
time.sleep(z)

pyautogui.click(x=1221, y=338)#click format
time.sleep(z)

pyautogui.click(x=1221, y=408)#click pdf option
time.sleep(z)

#click run button dropdown
pyautogui.click(x=1491, y=335)
time.sleep(z)

#click email option
pyautogui.click(x=1491, y=386)
time.sleep(z)

#click run button
#pyautogui.click(x=1421, y=334)
time.sleep(30)


#------------------------------Run YEST NPS Report------------------------------------#

#after login go to default reporting page
#note: uses refresh instead of get
driver.refresh()
time.sleep(12)

#add 00775
pyautogui.keyDown('shift')
pyautogui.click(x=519, y=487)
pyautogui.keyUp('shift')
time.sleep(z)

#click date of survey
pyautogui.click(x=810, y=428)
time.sleep(z)

#click yesterday
pyautogui.click(x=810, y=53)
time.sleep(z)

#click format
pyautogui.click(x=1221, y=338)
time.sleep(z)

#click pdf option
pyautogui.click(x=1221, y=408)
time.sleep(z)

#click run button dropdown
pyautogui.click(x=1491, y=335)
time.sleep(z)
#click email option
pyautogui.click(x=1491, y=386)

#click run button
#pyautogui.click(x=1421, y=334)
time.sleep(20)



#---------------------------Run YEST Comments Report----------------------------------#

#after login go to comments page
driver.get(('https://www.inmoment.com/focus/#/units/3738970/views/2455?page=LegacyReports&reportId=140338'))
time.sleep(8)

#add 00775
pyautogui.keyDown('shift')
pyautogui.click(x=519, y=487)
pyautogui.keyUp('shift')
time.sleep(z)
pyautogui.click(x=810, y=428)# click date of survey
time.sleep(z)
pyautogui.click(x=810, y=53)# click yesterday
time.sleep(z)
pyautogui.click(x=1221, y=338)# click format
time.sleep(z)
pyautogui.click(x=1221, y=408)# click pdf option
time.sleep(z)
pyautogui.click(x=1456, y=638)# click add column
time.sleep(z)
pyautogui.click(x=740, y=669)# click RA
time.sleep(z)
pyautogui.click(x=1050, y=595)#click ARROW to add RA
time.sleep(z)
pyautogui.click(x=1073, y=781)# click APPLY
time.sleep(z)
pyautogui.click(x=1491, y=335)# click run button dropdown
time.sleep(z)
pyautogui.click(x=1491, y=386)# click email option

#click run button
#pyautogui.click(x=1421, y=334)

#print("sleep for 60 seconds before quitting")
#time.sleep(60)
#driver.close()
#driver.quit()

Total novice from a programming aspect but this was really cool. Here’s the download script:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import os
import pyautogui

# create a new instance of chrome driver
driver = webdriver.Chrome()

#set waits variable
z=1

# go to a specified url
driver.get("https://www.inmoment.com/report/app?service=page&page=Login")

# providelogincreds
usernameStr = 'myusername'
passwordStr = 'mypassword'

# fill in username
username = driver.find_element_by_id('username')
username.send_keys(usernameStr)

# fill in password
password = driver.find_element_by_id('password')
password.send_keys(passwordStr)

# click the next button to complete login
signInButton = driver.find_element_by_id('Any_3')
signInButton.click()

# maximize browser window
driver.maximize_window()
time.sleep(3)

#------------------------------Run MTD NPS Report-------------------------------------#

#after login go to default reporting page
driver.get(('https://www.inmoment.com/focus/#/units/3738970/views/2455?page=LegacyReports&reportId=139959'))
time.sleep(8)

#add 00775
pyautogui.keyDown('shift')
pyautogui.click(x=519, y=487)
pyautogui.keyUp('shift')
time.sleep(1)

pyautogui.click(x=810, y=428)#click date of survey
time.sleep(z)

pyautogui.click(x=810, y=348)#click current month
time.sleep(z)

pyautogui.click(x=1221, y=338)#click format
time.sleep(z)

pyautogui.click(x=1221, y=408)#click pdf option
time.sleep(z)

#click run button dropdown
#pyautogui.click(x=1491, y=335)
#time.sleep(z)

#click email option
#pyautogui.click(x=1491, y=386)
#time.sleep(z)

#click run button
pyautogui.click(x=1421, y=334)
time.sleep(30)


#------------------------------Run YEST NPS Report------------------------------------#

#after login go to default reporting page
#note: uses refresh instead of get
driver.refresh()
time.sleep(5)

#add 00775
pyautogui.keyDown('shift')
pyautogui.click(x=519, y=487)
pyautogui.keyUp('shift')
time.sleep(z)

#click date of survey
pyautogui.click(x=810, y=428)
time.sleep(z)

#click yesterday
pyautogui.click(x=810, y=53)
time.sleep(z)

#click format
pyautogui.click(x=1221, y=338)
time.sleep(z)

#click pdf option
pyautogui.click(x=1221, y=408)
time.sleep(z)

#click run button dropdown
#pyautogui.click(x=1491, y=335)
#time.sleep(z)
#click email option
#pyautogui.click(x=1491, y=386)

#click run button
pyautogui.click(x=1421, y=334)
time.sleep(20)



#---------------------------Run YEST Comments Report----------------------------------#

#after login go to comments page
driver.get(('https://www.inmoment.com/focus/#/units/3738970/views/2455?page=LegacyReports&reportId=140338'))
time.sleep(5)

#add 00775
pyautogui.keyDown('shift')
pyautogui.click(x=519, y=487)
pyautogui.keyUp('shift')
time.sleep(z)
pyautogui.click(x=810, y=428)# click date of survey
time.sleep(z)
pyautogui.click(x=810, y=53)# click yesterday
time.sleep(z)
pyautogui.click(x=1221, y=338)# click format
time.sleep(z)
pyautogui.click(x=1221, y=408)# click pdf option
time.sleep(z)
pyautogui.click(x=1456, y=638)# click add column
time.sleep(z)
pyautogui.click(x=740, y=642)# click RA
time.sleep(z)
pyautogui.click(x=1050, y=563)#click ARROW to add RA
time.sleep(z)
pyautogui.click(x=1073, y=748)# click APPLY
time.sleep(z)
#pyautogui.click(x=1491, y=335)# click run button dropdown
#time.sleep(z)
#pyautogui.click(x=1491, y=386)# click email option

#click run button
pyautogui.click(x=1421, y=334)

#print("sleep for 60 seconds before quitting")
#time.sleep(60)
#driver.close()
#driver.quit()


Top