Advertisement
Guest User

Nendoroid Checker

a guest
Nov 6th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. """
  2.   Sean Nendoroid Checker
  3.   A dumb web page checker - checks if a value on a webpage has changed.
  4.   Built to monitor Sean Shin's figure collection - primarily so he can be judged
  5.  
  6.   Author: Samuel Pell
  7. """
  8. import requests as req
  9. import sys
  10. import smtplib
  11. from email.mime.text import MIMEText
  12.  
  13. USER = ""
  14. URL = "https://myfigurecollection.net/profile/sparky"
  15. PASSWORD = ""
  16. RECIPIENT = ""
  17. SEARCH_TERM = "owned ({})"
  18. FILENAME = 'prev_nendroid_count.txt'
  19.  
  20. def get_page(url):
  21.     try:
  22.         response = req.request('GET', url)
  23.         return response.content
  24.     except req.exceptions.ConnectionError:
  25.         sys.exit("Error")
  26.  
  27.  
  28. def send_email(current, prev):
  29.     msg = MIMEText("Sean now has {} nendoroids, this is up from {} previously.\n- With Love, your ever vigilant,\n\nBot-chan".format(current, prev))
  30.  
  31.     msg['From'] = USER
  32.     msg['Subject'] = "!!Nendoroid Alert!!"
  33.     msg['To'] = RECIPIENT
  34.  
  35.     s = smtplib.SMTP(host='smtp.gmail.com', port=587)
  36.     s.starttls()
  37.     s.login(USER, PASSWORD)
  38.     s.send_message(msg)
  39.     s.quit()
  40.  
  41.  
  42. def main():
  43.     #Get the previous recorded number
  44.     file = open(FILENAME, 'r')
  45.     prev = int(file.readline())
  46.     file.close()
  47.  
  48.     #Get the web page to check
  49.     web_page = get_page(URL)
  50.     web_page = str(web_page).lower()
  51.     isFound = web_page.find(SEARCH_TERM.format(prev));
  52.  
  53.     #If you cant find your search string, that indicates
  54.     #Sean has got more nendroids.
  55.     if isFound == -1:
  56.         #Find out how many more...
  57.         start = web_page.find("owned (")
  58.         fin = web_page.find(")", start)
  59.         new = int(web_page[start + len("owned ("):fin])
  60.  
  61.         #Write the new figure figure
  62.         file = open(FILENAME, 'w')
  63.         file.write(str(new))
  64.         file.close()
  65.  
  66.         #Alert Sam
  67.         print("The number owned has changed, sending email...")
  68.         send_email(new, prev)
  69.         print("email sent :)")
  70.     else:
  71.         print("Sean hasn't got any more nendroids.")
  72.  
  73.  
  74. if __name__ == "__main__":
  75.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement