Created
March 6, 2017 05:02
-
-
Save chrisrng/2a6970df4ad64e2d82f6578b739bd579 to your computer and use it in GitHub Desktop.
Retrieves data from PatentsView.org API
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
const cache = {} | |
const bigNandlitteNcompanies = [ | |
'Amazon Technologies, Inc.', | |
'Microsoft Technology Licensing, LLC', | |
'Apple Inc.', | |
'Facebook, Inc.', | |
'Google Inc.', | |
'LinkedIn Corporation', | |
'Uber Technologies, Inc.', | |
'Pinterest, Inc.', | |
'Snapchat, Inc.', | |
'QUALCOMM Incorporated', | |
'Two Sigma Investments, LLC', | |
'Palantir Technologies, Inc.', | |
'Dropbox, Inc.', | |
'Square, Inc.', | |
'salesforce.com, inc.', | |
'Yahoo! Inc.', | |
'Netflix, Inc.', | |
'Twitter, Inc.' | |
].map(companyName => { | |
cache[companyName] = {}; | |
for (var year = 2006; year <= 2016; year++) { | |
makePatentCall(companyName, year); | |
} | |
}); | |
// TODO: promisify makePatentCall and do a cache to CSV after promise.all | |
function cacheToCSV(cache) { | |
const csv = {}; | |
const csvHeader = []; | |
const comapanies = Object.keys(cache); | |
comapanies.map(company => { | |
csvHeader.push(company); | |
const years = Object.keys(cache[company]); | |
return years.map(year => { | |
if (!csv[year]) { csv[year] = [] }; | |
const value = cache[company][year]; | |
csv[year].push(value) | |
}); | |
}); | |
const csvStr = []; | |
csvStr.push('\t' + csvHeader.join('\t')); | |
const years = Object.keys(csv); | |
years.map(year => { | |
csvStr.push(`${year}\t` + csv[year].join('\t')); | |
}) | |
return csvStr.join('\r\n'); | |
} | |
function makePatentCall(companyName, yearEnding) { | |
const xmlhttp = new XMLHttpRequest(); | |
const url = buildPatentsViewURL([companyName], `${yearEnding}-01-01`, `${yearEnding}-12-31`); | |
xmlhttp.onreadystatechange = () => { | |
if (xmlhttp.readyState == XMLHttpRequest.DONE) { | |
if (xmlhttp.status == 200) { | |
const patentData = JSON.parse(xmlhttp.response); | |
console.log(`${companyName}, ${yearEnding}, ${patentData.total_patent_count}`); | |
cache[companyName][yearEnding] = patentData.total_patent_count; | |
} else { | |
alert(`There was an error ${xmlhttp.status}!`); | |
} | |
} | |
}; | |
xmlhttp.open('GET', url, true); | |
xmlhttp.send(); | |
} | |
function buildPatentsViewURL( | |
organizationNames = [], | |
dateStart = '2016-01-01', | |
dateEnd = '2016-12-31', | |
dataToGetBack = ['patent_title', 'inventor_first_name', 'inventor_last_name', 'patent_date', 'assignee_organization']) { | |
const listOfDataToGetBack = dataToGetBack.map(data => `"${data}"`).join(','); | |
const listOfOrganizations = organizationNames.map(orgName => `{"_and":[ | |
{"_eq": {"assignee_organization":"${orgName}"}}, | |
{"assignee_type":"2"}, | |
{"assignee_lastknown_country":"US"}, | |
{"_gte":{"patent_date":"${dateStart}"}}, | |
{"_lte":{"patent_date":"${dateEnd}"}} | |
]}`).join(',') | |
return `http://www.patentsview.org/api/patents/query?q={"_or":[${listOfOrganizations}]}&f=[${listOfDataToGetBack}]`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment