Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
human linter
Browse files Browse the repository at this point in the history
  • Loading branch information
breuerfelix committed Nov 10, 2019
1 parent 3e0fd53 commit c986ed1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
8 changes: 6 additions & 2 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,32 @@ class Config {
this.sessionPath = path.resolve(this.workspacePath, 'session.json');
}

public chooseComment = (): string => {
public chooseComment(): string {
if(!this.comments)
throw 'You likely forgot to set comments in your config';
return this.comments[Math.floor(Math.random()*this.comments.length)];
}

public findBlacklistedWord = (text: string): string => {
public findBlacklistedWord(text: string): string {
if (!text) return null;

for (const key of this.blacklist) {
if (text.includes(key)) {
return key;
}
}

return null;
}

public getInterestRate(text: string, base: number, inc: number): number {
if (!text) return base;

let interest = base;
for (const key of this.keywords) {
if (text.includes(key)) interest += inc;
}

return interest;
}
}
Expand Down
15 changes: 9 additions & 6 deletions src/features/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,33 @@ import logger from '../core/logging';
import { addServerCalls } from '../core/store';
import { User } from '../types';

export const defaultMediaValidator = (media: any, config: Config): boolean => {
export function defaultMediaValidator(media: any, config: Config): boolean {
if (media.ad_id || media.link) {
logger.info('[FILTER] media was an ad with id: %s / link: %s', media.ad_id, media.link);
return false;
}

if (media.has_liked) {
logger.warn('[FILTER] media was already liked. %s ', media.id);
return false;
}

if (!media.caption) {
logger.warn('[FILTER] media didn\'t have a caption. %s ', media.id);
return false;
}

const { text } = media.caption;
let badWord;
if((badWord = config.findBlacklistedWord(text))){

const badWord = config.findBlacklistedWord(text);
if(badWord) {
logger.warn('[FILTER] media %s matched blacklist word %s', media.id, badWord);
return false;
}

const { baseInterest, interestInc } = config;
return chance(config.getInterestRate(text, baseInterest, interestInc));
};
}

export async function mediaFeed<T>(
client: IgApiClient,
Expand Down Expand Up @@ -68,8 +71,8 @@ export async function mediaFeed<T>(
allMediaIDs.length,
);

if(!defaultMediaValidator(item, config))
media$.next(item);
// emit media if valid
if(!defaultMediaValidator(item, config)) media$.next(item);

progress++;
await sleep(config.mediaDelay);
Expand Down
11 changes: 7 additions & 4 deletions src/streams/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import logger from '../core/logging';
import { media$ } from './media';

export const comment$ = media$.pipe(

//execute action
flatMap(async media => {
const client = store.getState().client;
Expand All @@ -15,7 +16,7 @@ export const comment$ = media$.pipe(
try {
response = await client.media.comment({
mediaId: media.id,
text: config.chooseComment()
text: config.chooseComment(),
});
} catch (e) {
if (e.message.includes('deleted')) {
Expand All @@ -36,8 +37,9 @@ export const comment$ = media$.pipe(
logger.error(
'[COMMENT] unable to comment media: %o - response: %o',
convertIDtoPost(media.id),
response
response,
);

return false;
}),

Expand All @@ -49,12 +51,13 @@ export const comment$ = media$.pipe(
store.getState().imageComments + 1,
config.commentLimit,
convertIDtoPost(media.id),
response.text
response.text,
);

// increment image comments
store.change(({ imageComments, serverCalls }) => ({
imageComments: imageComments + 1,
serverCalls: serverCalls + 1
serverCalls: serverCalls + 1,
}));
}),

Expand Down
14 changes: 8 additions & 6 deletions src/streams/like.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ export const like$ = media$.pipe(
moduleInfo: {
module_name: 'profile',
user_id: user.pk,
username: user.username
username: user.username,
},
// d means like by double tap (1), you cant unlike posts with double tap
d: chance(0.5) ? 0 : 1
d: chance(.5) ? 0 : 1
});
} catch (e) {
if (e.message.includes('deleted')) {
response.status = 'not okay';
response.error = e;
} else {
throw e;
} // throw the error
}
}

return { media, response };
Expand All @@ -42,10 +42,12 @@ export const like$ = media$.pipe(
logger.error(
'[LIKE] unable to like media: %o - response: %o',
convertIDtoPost(media.id),
response
response,
);

return false;
}),

//perform statistics and log computation
tap(({ media, response }) => {
const config = store.getState().config;
Expand All @@ -54,12 +56,12 @@ export const like$ = media$.pipe(
store.getState().imageLikes + 1,
config.likeLimit,
convertIDtoPost(media.id),
response
response,
);

store.change(({ imageLikes, serverCalls }) => ({
imageLikes: imageLikes + 1,
serverCalls: serverCalls + 1
serverCalls: serverCalls + 1,
}));
}),

Expand Down

0 comments on commit c986ed1

Please sign in to comment.