Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. import os
  2. from datetime import datetime
  3. from urllib.request import urlopen
  4. import boto3
  5. import re
  6.  
  7. CLAIM_PAGE = os.environ['claim_page']
  8. SNS_TOPIC = os.environ['topic_arn']
  9. EMAIL = os.environ['email']
  10.  
  11.  
  12. def check_daily_book():
  13. '''Open the claim page for the daily free eBook and extract
  14. the title of the book. Then it by SNS to my email.
  15. '''
  16. with urlopen(CLAIM_PAGE) as stream:
  17. html = stream.read().decode("utf-8")
  18. print('Length of html: %d' % len(html))
  19. r = re.search(r'<div class="dotd-title">\s*?<h2>\s*(.+?)\s*</h2>\s*?</div>', html)
  20. if r:
  21. title = r.group(1).strip()
  22. print(title)
  23.  
  24. sns = boto3.client('sns')
  25. subject = "Packt Daily: " + (title[:57] + '...' if len(title) > 60 else title)
  26. msg = ""
  27. msg += "Packt daily free eBook is '%s'" % title
  28. msg += "\r\n\r\n"
  29. msg += "Check it out at " + CLAIM_PAGE
  30.  
  31. sns.publish(
  32. TopicArn=SNS_TOPIC,
  33. Message=msg,
  34. Subject=subject
  35. )
  36. return title
  37. return None
  38.  
  39. def lambda_handler(event, context):
  40. print('Checking {} at {}...'.format(CLAIM_PAGE, event['time']))
  41. try:
  42. check_daily_book()
  43. except:
  44. print('Check failed!')
  45. raise
  46. else:
  47. print('Check passed!')
  48. return event['time']
  49. finally:
  50. print('Check complete at {}'.format(str(datetime.now())))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement