forked from bangui/TEACHING-MATERIALS
Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
5,184 additions
and
202 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict' | ||
|
||
const koaJwt = require('koa-jwt') | ||
|
||
module.exports = koaJwt({ | ||
secret: 'A very secret key', // Should not be hardcoded | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |