Skip to content
This repository has been archived by the owner on Aug 15, 2023. It is now read-only.

Commit

Permalink
fix(tk:shared): check element exists before profile metadata extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
ascariandrea committed Oct 4, 2022
1 parent b72a5dc commit eda3fe1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion platforms/tktrex/backend/__tests__/native.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@shared/test/utils/parser.utils';
import { v4 as uuid } from 'uuid';
import { TKMetadata } from '@tktrex/shared/models';
import { parsers } from '@tktrex/shared/parser';
import { parsers } from '@tktrex/shared/parser/parsers';
import base58 from 'bs58';
import { parseISO, subMinutes } from 'date-fns';
import path from 'path';
Expand Down
2 changes: 1 addition & 1 deletion platforms/tktrex/backend/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@shared/providers/parser.provider';
import { sanitizeHTML } from '@shared/utils/html.utils';
import { TKMetadata } from '@tktrex/shared/models/Metadata';
import { TKParsers } from '@tktrex/shared/parser';
import { TKParsers } from '@tktrex/shared/parser/parsers';
import { TKParserConfig } from '@tktrex/shared/parser/config';
import { HTMLSource } from '@tktrex/shared/parser/source';
import { isValid } from 'date-fns';
Expand Down
16 changes: 10 additions & 6 deletions platforms/tktrex/shared/src/parser/parsers/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import { ParserFn } from '@shared/providers/parser.provider';
import _ from 'lodash';
import { TKParserConfig } from '../config';
import { HTMLSource } from '../source';
import {getNatureByHref} from './nature'
import { getNatureByHref } from './nature';

const debug = trexLogger.extend('parser:profile');


function getFullProfileMetadata(renod: HTMLElement, order: any): any {
function getFullProfileMetadata(renod: ParentNode, order: any): any {
const vlink = renod.querySelector('a[href^="https://www.tiktok.com/@"]');
const vhref = vlink?.getAttribute('href');
const vidnat = getNatureByHref(vhref as any);
Expand All @@ -33,15 +32,20 @@ function getFullProfileMetadata(renod: HTMLElement, order: any): any {
/* this is returning a bunch of native information,
* perhaps might be splitted in appropriate files.
* videoId, error messages, comment disabled, etc */
const profile: ParserFn<HTMLSource, any, TKParserConfig> = async(envelop, previous) => {
const profile: ParserFn<HTMLSource, any, TKParserConfig> = async(
envelop,
previous,
) => {
if (previous.nature.type !== 'profile') return false;

/* this piece of code return a list of videos, because
the search selector is not per video, but per 'body' */
const descs = envelop.jsdom.querySelectorAll('[data-e2e="user-post-item"]');
const results = _.compact(
_.map(descs, function(elem, i) {
return getFullProfileMetadata(elem?.parentNode as any, i + 1);
return elem?.parentNode
? getFullProfileMetadata(elem.parentNode, i + 1)
: null;
}),
);

Expand All @@ -67,6 +71,6 @@ const profile: ParserFn<HTMLSource, any, TKParserConfig> = async(envelop, previo
}

return retval;
}
};

export default profile;

0 comments on commit eda3fe1

Please sign in to comment.