Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert back to api 1.0.1 + context attribute #79

Merged
merged 1 commit into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 8 additions & 26 deletions src/js/extractWordData.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,15 @@ const sanitizeString = string => {
.replace(/"/g, '""')
}

const wordPicture = translations => {
if (!translations) return ''
const translationWithPic = translations.find(tr =>
tr.pic && !tr.pic.startsWith('https://contentcdn.lingualeo.com/uploads/upimages')
)
return translationWithPic ? `<img src='${translationWithPic.pic}'>` : ''
const wordPicture = word => {
if (!word) return ''
return word.picture && word.picture.startsWith('https://contentcdn.lingualeo.com/uploads/picture') ? `<img src='${word.picture}'>` : ''
}

const isWrappedContext = (contextStr) => {
try {
return !!(JSON.parse(contextStr) || {}).context_text
} catch (e) {
return false
}
}
const wordContext = (word) => {
if (!word || !word.context) return ''

const wordContext = (translation) => {
if (!translation || !translation.ctx) return ''

const rawContext = translation.ctx
const rawContext = word.context

try {
const contextObj = JSON.parse(rawContext) || {}
Expand All @@ -44,20 +33,13 @@ const wordContext = (translation) => {

const clozefy = (word, string) => string.replace(wordRegExp(word), '{{c1::$&}}')

const selectedTranslations = (word) => {
if (!word.translations) return []
const fromInternet = word.translations.filter(tr => isWrappedContext(tr.ctx))
return fromInternet.length > 0 ? fromInternet : [word.translations[0]]
}

const extractWordData = (word) => {
const userTranslations = selectedTranslations(word)
const translations = sanitizeString(word.combinedTranslation)
const wordValue = sanitizeString(word.wordValue)
const context = wordContext(userTranslations[0])
const context = wordContext(word)
const highlightedContext = highlightWord(wordValue, context)
const clozefiedContext = clozefy(wordValue, context)
const image = wordPicture(userTranslations) || wordPicture(word.translations)
const image = wordPicture(word)
const sound = `[sound:${word.pronunciation}]`
const groups = (word.wordSets || []).map(ws => ws.name).join(',')
const association = sanitizeString(word.association)
Expand Down
1 change: 1 addition & 0 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const createExportButton = (locale, totalWordsCount, groupId) => {
wordCount = totalWordsCount
} else {
const selectedWords = selectedWordsIds()
console.log({ selectedWords })
filter = selectedFilter(selectedWords)
wordCount = selectedWords.length
}
Expand Down
53 changes: 41 additions & 12 deletions src/js/lingualeo/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default class LeoApi {
credentials: 'include',
mode: 'cors',
body: JSON.stringify({
apiVersion: '1.0.0',
apiVersion: '1.0.1',
...requestData
})
})
Expand All @@ -41,27 +41,56 @@ export default class LeoApi {
})
}

_requestWords (wordSetId, leoFilter, totalWords, filter, progressHandler, offsetWordId, result) {
_requestWords (wordSetId, leoFilter, totalWords, filter, progressHandler, page, result) {
return this._request('GetWords', {
...GetWordsRequest,
...leoFilter,
wordSetIds: [wordSetId],
offset: offsetWordId ? { wordId: offsetWordId } : undefined
dateGroup: page.dataGroup,
offset: page.wordId ? { wordId: page.wordId } : undefined
}).then((data) => {
if (data.length === 0) return result
if (data.length === 0) return result
const groupsWithWords = data.filter(g => g.words.length > 0)
console.log({ groupsWithWords })

const words = data.filter(filter || emptyFilter)
const currentGroup = groupsWithWords.length
? groupsWithWords[groupsWithWords.length - 1].groupName
: page.dataGroup

result = result.concat(words)
progressHandler && progressHandler(result.length)
if (result.length >= totalWords) return result
console.log( { currentGroup })
const words = data.map(g => g.words).flat().filter(filter || emptyFilter)

const offsetWordId = data[data.length - 1].id
return this._requestWords(wordSetId, leoFilter, totalWords, filter, progressHandler, offsetWordId, result)
})
result = result.concat(words)
progressHandler && progressHandler(result.length)

const currentGroupIndex = data.findIndex(e => e.groupName === currentGroup)

if (
(currentGroupIndex === data.length - 1 && !words.length) ||
result.length >= totalWords
) {
return result
}

const nextGroupIndex = currentGroupIndex === data.length - 1 || words.length
? currentGroupIndex
: currentGroupIndex + 1

console.log({ nextGroupIndex });

page = {
dataGroup: data[nextGroupIndex].groupName,
wordId: words.length ? words[words.length - 1].id : undefined
}
return this._requestWords(wordSetId, leoFilter, totalWords, filter, progressHandler, page, result)
})
}

getWords (wordSetId, leoFilter, totalWords, filter, progressHandler) {
return this._requestWords(wordSetId, leoFilter, totalWords, filter, progressHandler, null, [])
const page = {
dataGroup: 'start'
}
return this._requestWords(wordSetId, leoFilter, totalWords, filter, progressHandler, page, [])
}
}
3 changes: 2 additions & 1 deletion src/js/lingualeo/requests/GetWords.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"picture": "pic",
"speechPartId": "pid",
"wordLemmaId": "lid",
"wordLemmaValue": "lwd"
"wordLemmaValue": "lwd",
"context": "ctx"
},
"mode": "basic",
"training": null,
Expand Down