Skip to content

Commit

Permalink
add searchLastWeek
Browse files Browse the repository at this point in the history
  • Loading branch information
JCGuest committed Aug 6, 2021
1 parent 7045840 commit 7256bd7
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 29 deletions.
50 changes: 37 additions & 13 deletions models/Search.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
const axios = require('axios');
class Search {
constructor(searchQuery) {
this.lastWeek = false;
this.query = searchQuery;
this.raceName = this.getName(searchQuery);
this.year = this.getYear(searchQuery);
this.position = this.getPosition(searchQuery);
}
async search() {
search() {
if (this.lastWeek) {
return this.searchLastWeek();
}
else {
return this.searchPast();
}
}
async searchPast() {
let roundNumber = await this.findRound();
if (roundNumber) {
let result = await axios
Expand All @@ -20,9 +29,6 @@ class Search {
.catch((err) => console.error(err));
return result;
}
else {
return 'No results';
}
}
async findRound() {
let answer = await axios
Expand All @@ -40,6 +46,7 @@ class Search {
let array = query.split(' ');
let year = array.find((word) => {
let letters = word.split('');
// the next line is a bit of a hack that excludes words like 19th or 22nd
if (!letters.includes('t') && !letters.includes('d')) {
if (letters[0] == '1') {
return word;
Expand Down Expand Up @@ -104,19 +111,29 @@ class Search {
'saudi',
'saudi arabian'
];
const lastWeekNames = ['week', 'previous', 'weeks'];
const queryArray = query.split(' ');
let name = '';
// the next block switches the lastWeek boolean and then
// prevents the while loop from running
if (!this.lastWeek) {
for (let x = 0; x < lastWeekNames.length; x++) {
for (let i = 0; i < queryArray.length; i++) {
if (queryArray[i].toLowerCase() == lastWeekNames[x]) {
name = 'previous';
this.lastWeek = true;
}
}
}
}
while (!name) {
for (let x = 0; x < gpNames.length; x++) {
for (let i = 0; i < queryArray.length; i++) {
gpNames[x] == queryArray[i].toLowerCase()
? (name = gpNames[x])
: null;
gpNames[x] == queryArray[i].toLowerCase() ? (name = gpNames[x]) : -1;
}
}
}
let fullName = name.toLowerCase() + ' grand prix';
console.log(fullName);
return fullName;
}
getPosition(query) {
Expand Down Expand Up @@ -187,10 +204,17 @@ class Search {
}
return gridTranslator[position];
}
async searchLastWeek() {
let result = await axios
.get(`http://ergast.com/api/f1/current/last/results/${this.position}.json`)
.then((json) => {
const data = json.data;
return (data.MRData.RaceTable.Races[0].Results[0].Driver.givenName +
' ' +
data.MRData.RaceTable.Races[0].Results[0].Driver.familyName);
})
.catch((err) => console.error(err));
return result;
}
}
// const Q = new Search('who won the 2009 Italian Grand Prix?');
// console.log(Q.query);
// console.log(Q.url);
// console.log(Q.raceName);
// console.log(Q.year);
module.exports = Search;
55 changes: 41 additions & 14 deletions models/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ class Search {
public raceName: string;
public year: string;
public position: string;
public lastWeek: boolean = false;

constructor(searchQuery: string) {
this.query = searchQuery;
this.raceName = this.getName(searchQuery);
this.year = this.getYear(searchQuery);
this.position = this.getPosition(searchQuery);
}
search() {
if (this.lastWeek) {
return this.searchLastWeek();
} else {
return this.searchPast();
}
}

async search() {
async searchPast() {
let roundNumber = await this.findRound();
if (roundNumber) {
let result = await axios
Expand All @@ -31,8 +39,6 @@ class Search {
.catch((err) => console.error(err));

return result;
} else {
return 'No results';
}
}

Expand All @@ -54,6 +60,7 @@ class Search {
let array = query.split(' ');
let year = array.find((word) => {
let letters = word.split('');
// the next line is a bit of a hack that excludes words like 19th or 22nd
if (!letters.includes('t') && !letters.includes('d')) {
if (letters[0] == '1') {
return word;
Expand Down Expand Up @@ -119,20 +126,29 @@ class Search {
'saudi',
'saudi arabian'
];
const lastWeekNames = ['week', 'previous', 'weeks'];
const queryArray = query.split(' ');
let name = '';

// the next block switches the lastWeek boolean and then
// prevents the while loop from running
if (!this.lastWeek) {
for (let x = 0; x < lastWeekNames.length; x++) {
for (let i = 0; i < queryArray.length; i++) {
if (queryArray[i].toLowerCase() == lastWeekNames[x]) {
name = 'previous';
this.lastWeek = true;
}
}
}
}
while (!name) {
for (let x = 0; x < gpNames.length; x++) {
for (let i = 0; i < queryArray.length; i++) {
gpNames[x] == queryArray[i].toLowerCase()
? (name = gpNames[x])
: null;
gpNames[x] == queryArray[i].toLowerCase() ? (name = gpNames[x]) : -1;
}
}
}
let fullName = name.toLowerCase() + ' grand prix';
console.log(fullName);
return fullName;
}

Expand Down Expand Up @@ -205,12 +221,23 @@ class Search {
}
return gridTranslator[position];
}
}
async searchLastWeek() {
let result = await axios
.get(
`http://ergast.com/api/f1/current/last/results/${this.position}.json`
)
.then((json) => {
const data = json.data;
return (
data.MRData.RaceTable.Races[0].Results[0].Driver.givenName +
' ' +
data.MRData.RaceTable.Races[0].Results[0].Driver.familyName
);
})
.catch((err) => console.error(err));

// const Q = new Search('who won the 2009 Italian Grand Prix?');
// console.log(Q.query);
// console.log(Q.url);
// console.log(Q.raceName);
// console.log(Q.year);
return result;
}
}

module.exports = Search;
2 changes: 0 additions & 2 deletions routes/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ const Search = require('../models/Search.js');
router.post('/', async (req, res) => {
try {
let search = new Search(req.body.query);
// let round = await search.findRound();
// console.log('ROUND >> ' + round);
let result = await search.search();
res.send(result);
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions views/src/components/Dictaphone.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ process.env.NODE_ENV == 'development'

const Dictaphone = () => {
const { transcript, resetTranscript } = useSpeechRecognition();
// const transcript = 'who was second place 1994 italian';
const [result, setResult] = useState('');

if (!SpeechRecognition.browserSupportsSpeechRecognition()) {
Expand Down

0 comments on commit 7256bd7

Please sign in to comment.