Last active
August 29, 2015 14:13
-
-
Save bradgignac/37f880300a013e3d99b3 to your computer and use it in GitHub Desktop.
Find and Destroy Mailing List Recipients
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
# Usage: | |
# python recipientsearch.py find query --key MYMAILGUNKEY | |
# python recipientsearch.py unsubscribe query --key MYMAILGUNKEY | |
# python recipientsearch.py destroy query --key MYMAILGUNKEY | |
import argparse | |
import requests | |
import urllib | |
FIND = 'find' | |
UNSUBSCRIBE = 'unsubscribe' | |
DESTROY = 'destroy' | |
def check_command(command): | |
if command not in [FIND, UNSUBSCRIBE, DESTROY]: | |
raise argparse.ArgumentTypeError('Command must be find, unsubscribe, or destroy') | |
return command | |
def get(url, key): | |
try: | |
auth = ('api', key) | |
res = requests.get(url, auth=auth) | |
body = res.json() | |
return body['items'] | |
except: | |
return [] | |
def get_lists(key): | |
return get('https://api.mailgun.net/v2/lists', key) | |
def get_members(l, key): | |
address = urllib.quote_plus(l['address'], '') | |
url = 'https://api.mailgun.net/v2/lists/{0}/members'.format(address) | |
return get(url, key) | |
def find_matching_recipients(query, key): | |
results = [] | |
for l in get_lists(key): | |
for m in get_members(l, key): | |
if query in m['address']: | |
results.append((l, m)) | |
return results | |
def unsubscribe_member(l, member, key): | |
list_address = urllib.quote_plus(l['address'], '') | |
member_address = urllib.quote_plus(member['address'], '') | |
print "Unsubscribing {address} ({list})".format(address=member['address'], list=l['address']) | |
url = 'https://api.mailgun.net/v2/lists/{0}/members/{1}'.format(list_address, member_address) | |
requests.put(url, auth=('api', key), data={'subscribed': False}) | |
def delete_member(l, member, key): | |
list_address = urllib.quote_plus(l['address'], '') | |
member_address = urllib.quote_plus(member['address'], '') | |
print "Deleting {address} ({list})".format(address=member['address'], list=l['address']) | |
url = 'https://api.mailgun.net/v2/lists/{0}/members/{1}'.format(list_address, member_address) | |
requests.delete(url, auth=('api', key)) | |
def find(query, key): | |
for l, m in find_matching_recipients(query, key): | |
print "{address} ({list})".format(address=m['address'], list=l['address']) | |
def unsubscribe(query, key): | |
for l, m in find_matching_recipients(query, key): | |
unsubscribe_member(l, m, key) | |
def destroy(query, key): | |
for l, m in find_matching_recipients(query, key): | |
delete_member(l, m, key) | |
parser = argparse.ArgumentParser(description='Find and destroy mailing list recipients.') | |
parser.add_argument('command', type=check_command, help='Command to run, either find or destroy') | |
parser.add_argument('query', type=str, help='Recipient to search for') | |
parser.add_argument('--key', type=str, help='Mailgun API key') | |
args = parser.parse_args() | |
if args.command == FIND: | |
find(args.query, args.key) | |
elif args.command == UNSUBSCRIBE: | |
unsubscribe(args.query, args.key) | |
elif args.command == DESTROY: | |
destroy(args.query, args.key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment