import urllib2 import os import time import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText from BeautifulSoup import BeautifulSoup class userClass: def __init__(self): self.userName = '' self.userEmail = '' self.sendEmail = 'thatotherguy23@gmail.com' self.sendPwd = 'oyster23' self.mailServer = smtplib.SMTP("smtp.gmail.com", 587) self.newContent = [] def dispAllEpis(self): # Get URL showName = raw_input("Enter Name of Series: ") url = "http://epguides.com/" + self.nameProc(showName) + '/' try: episList = self.getEpisodes(url) print '\n' for i in range(0, len(episList)): print episList[i] print "\n" except: print("\nInvalid Show Title\n\n") def getEpisodes(self, url): end1 = 0 begin1 = 0 foundStr = [] corrStr = [] j = 0 # Get Soup source = urllib2.urlopen(url) soup = BeautifulSoup(source) titles = soup.findAll('a') for i in titles: foundStr.append(i.contents[0].string) while end1 != 1: if foundStr[j] == "Back to": end1 = 1 if begin1 == 1 and end1 != 1: if foundStr[j] != "Recap" and foundStr[j] != "Trailer" and foundStr[j][0] != '&': corrStr.append(foundStr[j]) if foundStr[j] == "Menus": begin1 = 1 j += 1 return corrStr def logIn(self): self.userName = raw_input("Please enter user name: ") tempPath = os.path.dirname(__file__) + '/' + self.userName if not os.path.isdir(tempPath): print "Welcome, " + self.userName + '!' print "Your file will be created when you save shows." self.userEmail = '' else: readUserFile = open(tempPath + '/' + self.userName + '_data', 'r') self.userEmail = readUserFile.read() print "Welcome, " + self.userName + '!' print "Your settings have been loaded." print '\n' def saveShows(self): if self.userName == '': print "Welcome, guest!" print "Please log in before trying to save shows.\n" else: print "Welcome, " + self.userName + "!" showName = raw_input("Please enter the series name: ") filePath = self.filePathBuild(showName) url = "http://epguides.com/" + self.nameProc(showName) + '/' try: episList = self.getEpisodes(url) saveFile = open(filePath, 'w') for i in range(0, len(episList)): saveFile.write(episList[i] + "\n") print("\n\n" + showName + " has been saved to your saved shows. \n") except: print("\nInvalid Show Title\n\n") def checkEpis(self): j = 0 self.newContent = [] if self.userName == '': print "Welcome, guest!" print "Please log in before trying to save shows.\n" else: print("Please wait while your episode lists are checked, " + self.userName + '.\n') tempPath = os.path.dirname(__file__) + '/' + self.userName # Checks if folder exists if os.path.isdir(tempPath): # fileList: List of all files in user's folder fileList = os.listdir(tempPath) for i in range(0, len(fileList)): newContent = 0 tempUrl = "http://epguides.com/" + fileList[i] + '/' tempList = self.getEpisodes(tempUrl) fullTempPath = tempPath + '/' + fileList[i] tempOldList = self.rebuildList(fullTempPath) # listSizeAdjust = len(tempList) - len(tempOldList) # for k in range(0, listSizeAdjust): # tempOldList.append(' ') while not newContent and j < len(tempOldList): if tempOldList[j] != tempList[j]: newContent = 1 j += 1 if newContent: print fileList[i] + " has new content!\n" newContent.append(fileList[i]) else: print fileList[i] + " has no new content.\n" time.sleep(3) print ' ' else: print("Sorry, " + self.userName + ", your folder cannot be found.\n") def setAuto(self): self.userEmail = raw_input("Please enter email for alerts: ") userFileSave = open(os.path.dirname(__file__) + '/' + self.userName + '/' + self.userName + "_data", 'w') userFileSave.write(self.userEmail) userFileSave.close() def startAuto(self): self.checkEpis() if len(self.newContent) > 0: print("New Content Found.") for i in range(0, len(self.newContent)): # Send email self.sendEmail() def sendEmailAlert(self): msg = MIMEMultipart() msg['From'] = gmail_user msg['To'] = to msg['Subject'] = "subject" msg.attach(MIMEText(text)) mailServer = smtplib.SMTP("smtp.gmail.com", 587) mailServer.starttls() mailServer.login(gmail_user, gmail_pwd) mailServer.sendmail(gmail_user, to, msg.as_string()) mailServer.quit() def rebuildList(self, path): readFile = open(path, 'r') tempList = readFile.read() newList = [] tempString = '' for i in range(0, len(tempList)): if tempList[i] != '\n': tempString += tempList[i] else: newList.append(tempString) tempString = '' readFile.close() return newList def filePathBuild(self, showN): tempPath = os.path.dirname(__file__) + '/' + self.userName if not os.path.isdir(tempPath): # Create User's Directory os.mkdir(tempPath) # Create User Data File userFileSave = open(tempPath + '/' + self.userName + "_data", 'w') userFileSave.write(self.userEmail) userFileSave.close() path = os.path.join(os.path.dirname(__file__), self.userName + '/' + self.nameProc(showN)) return path def nameProc(self, nameStr): newName = '' for i in range (0, len(nameStr)): if nameStr[i] != ' ': newName += nameStr[i].lower() return newName
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question