Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.80 KB | None | 0 0
  1. #!/usr/local/bin/python
  2. import re
  3. import urllib
  4. import os
  5. import pickle
  6. import smtplib
  7. import os.path
  8. from email.mime.text import MIMEText  
  9.  
  10. os.system('cls' if os.name == 'nt' else 'clear')
  11.  
  12. mypath = str(os.path.dirname(os.path.realpath(__file__)))
  13.  
  14. def main():
  15.     print str(mypath)
  16.     deactivateScript = False
  17.    
  18.     if deactivateScript == True:
  19.         print "This script is currently deactivated"
  20.         return
  21.  
  22.     mailDict = {"Trondheim": ["bard.bjorsvik@gmail.com","holst.tania@gmail.com"]}
  23.  
  24.     for origin, recipients in mailDict.iteritems():
  25.         functionName(origin, recipients)
  26.  
  27.  
  28.  
  29. def functionName(origin, recipients):
  30.     notifiedcars = readFromFile(origin)
  31.    
  32.     rawtext = checkWeb(origin)
  33.    
  34.     if rawtext != []:
  35.         newCars = makeList(notifiedcars,rawtext,origin)
  36.  
  37.     if newCars != []:
  38.         mail = makeMail(newCars,origin)
  39.         if mail != "":
  40.             sendMail(mail,recipients)
  41.  
  42. def readFromFile(origin):
  43.     try:
  44.         filname = mypath+"/"+"carlist"+origin+".txt"
  45.         infile = open(filname, "rb")
  46.         notifiedcars = pickle.load(infile)
  47.         infile.close()
  48.     except:
  49.         notifiedcars = []
  50.  
  51.     return notifiedcars
  52.  
  53. def writeToFile(notifiedcars, origin):
  54.     filname = mypath+"/"+"carlist"+origin+".txt"
  55.     outfile = open(filname,"wb")
  56.     pickle.dump(notifiedcars, outfile)
  57.     outfile.close()
  58.  
  59. def checkWeb(origin):
  60.     socket = urllib.urlopen("http://www.transfercar4u.no/freecar.asp?origin="+origin)
  61.     htmlsource = socket.read()
  62.     socket.close()
  63.     result = re.findall('<table border=0 cellspacing=0 cellpadding=0 width=689>((?:.|\\n)*?)</table>', htmlsource)
  64.     return result
  65.  
  66. def makeList(notifiedcars, rawtext, personOrigin):
  67.     newCars = []
  68.     temptext = str(rawtext[0])
  69.     cars = []
  70.     temptext2 = temptext.split("</tr")
  71.     for i in temptext2:
  72.         if len(i) > 200:
  73.             cars.append(i)
  74.  
  75.     for line in cars:
  76.         temp = line.split("<td>")
  77.         if not "<i>" in temp[2]:
  78.  
  79.             refnr = temp[2][0:temp[2].find("</td>")]
  80.             date = temp[3][0:temp[3].find("</td>")]
  81.             origin = temp[4][0:temp[4].find("</td>")]
  82.             end = temp[5][0:temp[5].find("</td>")]
  83.  
  84.         else:
  85.  
  86.             refnr = temp[2][3:temp[2].find("</i>")]
  87.             date = temp[3][3:temp[3].find("</i>")]
  88.             origin = temp[4][3:temp[4].find("</i>")]
  89.             end = temp[5][3:temp[5].find("</i>")]
  90.  
  91.         car = [refnr, date, origin, end]
  92.            
  93.         if car not in notifiedcars:
  94.             notifiedcars.append(car)
  95.             newCars.append(car)
  96.  
  97.     writeToFile(notifiedcars,personOrigin)
  98.        
  99.     return newCars
  100.  
  101. def makeMail(carList, origin):
  102.     numberofnewcars = 0
  103.     msg = ""
  104.     for car in carList:
  105.         numberofnewcars += 1
  106.         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"
  107.    
  108.     if numberofnewcars == 1:
  109.         subject = 'There are ' + str(numberofnewcars) + ' new car from ' + origin
  110.     else:
  111.         subject = 'There are ' + str(numberofnewcars) + ' new cars from ' + origin
  112.     mail = [numberofnewcars, subject, msg]
  113.    
  114.     return mail
  115.    
  116.  
  117. def sendMail(mail,recipients):
  118.     sender  = "Bard Returbil <returbilsender@gmail.com>"
  119.     password = "bardreturbilpython2015"
  120.    
  121.     msg = MIMEText(mail[2])
  122.     msg['Subject'] = mail[1]
  123.     msg['From'] = sender
  124.     msg['To'] = ', '.join(recipients)
  125.        
  126.     try:
  127.         smtpObj = smtplib.SMTP_SSL('smtp.gmail.com', '465')
  128.         smtpObj.login("returbilsender@gmail.com", password)
  129.         smtpObj.sendmail(sender, recipients, msg.as_string())
  130.         smtpObj.close()
  131.         print "New car list sendt to " + ', '.join(recipients)
  132.     except smtplib.SMTPAuthenticationError:
  133.         print "SMTP Authentication error: wrong username and/or password"
  134.     except smtplib.SMTPRecipientsRefused:
  135.         print "Recipients refused: " + ', '.join(recipients)
  136.     except smtplib.SMTPSenderRefused:
  137.         print "Sender refused: " + sender
  138.     except smtplib.SMTPException:
  139.         print "SMTP Error: unable to send email"
  140.  
  141. def printList(notifiedcars):
  142.     for i in notifiedcars:
  143.         print i
  144.  
  145. if __name__ == "__main__":
  146.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement