Guest User

Untitled

a guest
May 28th, 2015
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. import time
  2. import praw
  3. import os
  4. import smtplib
  5. import email
  6. import datetime, time
  7.  
  8. from email.mime.multipart import MIMEMultipart
  9. from email.mime.text import MIMEText
  10. from email import encoders
  11. from email.header import Header
  12.  
  13.  
  14. #email sender/reciever
  15. sender = Header('','utf-8').encode()
  16. recievers = ['']
  17. #Reddit login info
  18. login = ''
  19. password =''
  20.  
  21.  
  22.  
  23. def getsub():
  24.     #Setting up PRAW to use
  25.     #Logging into reddit
  26.     r = praw.Reddit(user_agent='getting top 25 of leagueoflegends by /u/legendzs')
  27.     r.config.decode_html_entities = True
  28.     r.login(login, password)
  29.  
  30.     #Asks user which subreddit they wish to see and how many hot posts
  31.     sub = input('Which subreddit would you list to see?: ')
  32.     topHowMuch= input("How many of Hot posts of /r/{} would you like to see?: ".format(sub))
  33.     submissions = r.get_subreddit(sub).get_hot(limit=int(topHowMuch))
  34.  
  35.     #return the values of submissions and  sub to be used else where
  36.     return submissions, sub, topHowMuch
  37.  
  38.  
  39. ###Send email to different emails
  40. def chooseEmail():
  41.     pass
  42.  
  43.  
  44. def gettime():
  45.     #gets the time/date of when I send email
  46.    
  47.     timeee=time.time()
  48.     timeee =datetime.datetime.fromtimestamp(timeee).strftime('%m-%d-%Y %I:%M %p')
  49.     timeee = timeee[:10] + " at" +timeee[10:]
  50.     return timeee
  51.  
  52. def getredditurl():
  53.     #Gets redditurl, specific thread id
  54.    
  55.     #unpacking the variables/values from getsub()
  56.     submissions,sub, topHowMuch=getsub()
  57.    
  58.     link=''
  59.     for top10format in submissions:
  60.        
  61.         link += ("""
  62. <li>
  63.    <a href="www.reddit.com/r/leagueoflegends/{0}/">
  64.                {0}
  65.    </a>
  66. </li>
  67. """).format(top10format.id)
  68.     return link, sub,topHowMuch
  69.  
  70. def htmlmessage():
  71.     #Extracting variables that I need
  72.     timeee= gettime()
  73.     link, sub, topHowMuch=getredditurl()
  74.     ###### use a variable that formats the <li> <a href> links in
  75.     html ="""
  76.    <html>
  77.    <body>
  78.    <h2>The Top {} hottest posts on /r/{} on {}</h2>
  79.    <br>
  80.    <ol>
  81.    {}
  82.    </ol>
  83.    </body>
  84.    </html>
  85.    """.format(topHowMuch, sub, timeee, link)
  86.  
  87.     return html
  88.  
  89. def completeEmail():
  90.     html=htmlmessage()
  91.  
  92.     #loging into email
  93.     username = ""
  94.     password1 = ""
  95.     email = smtplib.SMTP('smtp.gmail.com:587')
  96.     email.starttls()
  97.     email.login(username, password1)
  98.  
  99.     msg = MIMEMultipart('alternative')
  100.     msg['To']= recievers
  101.     msg['From'] = sender
  102.     msg['Subject']= "Reddit posts"
  103.  
  104.     part1 = MIMEText(html, 'html')
  105.  
  106.     msg.attach(part1)
  107.  
  108.     return msg, email
  109.  
  110.    
  111.  
  112. def  emailsend():
  113.  
  114.     msg, email=completeEmail()
  115.    
  116.     try:
  117.            
  118.             email.sendmail(sender,recievers,msg)
  119.            
  120.            
  121.     except:
  122.             print("Didn't send - unknown error")
  123.     finally:
  124.             email.quit()        
  125.  
  126.  
  127. emailsend()
Add Comment
Please, Sign In to add comment