Skip to content

Instantly share code, notes, and snippets.

@benhannel
Created November 28, 2020 17:46
Show Gist options
  • Save benhannel/32adb299741812909b0924efa0bb694b to your computer and use it in GitHub Desktop.
Save benhannel/32adb299741812909b0924efa0bb694b to your computer and use it in GitHub Desktop.
Red cross blood donation search
import requests
import json
import requests_cache
import datetime
requests_cache.install_cache('blood_cache')
def get_drives():
headers = {
'content-type': 'application/json'
}
data = """
{
"city": null,
"donationTypes": [
"BLOOD",
"PLATELETS"
],
"order": "DATE",
"range": 5,
"limit": 100,
"state": null,
"suggestIfBlank": null,
"zipCode": "94611",
"startDate": "12/09/2020",
"endDate": "12/27/2020"
}
"""
response = requests.post('https://www.redcrossblood.org/api/drive/v1/drives', headers=headers, data=data)
try:
p = json.loads(response.text)
if 'list' not in p:
print(json.dumps(p, indent=1))
return p['list']
except Exception as e:
print(e, response.status_code, response.headers, response.text)
def get_slots(driveId, date):
response = requests.get('https://www.redcrossblood.org/api/drive/v1/drives/{}/schedules'.format(driveId))
slots = json.loads(response.text)['list']
parsed_date = datetime.datetime.strptime(date, '%m/%d/%Y')
filtered = []
for slot in slots:
t = datetime.datetime.strptime(slot['time'], '%I:%M %p').time()
if (t > datetime.time(hour=17) or parsed_date.weekday() >= 5) \
and slot['donationType'] in ['WHOLE_BLOOD', 'APHERESIS']:
filtered.append(slot)
return filtered
drives = get_drives()
for drive in drives:
slots = get_slots(drive['driveId'], drive['startDate'])
if len(slots) > 0:
# https://www.redcrossblood.org/give.html/drive-results?ed=12%2F14%2F2020&range=5&sd=12%2F14%2F2020&zipSponsor=94611
print(drive['driveName'], drive['startDate'])
for slot in slots:
print(slot['time'], slot['donationType'])
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment