Skip to content

Commit

Permalink
🔥[ FEAT ] : Added Kraken Fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
cavalluccijohann committed Jan 16, 2025
1 parent 56345d8 commit 26d5dcc
Show file tree
Hide file tree
Showing 13 changed files with 323 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ logs
.idea

# Local env files
.env
scripts/coinmarketcap/.env
.vercel
.turbo
Binary file modified bun.lockb
Binary file not shown.
2 changes: 0 additions & 2 deletions scripts/cryptocompare/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { MongoDBClient } from '@currencia/mongo'
import { chunkArray } from '@currencia/utils'
import { program } from 'commander'

/*const puppeteer = require('puppeteer')*/

const startUrl = 'https://www.cryptocompare.com/coins/'
const endUrl = '/overview/USD'

Expand Down
42 changes: 0 additions & 42 deletions scripts/cryptocompare/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,3 @@ self.onmessage = async (event) => {
}
}
}

/*
(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
const cryptoList = [
'btc',
'eth',
]
const selector = '#profile-info-header > div.coin-name > div.price-main.ng-scope > div.price-value > div'
const prices = []
for (const crypto of cryptoList) {
// Navigate the page to a URL.
await page.goto('https://www.cryptocompare.com/coins/'+ crypto +'/overview/USD', {
waitUntil: 'networkidle2', // Wait until there are no more than 2 network connections for at least 500 ms.
})
// Wait for the price element to appear
await page.waitForSelector(selector, { timeout: 2000 }).catch(() => {
console.log(`L'élément de prix n'a pas été trouvé pour ${crypto}`)
})
// Get the price
const price = await page.evaluate(() => {
const priceElement = document.querySelector(selector)
return priceElement ? priceElement.textContent.trim() : null
})
// format the price get just the number
// Utiliser une expression régulière pour extraire le nombre
const priceFormat = formatPrice(price)
// push the price to the array
prices.push('price for ' + crypto + ' is ' + priceFormat)
}
console.log(prices)
await browser.close()
})()
*/
5 changes: 5 additions & 0 deletions scripts/kraken/.config/shelve.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/HugoRCD/shelve/main/packages/types/schema.json",
"project": "currencia",
"slug": "hrcd"
}
175 changes: 175 additions & 0 deletions scripts/kraken/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
15 changes: 15 additions & 0 deletions scripts/kraken/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# scripts

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run index.ts
```

This project was created using `bun init` in bun v1.1.29. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
Binary file added scripts/kraken/bun.lockb
Binary file not shown.
51 changes: 51 additions & 0 deletions scripts/kraken/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Worker } from 'worker_threads'
import { cryptos, isCrypto } from '@currencia/cryptos'
import { MongoDBClient } from '@currencia/mongo'
import { chunkArray } from '@currencia/utils'
import { program } from 'commander'

program
.name('Kraken API Fetcher')
.version('1.0.0')
.option('-v, --verbose', 'enable verbose mode')
.option('-t, --threads <threads>', 'number of threads to use', '4')
.option('-c, --crypto <crypto>', 'specific crypto to fetch')
.action(async (options) => {
const results: Record<string, number> = {}
const threads = parseInt(options.threads)
const startUrl = 'https://api.kraken.com/0/public/Ticker?pair='
const endUrl = 'USD'

const fetchCryptoPrices = (cryptos: string[]) => {
return new Promise<void>((resolve) => {
const worker = new Worker('./worker.js', {
workerData: { cryptos, startUrl, endUrl },
})

worker.on('message', ({ crypto, price }) => {
results[crypto] = price
console.log(`${crypto}: ${price}`)
})

worker.on('exit', resolve)
})
}

if (options.crypto) {
await fetchCryptoPrices([options.crypto])
} else {
const cryptoArray = cryptos.map((crypto) => crypto.symbol)
const cryptoChunks = chunkArray(cryptoArray, Math.ceil(cryptoArray.length / threads))
const promises = cryptoChunks.map(chunk => fetchCryptoPrices(chunk))
await Promise.all(promises)
}

console.log('Final results:')
console.table(results)
const client = await MongoDBClient.create()
await client.savePrices(results)
await client.disconnect()
process.exit(0)
})

program.parse()
22 changes: 22 additions & 0 deletions scripts/kraken/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "kraken-fetcher",
"module": "index.ts",
"version": "1.0.0",
"type": "module",
"scripts": {
"dev": "bun --bun run index.ts"
},
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.7.3"
},
"dependencies": {
"@currencia/cryptos": "workspace:*",
"@currencia/utils": "workspace:*",
"@currencia/mongo": "workspace:*",
"commander": "^12.1.0",
"puppeteer": "^23.11.1"
}
}
5 changes: 5 additions & 0 deletions scripts/kraken/shelve.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/HugoRCD/shelve/main/packages/types/schema.json",
"project": "currencia",
"slug": "hrcd"
}
27 changes: 27 additions & 0 deletions scripts/kraken/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,

// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,

// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}
22 changes: 22 additions & 0 deletions scripts/kraken/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { parentPort, workerData } from 'worker_threads'

const fetchCryptoPrice = async (crypto: string, startUrl: string, endUrl: string) => {
try {
const response = await fetch(`${startUrl}${crypto}${endUrl}`)
const data = await response.json()
const price = data.result[Object.keys(data.result)[0]].c[0]
return parseFloat(price)
} catch (error) {
console.error(`Error fetching ${crypto}:`, error)
return null
}
}

const { cryptos, startUrl, endUrl } = workerData;

(async () => {
for (const crypto of cryptos) {
const price = await fetchCryptoPrice(crypto, startUrl, endUrl)
parentPort?.postMessage({ crypto, price })
}
})()

0 comments on commit 26d5dcc

Please sign in to comment.