Created
September 21, 2013 00:19
-
-
Save micahlmartin/6645622 to your computer and use it in GitHub Desktop.
Send money on Venmo via CSV import
This file contains 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
#!/usr/bin/env python | |
import sys | |
import os | |
import getopt | |
from decimal import Decimal | |
import csv | |
import locale | |
from subprocess import call | |
# Set the locale for currency formatting | |
locale.setlocale(locale.LC_ALL, '') | |
# Color formatting for console output | |
HEADER = '\033[95m' | |
OKBLUE = '\033[94m' | |
OKGREEN = '\033[92m' | |
WARNING = '\033[93m' | |
FAIL = '\033[91m' | |
ENDC = '\033[0m' | |
CHECK_MARK = u'\u2713' | |
helpText = """Usage: | |
./mass_sender -i list.txt -a 1 -n "Try Venmo!" --token Xyz12345 | |
-f <file> File containing newline separated phone numbers | |
-a <amount> Amount of money to send | |
-n <note> A note to include in the payment | |
--token <access_token> The access token for the sending user | |
""" | |
API_URL = "https://api.venmo.com" | |
def main(argv): | |
file, amount, note, access_token = parse_commandline_args(argv) | |
execute(file, amount, note, access_token) | |
def parse_commandline_args(argv): | |
file = None | |
amount = None | |
note = None | |
access_token = None | |
try: | |
opts, args = getopt.getopt(argv, "hf:o:a:n:t:", ["ifile=", "ofile=", "file=", "amount=", "note=", "token="]) | |
except getopt.GetoptError: | |
print helpText | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt == '-h': | |
print helpText | |
sys.exit() | |
elif opt in ("-f", "--file"): | |
file = arg | |
elif opt in ("-a", "--amount"): | |
amount = arg | |
elif opt in ("-n", "--note"): | |
note = arg | |
elif opt in ("-t", "--token"): | |
access_token = arg | |
if not file: | |
print_help_text_with_message("Couldn't find the input file") | |
if not amount: | |
print_help_text_with_message("Amount is required") | |
try: | |
amount = Decimal(amount) | |
except Exception: | |
print_help_text_with_message("Amount is invalid") | |
if not note: | |
print_help_text_with_message("Note is required") | |
if not access_token: | |
print_help_text_with_message("A users access token is required") | |
return (file, amount, note, access_token) | |
def print_help_text_with_message(message): | |
print "%s%s%s\n%s" % (FAIL, message, ENDC, helpText) | |
sys.exit(2) | |
def quit_with_error(message): | |
print "%s%s%s" % (FAIL, message, ENDC) | |
sys.exit(2) | |
def quit_with_warning(message): | |
print "%s%s%s" % (WARNING, message, ENDC) | |
sys.exit(2) | |
def get_phone_numbers(file): | |
""" | |
Reads the phone numbers from the first column of the csv file | |
""" | |
numbers = [] | |
with open(file, 'rb') as csvfile: | |
reader = csv.reader(csvfile, delimiter=',', quotechar='|') | |
try: | |
reader.next() # Skip header | |
for row in reader: | |
phone = row[0] | |
numbers.append(phone) | |
except csv.Error as e: | |
sys.exit('file %s, line %d: %s' % (file, reader.line_num, e)) | |
if len(numbers) == 0: | |
quit_with_warning("No phone numbers found. Exiting") | |
return numbers | |
def send_money(phone_numbers, amount, note, access_token): | |
for phone in phone_numbers: | |
# import ipdb; ipdb.set_trace | |
print "%s%s%s%s" % (OKGREEN, CHECK_MARK, ENDC, phone) | |
cmd = "curl %s/payments -d access_token=%s -d amount=%s -d phone=%s -d note=\"%s\"" % (API_URL, access_token, amount, phone, note) | |
os.system(cmd) | |
def execute(file, amount, note, access_token): | |
phone_numbers = get_phone_numbers(file) | |
print "You are about to send %s to %s phone numbers." % (locale.currency(amount), len(phone_numbers)) | |
prompt = raw_input("Type 'YES' to continue: ") | |
if prompt != "YES": | |
print "%sQuitting...%s" % (WARNING, ENDC) | |
sys.exit(2) | |
print "%sSending Money%s" % (OKGREEN, ENDC) | |
send_money(phone_numbers, amount, note, access_token) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment