Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import choice
- import re, urllib, os, pickle, smtplib
- from email.mime.text import MIMEText
- os.system('cls' if os.name == 'nt' else 'clear')
- def main():
- notifiedcars = readFromFile()
- makeList(notifiedcars)
- printList(notifiedcars)
- def readFromFile():
- try:
- infile = open("carlist.txt", "r")
- notifiedcars = pickle.load(infile)
- infile.close()
- except:
- notifiedcars = []
- return notifiedcars
- def writeToFile(notifiedcars):
- outfile = open("carlist.txt","w")
- pickle.dump(notifiedcars, outfile)
- outfile.close()
- def makeList(notifiedcars):
- while True:
- socket = urllib.urlopen("http://www.transfercar4u.no/freecar.asp?origin=Bergen")
- htmlsource = socket.read()
- socket.close()
- result = re.findall('<table border=0 cellspacing=0 cellpadding=0 width=689>((?:.|\\n)*?)</table>', htmlsource)
- rawtext = str(result[0])
- cars = []
- msg = ""
- numberofnewcars = 0
- print result
- temptext = rawtext.split("</tr")
- for i in temptext:
- 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)
- 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 msg != "":
- sendMail(msg, numberofnewcars)
- print msg
- writeToFile(notifiedcars)
- if result != []:
- break
- def sendMail(newCar, numberofnewcars):
- #Her har jeg laget en ny gmail fordi jeg må logge inn med brukernavn og passord og jeg vil ikke oppgi mitt skikkelige passord
- sender = "Bard Returbil <"min nye gmailkonto for å sende"@gmail.com>"
- password = "passordet til min nye gmailkonto(i stringformat)"
- receiver = "bard.bjorsvik@gmail.com"
- msg = MIMEText(newCar)
- msg['Subject'] = 'There are ' + str(numberofnewcars) + ' new car(s) from Bergen'
- msg['From'] = sender
- msg['To'] = receiver
- try:
- smtpObj = smtplib.SMTP_SSL('smtp.gmail.com', '465')
- smtpObj.login("returbilsender@gmail.com", password)
- smtpObj.sendmail(sender, [receiver], msg.as_string())
- smtpObj.close()
- print "New car list sendt to " + receiver
- except smtplib.SMTPAuthenticationError:
- print "SMTP Authentication error: wrong username and/or password"
- except smtplib.SMTPRecipientsRefused:
- print "Recipients refused: " + ', '.join(receiver)
- 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