-
Notifications
You must be signed in to change notification settings - Fork 1
/
card-lookups.js
81 lines (68 loc) · 2.1 KB
/
card-lookups.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const fetch = require('node-fetch')
const QueryString = require('querystring')
const urls = require('./urls')
const lookupPackOfCardsBySet = set => {
const targetUrl = urls.boosterSearchBySet(set)
console.log('Making mtgapi request for set ', set)
console.log('Target url ', targetUrl)
return fetch(targetUrl)
.then(response => response.json())
.then(data => {
const cards = data.cards
return cards
})
}
const lookupCardByMessage = text => {
const queryParams = {
pageSize: 2,
name: text.slice(1),
layout: 'normal|split|flip|double-faced|leveler|aftermath'
}
const params = QueryString.stringify(queryParams)
const targetUrl = urls.cardSearch + params
console.log('Making mtgapi request with params, ', params)
console.log(targetUrl)
return fetch(targetUrl)
.then(response => response.json())
.then(data => {
var firstCard = data.cards[0]
const cards = [firstCard]
if (firstCard.layout === 'double-faced') {
if (!firstCard.number.endsWith('a') && !firstCard.number.endsWith('b')) {
firstCard = data.cards[1]
}
const otherCardNumber = firstCard.number.endsWith('a')
? firstCard.number.replace('a', 'b')
: firstCard.number.replace('b', 'a')
return cardLookupBySetNumber({ set: firstCard.set, number: otherCardNumber })
.then(nextCard => { cards.push(nextCard) })
.then(() => cards)
}
return cards
})
}
const cardLookupBySetNumber = (options) => {
const set = options.set
const number = options.number
const queryParams = {
pageSize: 1,
set: set,
number: number,
layout: 'normal|split|flip|double-faced|leveler|aftermath'
}
const params = QueryString.stringify(queryParams)
console.log('Making mtgapi request')
console.log(params)
console.log(urls.cardSearch + params)
return fetch(urls.cardSearch + params)
.then(response => response.json())
.then(data => {
console.log(data)
return data.cards[0]
})
}
module.exports = {
cardLookupBySetNumber,
lookupCardByMessage,
lookupPackOfCardsBySet
}