Created
February 15, 2013 19:38
-
-
Save bialecki/4962832 to your computer and use it in GitHub Desktop.
Reddit Daily Email
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import requests | |
| API_KEY = 'KLAVIYO_API_KEY' | |
| TEMPLATE_ID = 'KLAVIYO_TEMPLATE_ID' | |
| SUBREDDIT = 'corgi' | |
| FROM_EMAIL = '[email protected]' | |
| FROM_NAME = 'Your Name' | |
| TO_EMAIL = '[email protected]' | |
| SUBJECT = 'The Daily Corgi' | |
| def run(): | |
| response = requests.get('http://www.reddit.com/r/%s/hot.json' % SUBREDDIT) | |
| posts = [] | |
| i = 0 | |
| for item in response.json()['data']['children']: | |
| image_url = item['data']['url'] | |
| if 'imgur' not in image_url: | |
| continue | |
| # Make sure we have an image and it's the appropriate size. | |
| path, file_name = image_url.rsplit('/', 1) | |
| if '.' not in file_name: | |
| image_url += 'l.jpg' | |
| else: | |
| image_url = path + '/' + 'l.'.join(file_name.split('.')) | |
| posts.append({ | |
| 'url' : 'http://www.reddit.com%s' % item['data']['permalink'], | |
| 'title' : item['data']['title'], | |
| 'image_url' : image_url, | |
| }) | |
| i += 1 | |
| if i > 4: break | |
| context = { | |
| 'posts' : posts, | |
| } | |
| requests.post('https://a.klaviyo.com/api/v1/email-template/%s/send' % TEMPLATE_ID, params={ | |
| 'api_key' : API_KEY, | |
| }, data={ | |
| 'service' : 'sendgrid', | |
| 'from_email' : FROM_EMAIL, | |
| 'from_name' : FROM_NAME, | |
| 'subject' : SUBJECT, | |
| 'to' : TO_EMAIL, | |
| 'context' : json.dumps(context), | |
| }, headers={ 'Accept': 'application/json', }) | |
| if __name__ == '__main__': | |
| run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment