1 | #!/usr/bin/env node
|
2 | const fs = require('fs')
|
3 | const { promisify } = require('util')
|
4 | const request = require('request-promise')
|
5 | const { uniqBy } = require('lodash')
|
6 | const additionalSupporters = require('./additional-supporters.json')
|
7 | const removeSupporters = require('./remove-supporters.json')
|
8 |
|
9 | const asyncWriteFile = promisify(fs.writeFile)
|
10 |
|
11 | const API_KEY = '43d883997da5983dc50dfcb288c690903fbebf9a'
|
12 | const REQUIRED_KEYS = ['totalDonations', 'slug', 'name']
|
13 | const filename = '../dist/supports.json'
|
14 |
|
15 | const graphqlEndpoint = `https://api.opencollective.com/graphql/v2/${API_KEY}`
|
16 |
|
17 | const graphqlQuery = `query account($limit: Int, $offset: Int) {
|
18 | account(slug: "bootstrap-table") {
|
19 | orders(limit: $limit, offset: $offset) {
|
20 | limit
|
21 | offset
|
22 | totalCount
|
23 | nodes {
|
24 | fromAccount {
|
25 | name
|
26 | slug
|
27 | website
|
28 | imageUrl
|
29 | }
|
30 | totalDonations {
|
31 | value
|
32 | }
|
33 | createdAt
|
34 | }
|
35 | }
|
36 | }
|
37 | }`
|
38 |
|
39 | const graphqlPageSize = 1000
|
40 |
|
41 | const nodeToSupporter = node => ({
|
42 | name: node.fromAccount.name,
|
43 | slug: node.fromAccount.slug,
|
44 | website: node.fromAccount.website,
|
45 | avatar: node.fromAccount.imageUrl,
|
46 | firstDonation: node.createdAt,
|
47 | totalDonations: node.totalDonations.value
|
48 | })
|
49 |
|
50 | const getAllOrders = async () => {
|
51 | const requestOptions = {
|
52 | method: 'POST',
|
53 | uri: graphqlEndpoint,
|
54 | body: { query: graphqlQuery, variables: { limit: graphqlPageSize, offset: 0 } },
|
55 | json: true
|
56 | }
|
57 |
|
58 | let allOrders = []
|
59 |
|
60 |
|
61 |
|
62 | while (true) {
|
63 | const result = await request(requestOptions)
|
64 | const orders = result.data.account.orders.nodes
|
65 | allOrders = [...allOrders, ...orders]
|
66 | requestOptions.body.variables.offset += graphqlPageSize
|
67 | if (orders.length < graphqlPageSize) {
|
68 | return allOrders
|
69 | }
|
70 | }
|
71 | }
|
72 |
|
73 | getAllOrders()
|
74 | .then(orders => {
|
75 | let supporters = orders.map(nodeToSupporter)
|
76 |
|
77 | supporters = supporters.concat(additionalSupporters)
|
78 | .sort((a, b) => b.totalDonations - a.totalDonations)
|
79 |
|
80 |
|
81 | supporters = uniqBy(supporters, 'slug')
|
82 |
|
83 | supporters = supporters.filter(s => {
|
84 | return s.totalDonations > 0 && !removeSupporters.slugs.includes(s.slug)
|
85 | })
|
86 |
|
87 | if (!Array.isArray(supporters)) {
|
88 | throw new Error('Supporters data is not an array.')
|
89 | }
|
90 |
|
91 | for (const item of supporters) {
|
92 | for (const key of REQUIRED_KEYS) {
|
93 | if (!item || typeof item !== 'object') {
|
94 | throw new Error(`Supporters: ${JSON.stringify(item)} is not an object.`)
|
95 | }
|
96 | if (!(key in item)) {
|
97 | throw new Error(`Supporters: ${JSON.stringify(item)} doesn't include ${key}.`)
|
98 | }
|
99 | }
|
100 | }
|
101 |
|
102 |
|
103 | return asyncWriteFile(filename, JSON.stringify(supporters, null, 2)).then(() =>
|
104 | console.log(`Fetched 1 file: ${filename}`)
|
105 | )
|
106 | })
|
107 | .catch(error => {
|
108 | console.error('utilities/fetch-supporters:', error)
|
109 | })
|