Skip to content

Instantly share code, notes, and snippets.

@dannguyen
Last active May 29, 2016 03:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannguyen/828a491f6aef459a30eaf3f130d80257 to your computer and use it in GitHub Desktop.
Save dannguyen/828a491f6aef459a30eaf3f130d80257 to your computer and use it in GitHub Desktop.
Download and aggregate by year the number of consumer product recalls initiated by the Consumer Product Safety Commission (Python 3.x)

The homepage for the CPSC API can be found at: http://www.cpsc.gov/en/Recalls/CPSC-Recalls-Application-Program-Interface-API-Information/

There's even a programmer's manual: http://www.cpsc.gov/Global/info/Recall/CPSC-Recalls-Retrieval-Web-Services-Programmers-Guide_3-25-2015.pdf

And here's a stashed version of the 2016 data (as of April):

http://stash.compjour.org/samples/cpsc/recalls201604.json

However, a bare call of the endpoint -- http://www.saferproducts.gov/RestWebServices/Recall?format=json -- with no parameters beyond specifying JSON as the format seems to yield the entire collection: ~6,700 records at around 20MB.

Here's a script that downloads the data file, stores it locally for later parsing, and then does a quick frequency count:

from collections import Counter
import requests
import json
SOURCE_URL = 'http://www.saferproducts.gov/RestWebServices/Recall'
FNAME = 'saferproducts-recalls.json'
print("Downloading from", SOURCE_URL, 'saving to', FNAME)
resp = requests.get(SOURCE_URL, params={'format': 'json'})
with open(FNAME, 'w') as wf:
    wf.write(resp.text)

data = resp.json()
print("Number of records:", len(data))
# frequency count by year
c = Counter(d['RecallDate'][0:4] for d in data)
for z in sorted(c.items()):
    print('%s: %s' % (z[0], str(z[1]).rjust(4)))

Output:

Downloading from http://www.saferproducts.gov/RestWebServices/Recall saving to saferproducts-recalls.json
Number of records: 6695
1973:    8
1974:   14
1975:   13
1976:   27
1977:   36
1978:   43
1979:   33
1980:   22
1981:   12
1982:   23
1983:   23
1984:   44
1985:   30
1986:   46
1987:   49
1988:   75
1989:   80
1990:   78
1991:   84
1992:   82
1993:   75
1994:  109
1995:  128
1996:  139
1997:  151
1998:  127
1999:  133
2000:  174
2001:  234
2002:  255
2003:  216
2004:  278
2005:  323
2006:  320
2007:  452
2008:  393
2009:  388
2010:  362
2011:  315
2012:  312
2013:  289
2014:  293
2015:  309
2016:   98
{
"ConsumerContact": null,
"Inconjunctions": [],
"ProductUPCs": [],
"Manufacturers": [
{
"CompanyID": "",
"Name": "F.H. Lawson"
},
{
"CompanyID": "",
"Name": "Tappan"
},
{
"CompanyID": "",
"Name": "Montgomery Ward"
}
],
"Hazards": [],
"Title": "Tappan Built-In Ovens Warning",
"RecallDate": "1973-06-08T00:00:00",
"Description": "Tappan Built-In Ovens Warning NEWS from CPSC U.S. Consumer Product Safety Commission Office of Information and Public Affairs Washington, DC 20207 FOR IMMEDIATE RELEASE June 8, 1973 \u00a0 Release # 73-003 Consumer Product Safety Commission Says Some Tappan Built-In Ovens May Pose Hazard To Consumers WASHINGTON, D.C. (June 8) - The Consumer Product Safety Commission Friday said it has learned from the Tappan Company of a defect in certain Tappan Company built-in gas ovens manufactured for installation primarily in mobile homes and other newly constructed homes. The Commission said the defect --a possible loose fitting connection-- could result in a hazard to consumers from gas leakage. The ovens in question were manufactured between January 23, 1973, and May 31, 1973, by the F.H. Lawson Company, Springfield, Tennessee, for the Tappan Company. While the majority of the ovens carry the \"Tappan\" name, some were produced for Montgomery Ward and carry the brand-name \"Signature.\" According to Tappan, the problem occurred in some 15,185 ovens, of which 11,974 have been shipped from the place of manufacture. The ovens carry model numbers AZ12-1239; AZ12-1249; AZ12-1441; AKTM-3850; and AKTM-3893. Some 8,172 of the ovens-- model number AZ12-1239-- are used exclusively in mobile homes. The Montgomery Ward ovens--\"Signature\"--carry the \"AKTM\" model number prefix. CPSC Chairman Richard 0. Simpson said the Commission is asking wholesalers, distributors, and retailers who may have the affected ovens in stock or installed in mobile homes or newly constructed homes immediately to contact the Tappan Company. In addition, Simpson said consumers who may have purchased a Tappan or \"Signature\" built-in oven with one of the model numbers in question should contact their local gas company or the nearest Tappan dealer regarding possible gas leakage between the shut-off valve and the pressure regulator. The Commission was told by Tappan that consumers can locate the model number on the oven by opening the top oven door and looking at the metal strip at the top of the oven which carries the model number on it. CPSC investigators have been sent to the Tappan headquarters, Mansfield, Ohio, and to the F.H. Lawson Company to determine whether the remedial action proposed by Tappan is appropriate and to determine the degree of potential hazard to consumers.",
"Images": [],
"Injuries": [],
"LastPublishDate": "2015-02-03T00:00:00",
"RecallNumber": "73003",
"URL": "http://www.cpsc.gov/en/Recalls/1973/Consumer-Product-Safety-Commission-Says-Some-Tappan-Built-In-Ovens-May-Pose-Hazard-To-Consumers",
"ManufacturerCountries": [],
"Products": [
{
"CategoryID": "2056",
"Model": "",
"Name": "",
"Type": "Ovens/Stoves/Ranges/Microwaves",
"Description": "",
"NumberOfUnits": ""
},
{
"CategoryID": "",
"Model": "",
"Name": "Montgomery Ward Signature built-in gas ovens",
"Type": "",
"Description": "",
"NumberOfUnits": ""
},
{
"CategoryID": "",
"Model": "",
"Name": "Tappan built-in gas ovens",
"Type": "",
"Description": "",
"NumberOfUnits": ""
}
],
"RecallID": 3722,
"Remedies": [],
"Retailers": []
}
{
"ConsumerContact": "YTL International toll-free at 888-723-6534 from 9 a.m. to 4:30 p.m. PT Monday through Friday, or online at www.ytlinternational.com and click on RECALL for more information.",
"Inconjunctions": [],
"ProductUPCs": [],
"Manufacturers": [],
"Hazards": [
{
"HazardTypeID": "67539",
"Name": "The drywall lifts can fail during use causing the load of drywall to fall onto the lift operator, posing an injury hazard."
}
],
"Title": "YTL International Recalls Drywall Lifts Due to Injury Hazard",
"RecallDate": "2016-04-20T00:00:00",
"Description": "This recall involves YTL drywall lifts used for lifting sheets of drywall. The metal lifts are red, have three 30 inch legs with casters at the base, horizontal bars at the top to hold the drywall and a telescoping post that can extend vertically to a height of 11 feet using the manual crank. The recalled drywall lifts have a label on the vertical mast stating \u201cPERFORMANCE BUILT,\u201d \u201cMUD BOSS\u201d OR \u201c150 LB. DRYWALL PANEL HOIST.\u201d",
"Images": [
{
"URL": "http://www.cpsc.gov/Global/Images/Recall/2016/16149/YTL International Drywall Lifter.jpg"
}
],
"Injuries": [
{
"Name": "The firm has received two reports of the drywall lifts failing and dropping their load. No injuries have been reported."
}
],
"LastPublishDate": "2016-04-20T00:00:00",
"RecallNumber": "16149",
"URL": "http://www.cpsc.gov/en/Recalls/2016/YTL-International-Recalls-Drywall-Lifts/",
"ManufacturerCountries": [
{
"Country": "China"
}
],
"Products": [
{
"CategoryID": "67849",
"Model": "",
"Name": "Drywall lifts",
"Type": "Drywall",
"Description": "",
"NumberOfUnits": "About 17,000 (in addition, 2,700 were sold in Canada)"
}
],
"RecallID": 6712,
"Remedies": [],
"Retailers": [
{
"CompanyID": "0",
"Name": "Lowe\u2019s, Menards and Orgill Inc. stores nationwide from January 2013 through January 2016 for about $250."
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment