Created
May 8, 2017 17:47
-
-
Save rdegges/70745f636e2f48b5345ff2b122c94f9d to your computer and use it in GitHub Desktop.
Convert a Heroku application email into a Heroku owner 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
""" | |
heroku-email-lookup | |
~~~~~~~~~~~~~~~~~~~ | |
Given a text file that contains Heroku app email addresses (one on each | |
line), this program will spit out a list of app owner email addresses. | |
This is useful for Heroku addon providers (like me) who want to email their | |
addon customers. Because Heroku customers may change their account email | |
address from time-to-time, Heroku exposes a special API endpoint that this | |
script uses to retrieve the account holder's latest email address -- this | |
way you can actually email your Heroku addon customers. | |
To run this script, set the necessary global variables below, ensure you are | |
using Python 3+, and then run: | |
$ pip install requests | |
$ python lookup.py <file1.txt> [<file2.txt> <file3.txt> ...] | |
Written by Randall Degges ( | |
https://www.rdegges.com | |
[email protected] | |
@rdegges | |
) | |
""" | |
from sys import argv, exit | |
from requests import get | |
# Settings | |
HEROKU_USERNAME = 'your-addon-id' | |
HEROKU_PASSWORD = 'your-addon-manifest-password' | |
def get_app_emails(files): | |
""" | |
Given an array of filenames, iterate through these files, and return each | |
line (stripped) via a generator. | |
:param list files: A list of filenames. | |
""" | |
for file in files: | |
for app_email in open(file).readlines(): | |
yield app_email.strip() | |
def get_owner_email(app_email): | |
""" | |
Given an app_email, retrieve the owner email from Heroku (if available). | |
WARNING: This may quit the program if your credentials are invalid. Better | |
to fail immediately than error forever. | |
:param str app_email: The application email from Heroku. | |
:rtype: str or None | |
:returns: The owner email, or None. | |
""" | |
try: | |
resp = get('https://api.heroku.com/vendor/apps/{}'.format(app_email), auth=(HEROKU_USERNAME, HEROKU_PASSWORD)) | |
except KeyboardInterrupt as e: | |
print('WARNING: CTRL-C detected from user. Quitting immediately.') | |
exit(1) | |
except Exception as e: | |
print(e) | |
return None | |
if resp.status_code == 401: | |
print('ERROR: Your Heroku credentials are incorrect.') | |
print('Your username should be the name of your addon (eg: `heroku-redis`), and your password should be the password specified in your addon manifest.') | |
exit(1) | |
return resp.json()['owner']['email'] if resp.status_code == 200 else None | |
if __name__ == '__main__': | |
for app_email in get_app_emails(argv[1:]): | |
owner_email = get_owner_email(app_email) | |
if owner_email: | |
print(app_email, owner_email) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment