From 65c5b3f0d1da5e81779e28ccbf24db3033c18eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Thu, 23 Jan 2020 11:35:19 +0800 Subject: [PATCH] feat($Exercise, BodyPart): parse exercise link which is sorted by body part --- .../exercise-link-sorted-by-body-part.ts | 4 +++ src/views/exrx-net/components/body-part.vue | 25 +++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src/domain/body-part/exercise-link-sorted-by-body-part.ts diff --git a/src/domain/body-part/exercise-link-sorted-by-body-part.ts b/src/domain/body-part/exercise-link-sorted-by-body-part.ts new file mode 100644 index 0000000..7881e38 --- /dev/null +++ b/src/domain/body-part/exercise-link-sorted-by-body-part.ts @@ -0,0 +1,4 @@ +export class ExerciseLinkSortedByBodyPart { + bodyPartName?: string + link!: string +} diff --git a/src/views/exrx-net/components/body-part.vue b/src/views/exrx-net/components/body-part.vue index cfc3c82..f062fd7 100644 --- a/src/views/exrx-net/components/body-part.vue +++ b/src/views/exrx-net/components/body-part.vue @@ -17,7 +17,11 @@
- {{ bodyPartList }} +

Body Part List

+

{{ bodyPartList }}

+

Exercise Link List (sorted by body part)

+ Length: {{ exerciseLinkList.length }} +

{{ exerciseLinkList }}

@@ -31,6 +35,8 @@ import { exrxNetApi } from '@/requests/exrx-net-api' import { DomUtil } from '@/utils/dom-util' import { SaveBodyPartPayload } from '@/domain/body-part/save-body-part-payload' import { bodyPartApi } from '@/requests/body-part-api' +import { ExerciseLinkSortedByBodyPart } from '@/domain/body-part/exercise-link-sorted-by-body-part' +import { HyperlinkUtil } from '@/utils/hyperlink-util' export default Vue.extend({ name: 'body-part', @@ -39,7 +45,8 @@ export default Vue.extend({ mergedMuscleUnorderedList: {} as Cheerio, bodyPartList: [] as string[], loadingSaveBodyPart: false, - showBodyPartList: false + showBodyPartList: false, + exerciseLinkList: [] as ExerciseLinkSortedByBodyPart[] }), methods: { async getAndParseBodyPart (): Promise { @@ -48,6 +55,20 @@ export default Vue.extend({ const cheerioInstance = cheerio.load(response) this.mergedMuscleUnorderedList = DomUtil.mergeSameLevelUnorderedList(cheerioInstance('.col-sm-6').find('div > ul')) this.bodyPartList = DomUtil.getFirstLevelTextArray(this.mergedMuscleUnorderedList.children()) + const invalidLinkRegExp = /#+/ + const exerciseListPrefix = 'ExList/' + this.mergedMuscleUnorderedList.find('a').each((index, element) => { + if (!invalidLinkRegExp.test(element.attribs.href)) { + const exerciseLinkSortedByBodyPart = new ExerciseLinkSortedByBodyPart() + exerciseLinkSortedByBodyPart.bodyPartName = element.children[0].data + if (element.attribs.href.search(exerciseListPrefix) < 0) { + exerciseLinkSortedByBodyPart.link = HyperlinkUtil.restorePathToUrl(`/${exerciseListPrefix}${element.attribs.href}`) + } else { + exerciseLinkSortedByBodyPart.link = HyperlinkUtil.restorePathToUrl(`/${element.attribs.href}`) + } + this.exerciseLinkList.push(exerciseLinkSortedByBodyPart) + } + }) } catch (error) { console.error('Error occurred when sending request `exerciseDirectory`!', error) this.$toast.error(`Error occurred when sending request \`exerciseDirectory\`! Cause: ${error.message}`)