-
Notifications
You must be signed in to change notification settings - Fork 728
Member enrichment ProgAI Linkedin scraper source #2674
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
65c3605
progai linkedin scraper start
epipav 95098d7
progai linkedin scraper first tests passing fine
epipav f0405c1
Merge branch 'main' into feature/progai-linkedin-scraper-source
epipav 7fa43ef
fix linting
epipav be18b12
Merge branch 'feature/progai-linkedin-scraper-source' of github.com:C…
epipav 3f6ca89
linting and cleaning
epipav b565f7a
don't reinforce data if same profile is returned between linkedin scr…
epipav 5632d0e
increase getMembersToEnrich startToCloseTimeout
epipav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
...ces/apps/premium/members_enrichment_worker/src/sources/progai-linkedin-scraper/service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| import axios from 'axios' | ||
|
|
||
| import { Logger, LoggerBase } from '@crowd/logging' | ||
| import { IMemberIdentity, MemberEnrichmentSource, PlatformType } from '@crowd/types' | ||
|
|
||
| import { findMemberEnrichmentCacheForAllSources } from '../../activities/enrichment' | ||
| import { EnrichmentSourceServiceFactory } from '../../factory' | ||
| import { | ||
| IEnrichmentService, | ||
| IEnrichmentSourceInput, | ||
| IMemberEnrichmentDataNormalized, | ||
| } from '../../types' | ||
| import { IMemberEnrichmentDataProgAI, IMemberEnrichmentDataProgAIResponse } from '../progai/types' | ||
|
|
||
| export default class EnrichmentServiceProgAILinkedinScraper | ||
| extends LoggerBase | ||
| implements IEnrichmentService | ||
| { | ||
| public source: MemberEnrichmentSource = MemberEnrichmentSource.PROGAI_LINKEDIN_SCRAPER | ||
| public platform = `enrichment-${this.source}` | ||
|
|
||
| public alsoFindInputsInSourceCaches: MemberEnrichmentSource[] = [ | ||
| MemberEnrichmentSource.CLEARBIT, | ||
| MemberEnrichmentSource.SERP, | ||
| ] | ||
|
|
||
| public enrichableBySql = `(mi.verified AND mi.type = 'username' and mi.platform = 'linkedin')` | ||
|
|
||
| // bust cache after 120 days | ||
| public cacheObsoleteAfterSeconds = 60 * 60 * 24 * 120 | ||
|
|
||
| constructor(public readonly log: Logger) { | ||
| super(log) | ||
| } | ||
|
|
||
| // in addition to members with linkedin identity | ||
| // we'll also use existing cache rows from other sources (serp and clearbit) | ||
| // if there are linkedin urls there as well, we'll enrich using these also | ||
| async isEnrichableBySource(input: IEnrichmentSourceInput): Promise<boolean> { | ||
| const caches = await findMemberEnrichmentCacheForAllSources(input.memberId) | ||
|
|
||
| let hasEnrichableLinkedinInCache = false | ||
| for (const cache of caches) { | ||
| if (this.alsoFindInputsInSourceCaches.includes(cache.source)) { | ||
| const service = EnrichmentSourceServiceFactory.getEnrichmentSourceService( | ||
| cache.source, | ||
| this.log, | ||
| ) | ||
| const normalized = service.normalize(cache.data) as IMemberEnrichmentDataNormalized | ||
| if (normalized.identities.some((i) => i.platform === PlatformType.LINKEDIN)) { | ||
| hasEnrichableLinkedinInCache = true | ||
| break | ||
| } | ||
|
|
||
| break | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| hasEnrichableLinkedinInCache || | ||
| (input.linkedin && input.linkedin.value && input.linkedin.verified) | ||
| ) | ||
| } | ||
|
|
||
| async hasRemainingCredits(): Promise<boolean> { | ||
| return true | ||
| } | ||
|
|
||
| private async findConsumableLinkedinIdentities( | ||
| input: IEnrichmentSourceInput, | ||
| ): Promise<IMemberIdentity[]> { | ||
| const consumableIdentities: IMemberIdentity[] = [] | ||
| const caches = await findMemberEnrichmentCacheForAllSources(input.memberId) | ||
| const linkedinUrlHashmap = new Map<string, boolean>() | ||
|
|
||
| for (const cache of caches) { | ||
| if (this.alsoFindInputsInSourceCaches.includes(cache.source)) { | ||
| const service = EnrichmentSourceServiceFactory.getEnrichmentSourceService( | ||
| cache.source, | ||
| this.log, | ||
| ) | ||
| const normalized = service.normalize(cache.data) as IMemberEnrichmentDataNormalized | ||
| if (normalized.identities.some((i) => i.platform === PlatformType.LINKEDIN)) { | ||
| const identity = normalized.identities.find((i) => i.platform === PlatformType.LINKEDIN) | ||
| if (!linkedinUrlHashmap.get(identity.value)) { | ||
| consumableIdentities.push(identity) | ||
| linkedinUrlHashmap.set(identity.value, true) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // also add the linkedin identity from the input | ||
| if ( | ||
| input.linkedin && | ||
| input.linkedin.value && | ||
| input.linkedin.verified && | ||
| !linkedinUrlHashmap.get(input.linkedin.value) | ||
| ) { | ||
| consumableIdentities.push(input.linkedin) | ||
| } | ||
|
|
||
| return consumableIdentities | ||
| } | ||
|
|
||
| async getData(input: IEnrichmentSourceInput): Promise<IMemberEnrichmentDataProgAI[] | null> { | ||
| const profiles: IMemberEnrichmentDataProgAI[] = [] | ||
| const consumableIdentities = await this.findConsumableLinkedinIdentities(input) | ||
|
|
||
| for (const identity of consumableIdentities) { | ||
| const data = await this.getDataUsingLinkedinHandle(identity.value) | ||
| if (data) { | ||
| profiles.push(data) | ||
| } | ||
| } | ||
|
|
||
| return profiles.length > 0 ? profiles : null | ||
| } | ||
|
|
||
| private async getDataUsingLinkedinHandle(handle: string): Promise<IMemberEnrichmentDataProgAI> { | ||
| let response: IMemberEnrichmentDataProgAIResponse | ||
|
|
||
| try { | ||
| const url = `${process.env['CROWD_ENRICHMENT_PROGAI_URL']}/get_profile` | ||
| const config = { | ||
| method: 'get', | ||
| url, | ||
| params: { | ||
| linkedin_url: `https://linkedin.com/in/${handle}`, | ||
| with_emails: true, | ||
| api_key: process.env['CROWD_ENRICHMENT_PROGAI_API_KEY'], | ||
| }, | ||
| headers: {}, | ||
| } | ||
|
|
||
| response = (await axios(config)).data | ||
| } catch (err) { | ||
| throw new Error(err) | ||
| } | ||
|
|
||
| return response.profile | ||
| } | ||
|
epipav marked this conversation as resolved.
|
||
|
|
||
| normalize(profiles: IMemberEnrichmentDataProgAI[]): IMemberEnrichmentDataNormalized[] { | ||
| const normalizedProfiles: IMemberEnrichmentDataNormalized[] = [] | ||
| const progaiService = EnrichmentSourceServiceFactory.getEnrichmentSourceService( | ||
| MemberEnrichmentSource.PROGAI, | ||
| this.log, | ||
| ) | ||
|
|
||
| for (const profile of profiles) { | ||
| const normalized = progaiService.normalize(profile) as IMemberEnrichmentDataNormalized | ||
| normalizedProfiles.push(normalized) | ||
| } | ||
|
|
||
| return normalizedProfiles.length > 0 ? normalizedProfiles : null | ||
| } | ||
| } | ||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implement actual credit checking.
The method currently always returns true without verifying available credits. This could lead to issues if the ProgAI service has rate limits or credit restrictions.
Consider implementing actual credit checking by:
Would you like me to help implement this functionality?