Skip to content
Permalink
Browse files
added currency script
  • Loading branch information
aa7401 committed Dec 24, 2018
1 parent 4542a8b commit ade187e6086c3cd90fcb24fd2c5bf1a3f326f4fd
Show file tree
Hide file tree
Showing 11 changed files with 5,184 additions and 202 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

@@ -0,0 +1,69 @@
'use strict'

const puppeteer = require('puppeteer')
const fs = require('fs')
const request = require('request')
const csv = require('fast-csv')

const getRates = async query => {
const width = 1920
const height = 926
const browser = await puppeteer.launch({ headless: false})
const page = await browser.newPage()
await page.setViewport({ width: width, height: height })
await page.goto('https://www.travelex.co.uk/currency/exchange-rates', { waitUntil: 'domcontentloaded' })
await page.waitFor(5000)
try {
await page.click('a#loadAll')
} catch (error) {
console.log('the loadAll button has already been clicked.')
}
console.log('ready to grab page content')
//const html = await page.content()
const dom = await page.evaluate(() => {
const elements = document.querySelectorAll('div.row')
console.log(`found ${elements.length} rows.`)
const hotels = []
elements.forEach((element) => {
const quoteJson = {}
try {
//quoteJson.quote = element.innerText.replace(/ +/g, ',')
quoteJson.country = element.querySelector('span.col:first-child').innerText
//quoteJson.currencyStr = element.querySelector('span.col:nth-child(2)').innerText
quoteJson.currency = element.querySelector('span.col:nth-child(2)').innerText.split(' (')[0]
quoteJson.code = element.querySelector('span.col:nth-child(2)').innerText.split(' (')[1].replace(')', '')
quoteJson.rate = parseFloat(element.querySelector('span.col:nth-child(3)').innerText)
} catch (err) {
return new Error('oops')
}
hotels.push(quoteJson)
})
return hotels
})
await browser.close()
return dom
}

const getCurrency = callback => getRates().then(data => callback(null, data)).catch(err => callback(err))

getCurrency( (err, data) => {
if(err) console.log('oops!')
console.log(`found ${data.length} CURRENCY codes`)
console.log(data.length)
fs.writeFileSync('currency.json', JSON.stringify(data, null, 2))
})


const getCountries = callback => {
const countryData = []
csv.fromStream(request('https://datahub.io/core/country-list/r/0.csv'))
.on('data', (data) => countryData.push({country: data[0], code: data[1]}))
.on('end', () => callback(null, countryData))
}

getCountries( (err, data) => {
if(err) console.log('oops!')
//console.log(data)
console.log(`found ${data.length} COUNTRY codes`)
fs.writeFileSync('countries.json', JSON.stringify(data, null, 2))
})

Large diffs are not rendered by default.

@@ -9,7 +9,7 @@ const search = async query => {
const browser = await puppeteer.launch({ headless: true})
const page = await browser.newPage()
await page.setViewport({ width: width, height: height })
await page.goto(`https://www.brainyquote.com/search_results?q=${query}`, { waitUntil: 'domcontentloaded' })
await page.goto(`https://www.brainyquote.com/search_results?q=${query}`, { waitUntil: 'networkidle0' })
await autoScroll(page)
const dom = await page.evaluate(() => {
const elements = document.querySelectorAll('div.m-brick')
@@ -0,0 +1,20 @@

'use strict'

const jwt = require('koa-jwt')

module.exports = function(ctx) {
if (ctx.request.body.password === 'password') {
ctx.status = 200
ctx.body = {
token: jwt.sign({ role: 'admin' }, 'A very secret key'), //Should be the same secret key as the one used is ./jwt.js
message: 'Successfully logged in!'
}
} else {
ctx.status = ctx.status = 401
ctx.body = {
message: 'Authentication failed'
}
}
return ctx
}
@@ -0,0 +1,33 @@
'use strict'

const customers = [
{
'id': 1,
'first_name': 'John',
'last_name': 'Smith',
'date_of_birth': '1993-04-23T00:00:00.000Z'
},
{
'id': 2,
'first_name': 'Justin',
'last_name': 'Bieber',
'date_of_birth': '1994-04-23T00:00:00.000Z'
}
]

let maxId = 2

module.exports = {
getCustomers: function() {
return customers
},
getCustomer: function(id) {
return customers.find(customer => customer.id === parseInt(id) || customer.id === id)
},
postCustomer: function(customer) {
maxId++
customer.id = maxId
customers.push(customer)
return this.getCustomer(maxId)
}
}
@@ -0,0 +1,46 @@
#!/usr/bin/env node

'use strict'

// https://blog.theodo.fr/2016/11/securize-a-koa-api-with-a-jwt-token/

const Koa = require('koa')
const Router = require('koa-router')
//const koaBetterBody = require('koa-better-body')
const bodyParser = require('koa-bodyparser')

const app = new Koa()
app.use(bodyParser())
const router = new Router()

const customerService = require('./customerService')
const jwt = require('./jwt')
const authenticate = require('./authenticate.js')

//app.use(koaBetterBody({fields: 'body'}))

router.post('/login', async ctx => {
authenticate(ctx)
})

router.get('/customer', jwt, async ctx => {
ctx.body = customerService.getCustomers()
})

router.get('/customer/:id', async ctx => {
if (customerService.getCustomer(this.params.id)) {
ctx.body = customerService.getCustomer(this.params.id)
} else {
this.status = 404
ctx.body = {'error': 'There is no customer with that id'}
}
})

router.post('/customer', async ctx => {
this.body = customerService.postCustomer(this.request.body)
})

app.use(router.routes())
//.use(router.allowedMethods())

app.listen(8080)
@@ -0,0 +1,7 @@
'use strict'

const koaJwt = require('koa-jwt')

module.exports = koaJwt({
secret: 'A very secret key', // Should not be hardcoded
})
@@ -0,0 +1,18 @@
{
"name": "jwt",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"koa": "^2.6.2",
"koa-better-body": "^3.0.4",
"koa-bodyparser": "^4.2.1",
"koa-jwt": "^3.5.1",
"koa-router": "^7.4.0"
}
}

0 comments on commit ade187e

Please sign in to comment.