-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape.ts
More file actions
80 lines (69 loc) · 1.82 KB
/
Copy pathscrape.ts
File metadata and controls
80 lines (69 loc) · 1.82 KB
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
import type {Data, Gem, Scroll, Shoe, Shoebox} from './types.ts'
import {scrapeScript, scrapeFile, stepnApp} from './config.ts'
import { minute, runCommand, wait } from "./util.ts";
const priceRegex = /([0-9.]+) (SOL|BNB|GMT)/
const mintRegex = /Mint: ([0-9]+)/
const levelRegex = /Lv ([0-9]+)/
const idRegex = /[#]\s([0-9]+)/
function fixItem (item: Gem | Shoe | Shoebox | Scroll) {
// adjust details
if ( !item.details || item.details === 'null' ) {
item.details = null
return
}
// extract price
const priceMatch = item.details.match(priceRegex)
if ( priceMatch ) item.price = priceMatch[1]
// extract id
const idMatch = item.details.match(idRegex)
if ( idMatch ) item.id = idMatch[1]
// done
return item
}
export function resetScrapedData() {
const data: Data = {
gems: [],
shoes: [],
shoeboxes: [],
scrolls: []
}
return Deno.writeTextFile(scrapeFile,
JSON.stringify(
data, null, ' '
)
)
}
export async function generateScrapedData() {
await resetScrapedData()
// await runCommand(['open', stepnApp])
// await wait(minute)
const cmd = ['osascript', scrapeScript]
const result = await runCommand(cmd)
if ( ! result.success ) {
console.error(cmd, result)
throw new Error('Failed to update database')
}
return result
}
export async function readScrapedData(): Promise<Data> {
const data: Data = JSON.parse(await Deno.readTextFile(scrapeFile))
for ( const item of data.scrolls ) {
fixItem(item)
}
for ( const item of data.shoeboxes ) {
fixItem(item)
}
for ( const item of data.gems ) {
fixItem(item)
}
for ( const item of data.shoes ) {
fixItem(item)
if ( item.details ) {
const mintMatch = item.details.match(mintRegex)
if ( mintMatch ) item.mint = mintMatch[1]
const levelMatch = item.details.match(levelRegex)
if ( levelMatch ) item.level = levelMatch[1]
}
}
return data
}