Advertisement
Guest User

Untitled

a guest
Jul 12th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. from random import choice
  2. import re, urllib, os, pickle, smtplib
  3. from email.mime.text import MIMEText  
  4.  
  5. os.system('cls' if os.name == 'nt' else 'clear')
  6.  
  7. def main():
  8.     notifiedcars = readFromFile()
  9.     makeList(notifiedcars)
  10.     printList(notifiedcars)
  11.  
  12.  
  13. def readFromFile():
  14.     try:
  15.         infile = open("carlist.txt", "r")
  16.         notifiedcars = pickle.load(infile)
  17.         infile.close()
  18.     except:
  19.         notifiedcars = []
  20.  
  21.     return notifiedcars
  22.  
  23. def writeToFile(notifiedcars):
  24.     outfile = open("carlist.txt","w")
  25.     pickle.dump(notifiedcars, outfile)
  26.     outfile.close()
  27.  
  28. def makeList(notifiedcars):
  29.     while True:
  30.         socket = urllib.urlopen("http://www.transfercar4u.no/freecar.asp?origin=Bergen")
  31.         htmlsource = socket.read()
  32.         socket.close()
  33.         result = re.findall('<table border=0 cellspacing=0 cellpadding=0 width=689>((?:.|\\n)*?)</table>', htmlsource)
  34.        
  35.         rawtext = str(result[0])
  36.         cars = []
  37.         msg = ""
  38.         numberofnewcars = 0
  39.         print result
  40.         temptext = rawtext.split("</tr")
  41.         for i in temptext:
  42.             if len(i) > 200:
  43.                 cars.append(i)
  44.  
  45.         for line in cars:
  46.             temp = line.split("<td>")
  47.             if not "<i>" in temp[2]:
  48.  
  49.                 refnr = temp[2][0:temp[2].find("</td>")]
  50.                 date = temp[3][0:temp[3].find("</td>")]
  51.                 origin = temp[4][0:temp[4].find("</td>")]
  52.                 end = temp[5][0:temp[5].find("</td>")]
  53.  
  54.             else:
  55.  
  56.                 refnr = temp[2][3:temp[2].find("</i>")]
  57.                 date = temp[3][3:temp[3].find("</i>")]
  58.                 origin = temp[4][3:temp[4].find("</i>")]
  59.                 end = temp[5][3:temp[5].find("</i>")]
  60.  
  61.             car = [refnr, date, origin, end]
  62.  
  63.             if car not in notifiedcars:
  64.                 notifiedcars.append(car)
  65.                 numberofnewcars += 1
  66.                 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"
  67.  
  68.         if msg != "":
  69.             sendMail(msg, numberofnewcars)
  70.             print msg
  71.  
  72.         writeToFile(notifiedcars)
  73.        
  74.         if result != []:
  75.             break
  76.  
  77. def sendMail(newCar, numberofnewcars):
  78.     #Her har jeg laget en ny gmail fordi jeg må logge inn med brukernavn og passord og jeg vil ikke oppgi mitt skikkelige passord
  79.     sender  = "Bard Returbil <"min nye gmailkonto for å sende"@gmail.com>"
  80.     password = "passordet til min nye gmailkonto(i stringformat)"
  81.     receiver = "bard.bjorsvik@gmail.com"
  82.    
  83.     msg = MIMEText(newCar)
  84.     msg['Subject'] = 'There are ' + str(numberofnewcars) + ' new car(s) from Bergen'
  85.     msg['From'] = sender
  86.     msg['To'] = receiver
  87.    
  88.     try:
  89.         smtpObj = smtplib.SMTP_SSL('smtp.gmail.com', '465')
  90.         smtpObj.login("returbilsender@gmail.com", password)
  91.         smtpObj.sendmail(sender, [receiver], msg.as_string())
  92.         smtpObj.close()
  93.         print "New car list sendt to " + receiver
  94.     except smtplib.SMTPAuthenticationError:
  95.         print "SMTP Authentication error: wrong username and/or password"
  96.     except smtplib.SMTPRecipientsRefused:
  97.         print "Recipients refused: " + ', '.join(receiver)
  98.     except smtplib.SMTPSenderRefused:
  99.         print "Sender refused: " + sender
  100.     except smtplib.SMTPException:
  101.         print "SMTP Error: unable to send email"
  102.  
  103. def printList(notifiedcars):
  104.     for i in notifiedcars:
  105.         print i
  106.  
  107. if __name__ == "__main__":
  108.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement