|
| 1 | +/* eslint-disable no-console */ |
| 2 | +// A script to upload technologies and their categories to BigQuery. |
| 3 | + |
| 4 | +const fs = require('fs') |
| 5 | +const path = require('path') |
| 6 | +const { BigQuery } = require('@google-cloud/bigquery') |
| 7 | + |
| 8 | +const readJsonFiles = (directory) => { |
| 9 | + const files = fs.readdirSync(directory) |
| 10 | + return files.reduce((mergedData, file) => { |
| 11 | + const filePath = path.join(directory, file) |
| 12 | + const data = fs.readFileSync(filePath, 'utf8') |
| 13 | + return { ...mergedData, ...JSON.parse(data) } |
| 14 | + }, {}) |
| 15 | +} |
| 16 | + |
| 17 | +const getArray = (value) => |
| 18 | + typeof value === 'string' ? [value] : Array.isArray(value) ? value : [] |
| 19 | + |
| 20 | +const getRuleObject = (value) => { |
| 21 | + if (typeof value === 'string') { |
| 22 | + return [{ name: value, value: null }] |
| 23 | + } |
| 24 | + if (Array.isArray(value)) { |
| 25 | + return value.map((key) => ({ name: key, value: null })) |
| 26 | + } |
| 27 | + if (typeof value === 'object') { |
| 28 | + return Object.keys(value).map((key) => ({ |
| 29 | + name: key, |
| 30 | + value: |
| 31 | + typeof value[key] === 'object' |
| 32 | + ? JSON.stringify(value[key]) |
| 33 | + : value[key].toString(), |
| 34 | + })) |
| 35 | + } |
| 36 | + return [] |
| 37 | +} |
| 38 | + |
| 39 | +const loadToBigQuery = async ( |
| 40 | + data, |
| 41 | + tableName = 'apps', |
| 42 | + datasetName = 'wappalyzer', |
| 43 | + writeDisposition = 'WRITE_TRUNCATE', |
| 44 | + sourceFormat = 'NEWLINE_DELIMITED_JSON' |
| 45 | +) => { |
| 46 | + if (!data) { |
| 47 | + throw new Error(`No data to load to \`${datasetName}.${tableName}\`.`) |
| 48 | + } |
| 49 | + |
| 50 | + const bigquery = new BigQuery({ |
| 51 | + keyFilename: '/tmp/gcp_key.json', |
| 52 | + }) |
| 53 | + const schema = { |
| 54 | + fields: [ |
| 55 | + { name: 'name', type: 'STRING' }, |
| 56 | + { name: 'categories', type: 'STRING', mode: 'REPEATED' }, |
| 57 | + { name: 'website', type: 'STRING' }, |
| 58 | + { name: 'description', type: 'STRING' }, |
| 59 | + { name: 'icon', type: 'STRING' }, |
| 60 | + { name: 'cpe', type: 'STRING' }, |
| 61 | + { name: 'saas', type: 'BOOLEAN' }, |
| 62 | + { name: 'oss', type: 'BOOLEAN' }, |
| 63 | + { name: 'pricing', type: 'STRING', mode: 'REPEATED' }, |
| 64 | + { name: 'implies', type: 'STRING', mode: 'REPEATED' }, |
| 65 | + { name: 'requires', type: 'STRING', mode: 'REPEATED' }, |
| 66 | + { name: 'requiresCategory', type: 'STRING', mode: 'REPEATED' }, |
| 67 | + { name: 'excludes', type: 'STRING', mode: 'REPEATED' }, |
| 68 | + { |
| 69 | + name: 'cookies', |
| 70 | + type: 'RECORD', |
| 71 | + mode: 'REPEATED', |
| 72 | + fields: [ |
| 73 | + { name: 'name', type: 'STRING' }, |
| 74 | + { name: 'value', type: 'STRING' }, |
| 75 | + ], |
| 76 | + }, |
| 77 | + { |
| 78 | + name: 'dom', |
| 79 | + type: 'RECORD', |
| 80 | + mode: 'REPEATED', |
| 81 | + fields: [ |
| 82 | + { name: 'name', type: 'STRING' }, |
| 83 | + { name: 'value', type: 'STRING' }, |
| 84 | + ], |
| 85 | + }, |
| 86 | + { |
| 87 | + name: 'dns', |
| 88 | + type: 'RECORD', |
| 89 | + mode: 'REPEATED', |
| 90 | + fields: [ |
| 91 | + { name: 'name', type: 'STRING' }, |
| 92 | + { name: 'value', type: 'STRING' }, |
| 93 | + ], |
| 94 | + }, |
| 95 | + { |
| 96 | + name: 'js', |
| 97 | + type: 'RECORD', |
| 98 | + mode: 'REPEATED', |
| 99 | + fields: [ |
| 100 | + { name: 'name', type: 'STRING' }, |
| 101 | + { name: 'value', type: 'STRING' }, |
| 102 | + ], |
| 103 | + }, |
| 104 | + { |
| 105 | + name: 'headers', |
| 106 | + type: 'RECORD', |
| 107 | + mode: 'REPEATED', |
| 108 | + fields: [ |
| 109 | + { name: 'name', type: 'STRING' }, |
| 110 | + { name: 'value', type: 'STRING' }, |
| 111 | + ], |
| 112 | + }, |
| 113 | + { name: 'text', type: 'STRING', mode: 'REPEATED' }, |
| 114 | + { name: 'css', type: 'STRING', mode: 'REPEATED' }, |
| 115 | + { |
| 116 | + name: 'probe', |
| 117 | + type: 'RECORD', |
| 118 | + mode: 'REPEATED', |
| 119 | + fields: [ |
| 120 | + { name: 'name', type: 'STRING' }, |
| 121 | + { name: 'value', type: 'STRING' }, |
| 122 | + ], |
| 123 | + }, |
| 124 | + { name: 'robots', type: 'STRING', mode: 'REPEATED' }, |
| 125 | + { name: 'url', type: 'STRING', mode: 'REPEATED' }, |
| 126 | + { name: 'xhr', type: 'STRING', mode: 'REPEATED' }, |
| 127 | + { |
| 128 | + name: 'meta', |
| 129 | + type: 'RECORD', |
| 130 | + mode: 'REPEATED', |
| 131 | + fields: [ |
| 132 | + { name: 'name', type: 'STRING' }, |
| 133 | + { name: 'value', type: 'STRING' }, |
| 134 | + ], |
| 135 | + }, |
| 136 | + { name: 'scriptSrc', type: 'STRING', mode: 'REPEATED' }, |
| 137 | + { name: 'script', type: 'STRING', mode: 'REPEATED' }, |
| 138 | + { name: 'html', type: 'STRING', mode: 'REPEATED' }, |
| 139 | + ], |
| 140 | + } |
| 141 | + |
| 142 | + const options = { schema, sourceFormat, writeDisposition } |
| 143 | + const [job] = await bigquery |
| 144 | + .dataset(datasetName) |
| 145 | + .table(tableName) |
| 146 | + .load(data, options) |
| 147 | + |
| 148 | + if (job.status.errors && job.status.errors.length > 0) { |
| 149 | + console.error('Errors encountered:', job.status.errors) |
| 150 | + throw new Error('Error loading data into BigQuery') |
| 151 | + } |
| 152 | + |
| 153 | + console.log( |
| 154 | + `Loaded ${job.numRowsLoaded} rows into ${datasetName}.${tableName}...` |
| 155 | + ) |
| 156 | +} |
| 157 | + |
| 158 | +const main = async () => { |
| 159 | + const technologies = readJsonFiles('./src/technologies') |
| 160 | + const categories = JSON.parse( |
| 161 | + fs.readFileSync('./src/categories.json', 'utf8') |
| 162 | + ) |
| 163 | + |
| 164 | + const transformedTechnologies = Object.keys(technologies).map((key) => { |
| 165 | + const app = { |
| 166 | + name: key, |
| 167 | + categories: technologies[key].cats.map( |
| 168 | + (category) => categories[category].name |
| 169 | + ), |
| 170 | + } |
| 171 | + |
| 172 | + ;[ |
| 173 | + 'implies', |
| 174 | + 'requires', |
| 175 | + 'requiresCategory', |
| 176 | + 'excludes', |
| 177 | + 'text', |
| 178 | + 'css', |
| 179 | + 'robots', |
| 180 | + 'url', |
| 181 | + 'xhr', |
| 182 | + 'scriptSrc', |
| 183 | + 'script', |
| 184 | + 'html', |
| 185 | + ].forEach((field) => { |
| 186 | + app[field] = getArray(technologies[key][field]) |
| 187 | + }) |
| 188 | + ;['cookies', 'dom', 'dns', 'js', 'headers', 'probe', 'meta'].forEach( |
| 189 | + (field) => { |
| 190 | + app[field] = getRuleObject(technologies[key][field]) |
| 191 | + } |
| 192 | + ) |
| 193 | + ;[ |
| 194 | + 'website', |
| 195 | + 'description', |
| 196 | + 'icon', |
| 197 | + 'cpe', |
| 198 | + 'saas', |
| 199 | + 'oss', |
| 200 | + 'pricing', |
| 201 | + ].forEach((field) => { |
| 202 | + app[field] = technologies[key][field] |
| 203 | + }) |
| 204 | + |
| 205 | + return app |
| 206 | + }) |
| 207 | + |
| 208 | + const transformedTechnologiesJsonL = transformedTechnologies |
| 209 | + .map((line) => JSON.stringify(line)) |
| 210 | + .join('\n') |
| 211 | + const filePath = './transformedTechnologies.jsonl' |
| 212 | + fs.writeFileSync(filePath, transformedTechnologiesJsonL) |
| 213 | + |
| 214 | + await loadToBigQuery(filePath, 'apps') |
| 215 | + |
| 216 | + // cleanup file |
| 217 | + fs.unlinkSync(filePath) |
| 218 | +} |
| 219 | + |
| 220 | +main().catch(console.error) |
0 commit comments