michaelminter
5/31/2016 - 8:14 PM

mobile_list.py

#!/usr/bin/env python2
import athenahealthapi
import datetime
import csv

# Set up
key = 'FILTERED'
secret = 'FILTERED'
version = 'FILTERED'
practice_id = 123
department_id = 1
appointment_type_id = 1

# Don't touch anything below this line
def format_date(date, format = '%m/%d/%Y'):
	return date.strftime(format)

patients = []
processed = 0
today = datetime.date.today()
tomorrow = datetime.date.today() + datetime.timedelta(days = 1)
nextyear = today.replace(year = today.year + 1)
filename = 'mobile_list_%s.csv' % format_date(today, '%Y%m%d')

api = athenahealthapi.APIConnection(version, key, secret, practice_id)

# If you want to change which practice you're working with after initialization, this is how.
# api.practiceid = 000000

# Get appointments
appointments = api.GET('/appointments/booked', {
	'departmentid': department_id,
	'startdate': format_date(today),
	'enddate': format_date(nextyear),
    'scheduledstartdate': format_date(today),
    'scheduledenddate': format_date(tomorrow)
})
print 'Found %s appointments between %s and %s...' % (appointments['totalcount'], today, nextyear)

# Get patients
for appointment in appointments['appointments']:
  patient = api.GET('/patients/'+appointment['patientid'])[0]
  if 'email' in patient:
    if 'mobilephone' in patient or 'homephone' in patient:
      processed = processed + 1
      patients.append([appointment['appointmentid'], appointment['scheduleddatetime'], patient['mobilephone'] if patient.has_key('mobilephone') else patient['homephone']])
print 'Processing %s patients with available phone number...' % processed

# Will overwrite an existing file of the same name
with open(filename, 'wb') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    for p in patients:
        spamwriter.writerow(p)
        print ' * %s' % p

print 'Saved %s.' % filename