Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python
- import re
- import urllib
- import os
- import pickle
- import smtplib
- import os.path
- from email.mime.text import MIMEText
- os.system('cls' if os.name == 'nt' else 'clear')
- mypath = str(os.path.dirname(os.path.realpath(__file__)))
- def main():
- print str(mypath)
- deactivateScript = False
- if deactivateScript == True:
- print "This script is currently deactivated"
- return
- mailDict = {"Trondheim": ["bard.bjorsvik@gmail.com","holst.tania@gmail.com"]}
- for origin, recipients in mailDict.iteritems():
- functionName(origin, recipients)
- def functionName(origin, recipients):
- notifiedcars = readFromFile(origin)
- rawtext = checkWeb(origin)
- if rawtext != []:
- newCars = makeList(notifiedcars,rawtext,origin)
- if newCars != []:
- mail = makeMail(newCars,origin)
- if mail != "":
- sendMail(mail,recipients)
- def readFromFile(origin):
- try:
- filname = mypath+"/"+"carlist"+origin+".txt"
- infile = open(filname, "rb")
- notifiedcars = pickle.load(infile)
- infile.close()
- except:
- notifiedcars = []
- return notifiedcars
- def writeToFile(notifiedcars, origin):
- filname = mypath+"/"+"carlist"+origin+".txt"
- outfile = open(filname,"wb")
- pickle.dump(notifiedcars, outfile)
- outfile.close()
- def checkWeb(origin):
- socket = urllib.urlopen("http://www.transfercar4u.no/freecar.asp?origin="+origin)
- htmlsource = socket.read()
- socket.close()
- result = re.findall('<table border=0 cellspacing=0 cellpadding=0 width=689>((?:.|\\n)*?)</table>', htmlsource)
- return result
- def makeList(notifiedcars, rawtext, personOrigin):
- newCars = []
- temptext = str(rawtext[0])
- cars = []
- temptext2 = temptext.split("</tr")
- for i in temptext2:
- if len(i) > 200:
- cars.append(i)
- for line in cars:
- temp = line.split("<td>")
- if not "<i>" in temp[2]:
- refnr = temp[2][0:temp[2].find("</td>")]
- date = temp[3][0:temp[3].find("</td>")]
- origin = temp[4][0:temp[4].find("</td>")]
- end = temp[5][0:temp[5].find("</td>")]
- else:
- refnr = temp[2][3:temp[2].find("</i>")]
- date = temp[3][3:temp[3].find("</i>")]
- origin = temp[4][3:temp[4].find("</i>")]
- end = temp[5][3:temp[5].find("</i>")]
- car = [refnr, date, origin, end]
- if car not in notifiedcars:
- notifiedcars.append(car)
- newCars.append(car)
- writeToFile(notifiedcars,personOrigin)
- return newCars
- def makeMail(carList, origin):
- numberofnewcars = 0
- msg = ""
- for car in carList:
- numberofnewcars += 1
- msg = msg + "\nLink:\thttp://www.transfercar4u.no/freecar.asp?detail=hide&c_id="+car[0]+"\nDato:\t"+car[1]+"\nFra:\t"+car[2]+"\nTil:\t"+car[3]+"\n"
- if numberofnewcars == 1:
- subject = 'There are ' + str(numberofnewcars) + ' new car from ' + origin
- else:
- subject = 'There are ' + str(numberofnewcars) + ' new cars from ' + origin
- mail = [numberofnewcars, subject, msg]
- return mail
- def sendMail(mail,recipients):
- sender = "Bard Returbil <returbilsender@gmail.com>"
- password = "bardreturbilpython2015"
- msg = MIMEText(mail[2])
- msg['Subject'] = mail[1]
- msg['From'] = sender
- msg['To'] = ', '.join(recipients)
- try:
- smtpObj = smtplib.SMTP_SSL('smtp.gmail.com', '465')
- smtpObj.login("returbilsender@gmail.com", password)
- smtpObj.sendmail(sender, recipients, msg.as_string())
- smtpObj.close()
- print "New car list sendt to " + ', '.join(recipients)
- except smtplib.SMTPAuthenticationError:
- print "SMTP Authentication error: wrong username and/or password"
- except smtplib.SMTPRecipientsRefused:
- print "Recipients refused: " + ', '.join(recipients)
- except smtplib.SMTPSenderRefused:
- print "Sender refused: " + sender
- except smtplib.SMTPException:
- print "SMTP Error: unable to send email"
- def printList(notifiedcars):
- for i in notifiedcars:
- print i
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement