From 0b96d56c8255e799fe237301ed8762958a10eba8 Mon Sep 17 00:00:00 2001 From: stv-beep Date: Tue, 30 Aug 2022 19:21:01 +0200 Subject: [PATCH] change in get all quotes: now only get top 10 --- README.md | 6 +++--- package-lock.json | 4 ++-- package.json | 2 +- src/routes/driversQuotes.ts | 34 ++++++++++++++++++---------------- src/services/drivers.ts | 4 ++-- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index c09ccd8..6efc3ef 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ -# F1 quotes api 0.3.1 +# F1 quotes api 0.3.2 ##### An API showing several quotes said by famous Formula 1 drivers and Formula 1 personalities. -##### Very easy to use. ```/``` shows all the F1 drivers or personalities who have quotes saved in this API, and their code to access them. For example: Max Verstappen's quotes: ```/quotes/verstappen``` +##### Very easy to use. GET ```/``` shows all the F1 drivers or personalities who have quotes saved in this API, and their code to access them. For example: Max Verstappen's quotes: ```/quotes/verstappen``` ## Endpoints #### GET list of F1 drivers or personalities **->** GET ```/``` -#### GET all quotes of F1 drivers or personalities **->** GET ```/quotes``` +#### GET 10 interesting quotes of F1 drivers or personalities **->** GET ```/quotes``` #### GET all saved quotes from an specific F1 driver **->** GET ```/quotes/:drivers_last_name``` diff --git a/package-lock.json b/package-lock.json index 49f572e..a6d2052 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "f1-quotes-api", - "version": "0.3.1", + "version": "0.3.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "f1-quotes-api", - "version": "0.3.1", + "version": "0.3.2", "license": "ISC", "dependencies": { "axios": "^0.27.2", diff --git a/package.json b/package.json index 3e97740..396bf42 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "f1-quotes-api", - "version": "0.3.1", + "version": "0.3.2", "description": "", "main": "index.js", "scripts": { diff --git a/src/routes/driversQuotes.ts b/src/routes/driversQuotes.ts index 304398b..e2754c7 100644 --- a/src/routes/driversQuotes.ts +++ b/src/routes/driversQuotes.ts @@ -1,7 +1,7 @@ import express from 'express' import axios from 'axios' -import cheerio, { load } from 'cheerio' -import drivers from '../services/drivers' +import { load } from 'cheerio' +import { drivers, top10 } from '../services/drivers' import { driverName } from '../types' const router = express.Router() @@ -11,22 +11,24 @@ const quotes = [] as any const quotesErrorMessage: string = 'Something went wrong or your input is not correct. Try "/quotes".' const driverQuotesErrorMsg: string = ' is not in the database or the input is incorrect.' +const quoteClass: string = '.b-qt' +const quoteContent: string = '.quoteContent' + + /* all quotes */ -drivers.forEach(driver => { - axios.get(driver.address) - .then(response => { - const html = response.data - const $ = load(html) +axios.get(top10) + .then(response => { + const html = response.data + const $ = load(html) - $('.b-qt', html).each(function () { - const quote = $(this).text().replace(/\n/g, '') - quotes.push({ - quote, - author: driver.name - }) + $(quoteContent).each(function () { + const quote = $(this).text().replace(/\n/g, '').replace(/\./g, '. Author: ').replace('Jr. Author: ', 'Jr.') + quotes.push({ + quote }) - }).catch(err => console.log(err)) -}) + }) + + }).catch(err => console.log(err)) router.get('/', (req, res) => { //quotes res.status(200) ? res.send(quotes) : res.json(quotesErrorMessage) }) @@ -51,7 +53,7 @@ router.get('/:driverId', (req, res) => { //quotes/:driverId const $ = load(html) const specificQuotes = [] as any - $('.b-qt', html).each(function () { + $(quoteClass, html).each(function () { const quote = $(this).text().replace(/\n/g, '') specificQuotes.push({ quote, diff --git a/src/services/drivers.ts b/src/services/drivers.ts index 660ff69..c508223 100644 --- a/src/services/drivers.ts +++ b/src/services/drivers.ts @@ -1,4 +1,4 @@ -const drivers = [ +export const drivers = [ { name: 'Max Verstappen', address: 'https://www.brainyquote.com/authors/max-verstappen-quotes', @@ -211,4 +211,4 @@ const drivers = [ } ] -export default drivers; \ No newline at end of file +export const top10 = 'https://www.brainyquote.com/lists/topics/top-10-f1-quotes'