DeaD_EyE

PacktPubClaimNotoficationWithDownload

May 1st, 2017
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. #!/home/packt/env/bin/python
  2.  
  3. import emails # https://pypi.python.org/pypi/emails
  4. from emails.template import JinjaTemplate as T
  5. import freepacktbook # https://pypi.python.org/pypi/freepacktbook/1.0.2
  6. import requests # https://pypi.python.org/pypi/requests/2.13.0
  7.  
  8.  
  9. mail_from = 'your_address@domain.tld'
  10. mail_to   = 'receiver_address@domain.tld'
  11. mail_host = 'smpthost'
  12. mail_port = 587
  13. mail_user = 'userlogin'
  14. mail_password = 'userpassword'
  15. mail_tls = True
  16. mail_ssl = False
  17. packt_credentials = {'email': 'YourEMail', 'password': 'PASSWORD'}
  18.  
  19. smtp = {'ssl': mail_ssl, 'tls': mail_tls, 'host': mail_host, 'port': mail_port, 'user': mail_user, 'password': mail_password}
  20. packt = freepacktbook.FreePacktBook(**packt_credentials)
  21. book = packt.claim_free_ebook()
  22. packt.book_formats = ['pdf']
  23. book_url = book['book_url']
  24. claim_url = book['claim_url']
  25. title = book.get('title')
  26. description = book['description']
  27. image_url = book.get('image_url')
  28. image_ext = image_url.split('.')[-1]
  29. image = requests.get(image_url).content
  30. pdf_url = packt.download_url % {'book_id': book['id'], 'format': 'pdf'}
  31. pdf = packt.session.get(pdf_url).content # load in memory
  32.  
  33. tpl = T('''<!DOCTYPE html>
  34.    <meta charset="utf-8">
  35.    <html>
  36.    <h1>{{ title  }}</h1>
  37.    <p>{{ description  }}</p>
  38.    <img src="cover.{{ image_ext  }}" alt="cover"><br />
  39.    <ul>
  40.    <li>Book URL: {{ book_url }}</li>
  41.    <li>Claim URL: {{ claim_url  }}</li>
  42.    <li>Download link: {{ pdf_url  }}</li>
  43.    </ul>
  44.    </html>''')
  45.  
  46. html = tpl.render(title=title,
  47.     description=description,
  48.     image_ext=image_ext,
  49.     book_url=book_url,
  50.     claim_url=claim_url,
  51.     pdf_url=pdf_url)
  52.  
  53. msg = emails.Message(mail_to=mail_to, subject='Free PacktPub Claim', html=html)
  54. msg.attach(data=image, filename='cover.{}'.format(image_ext), content_disposition='inline')
  55. msg.attach(data=pdf, filename='{}.{}'.format(title, 'pdf'))
  56. msg.transformer.synchronize_inline_images()
  57. msg.transformer.save()
  58. msg.send(mail_from=mail_from, smtp=smtp)
Add Comment
Please, Sign In to add comment