add parsing words api response
This commit is contained in:
parent
09f6293879
commit
4b910d6646
|
@ -1,32 +1,16 @@
|
||||||
import {search} from 'urban-dictionary-client';
|
import {search} from 'urban-dictionary-client';
|
||||||
import {SET_AVAILABLE_API} from '../../constants/api-constants';
|
import {
|
||||||
|
SET_AVAILABLE_API,
|
||||||
|
URBAN_DICTIONARY_API,
|
||||||
|
WORDS_API,
|
||||||
|
} from '../../constants/api-constants';
|
||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
|
import {getResFromWordsAPI, parseWordsApi} from './words-api';
|
||||||
async function getResFromWordsAPI(word) {
|
import {
|
||||||
const req = await fetch(`https://wordsapiv1.p.rapidapi.com/words/${word}`, {
|
getResFromUrbanDictionary,
|
||||||
method: 'GET',
|
parseUrbanDictionaryApi,
|
||||||
headers: {
|
} from './urban-dictionary';
|
||||||
'x-rapidapi-host': 'wordsapiv1.p.rapidapi.com',
|
import {createDictionary} from '../dictionary/create-dictionary';
|
||||||
'x-rapidapi-key': 'e08b0f617cmsh74abbf9a3b01eb0p164f22jsnabea29750b15',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const json = await req.json();
|
|
||||||
const res = {...json, source: 'wordsAPI'};
|
|
||||||
if (res.success === false) {
|
|
||||||
// throw new Error(res.message);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return Promise.resolve(res);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getResFromUrbanDictionary(word) {
|
|
||||||
const res = await search(word);
|
|
||||||
if (res.list.length === 0) {
|
|
||||||
// throw new Error('nothing was found');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return Promise.resolve({...res, word, source: 'urbanDictionary'});
|
|
||||||
}
|
|
||||||
|
|
||||||
const getAvailableApi = (apiArray = []) => {
|
const getAvailableApi = (apiArray = []) => {
|
||||||
for (const api of apiArray) {
|
for (const api of apiArray) {
|
||||||
|
@ -41,16 +25,19 @@ const setAvailableApi = api => ({
|
||||||
type: SET_AVAILABLE_API,
|
type: SET_AVAILABLE_API,
|
||||||
payload: api,
|
payload: api,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const wordInfo = word => async dispatch => {
|
export const wordInfo = word => async dispatch => {
|
||||||
try {
|
try {
|
||||||
const api1 = await getResFromWordsAPI(word);
|
const api1 = await getResFromWordsAPI(word);
|
||||||
const api2 = await getResFromUrbanDictionary(word);
|
const api2 = await getResFromUrbanDictionary(word);
|
||||||
const availableApi = getAvailableApi([api1, api2]);
|
const availableApi = getAvailableApi([api1, api2]);
|
||||||
console.log(availableApi);
|
|
||||||
if (availableApi === false) {
|
if (availableApi === false) {
|
||||||
throw new Error('word not found');
|
throw new Error('word not found');
|
||||||
}
|
}
|
||||||
|
console.log(availableApi);
|
||||||
|
//function which return universal template for more simple interaction with api
|
||||||
await dispatch(setAvailableApi(availableApi));
|
await dispatch(setAvailableApi(availableApi));
|
||||||
|
createDictionary(availableApi);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
|
|
0
src/actions/api/parse-actions.js
Normal file
0
src/actions/api/parse-actions.js
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import {search} from "urban-dictionary-client";
|
||||||
|
import {URBAN_DICTIONARY_API} from "../../constants/api-constants";
|
||||||
|
|
||||||
|
export async function getResFromUrbanDictionary(word) {
|
||||||
|
const res = await search(word);
|
||||||
|
if (res.list.length === 0) {
|
||||||
|
// throw new Error('nothing was found');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return Promise.resolve({...res, word, source: URBAN_DICTIONARY_API});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const parseUrbanDictionaryApi = api => {}
|
|
@ -0,0 +1,50 @@
|
||||||
|
import {WORDS_API} from '../../constants/api-constants';
|
||||||
|
|
||||||
|
export async function getResFromWordsAPI(word) {
|
||||||
|
const req = await fetch(`https://wordsapiv1.p.rapidapi.com/words/${word}`, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'x-rapidapi-host': 'wordsapiv1.p.rapidapi.com',
|
||||||
|
'x-rapidapi-key': 'e08b0f617cmsh74abbf9a3b01eb0p164f22jsnabea29750b15',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const json = await req.json();
|
||||||
|
const res = {...json, source: WORDS_API};
|
||||||
|
if (res.success === false) {
|
||||||
|
// throw new Error(res.message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return Promise.resolve(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsePOS = wordArray => {
|
||||||
|
let posSet = new Set();
|
||||||
|
let posArray = [];
|
||||||
|
wordArray.forEach((result, id) => {
|
||||||
|
const pos = result.partOfSpeech;
|
||||||
|
if (!posSet.has(pos)) {
|
||||||
|
posSet.add(pos);
|
||||||
|
posArray.push({pos, id});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return posArray;
|
||||||
|
};
|
||||||
|
//get all what anki template need for template
|
||||||
|
const getDefinitionList = wordsArray => {
|
||||||
|
const partOfSpeeches = parsePOS(wordsArray);
|
||||||
|
let definitionList = [];
|
||||||
|
partOfSpeeches.forEach(pos => {
|
||||||
|
const currentWord = wordsArray[pos.id];
|
||||||
|
definitionList.push({
|
||||||
|
definition: currentWord.definition,
|
||||||
|
example: currentWord.examples[0],
|
||||||
|
id: pos.id,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return definitionList;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const parseWordsApi = api => ({
|
||||||
|
pronunciation: api.pronunciation.all,
|
||||||
|
words: getDefinitionList(api.results),
|
||||||
|
});
|
5
src/actions/dictionary/create-dictionary.js
Normal file
5
src/actions/dictionary/create-dictionary.js
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import {parseDictionary} from './parsing-dictionary';
|
||||||
|
|
||||||
|
export const createDictionary = apiRes => {
|
||||||
|
const parsedDictionary = parseDictionary(apiRes);
|
||||||
|
};
|
16
src/actions/dictionary/parsing-dictionary.js
Normal file
16
src/actions/dictionary/parsing-dictionary.js
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
//middleware between two api response parsers
|
||||||
|
import {URBAN_DICTIONARY_API, WORDS_API} from '../../constants/api-constants';
|
||||||
|
import {parseWordsApi} from '../api/words-api';
|
||||||
|
import {parseUrbanDictionaryApi} from '../api/urban-dictionary';
|
||||||
|
|
||||||
|
export const parseDictionary = api => {
|
||||||
|
switch (api.source) {
|
||||||
|
case WORDS_API:
|
||||||
|
return parseWordsApi(api);
|
||||||
|
case URBAN_DICTIONARY_API:
|
||||||
|
alert('Not available yet');
|
||||||
|
return parseUrbanDictionaryApi(api);
|
||||||
|
default:
|
||||||
|
throw new Error('wrong api');
|
||||||
|
}
|
||||||
|
};
|
|
@ -11,8 +11,8 @@ import {wordInfo} from "../actions/api/dictionary";
|
||||||
|
|
||||||
const AnkiForm = props => {
|
const AnkiForm = props => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
props.yDictionary('wealth');
|
props.yDictionary('like');
|
||||||
props.wordInfo('suck')
|
props.wordInfo('penis poop')
|
||||||
}, []);
|
}, []);
|
||||||
return (
|
return (
|
||||||
<Container style={{padding: 20}}>
|
<Container style={{padding: 20}}>
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
// export const c = 'c'
|
// export const ct = 'ct'
|
||||||
export const SET_YANDEX_DICTIONARY_RESPONSE = 'SET_YANDEX_DICTIONARY_RESPONSE';
|
export const SET_YANDEX_DICTIONARY_RESPONSE = 'SET_YANDEX_DICTIONARY_RESPONSE';
|
||||||
export const SET_AVAILABLE_API = 'SET_AVAILABLE_API';
|
export const SET_AVAILABLE_API = 'SET_AVAILABLE_API';
|
||||||
|
export const WORDS_API = 'WORDS_API'
|
||||||
|
export const URBAN_DICTIONARY_API = 'URBAN_DICTIONARY_API'
|
||||||
|
|
||||||
|
|
||||||
|
export const SET_PARSED_DICTIONARY = 'SET_PARSED_DICTIONARY'
|
||||||
|
|
|
@ -10,6 +10,7 @@ const initialState = {
|
||||||
availableApi: {},
|
availableApi: {},
|
||||||
availableApiName: '',
|
availableApiName: '',
|
||||||
yandexDictionaryInfo: [],
|
yandexDictionaryInfo: [],
|
||||||
|
parsedDictionary: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
const apiReducer = (state = initialState, action) => {
|
const apiReducer = (state = initialState, action) => {
|
||||||
|
|
Loading…
Reference in a new issue