/
jobs.ts
96 lines (80 loc) · 2.44 KB
/
jobs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Browser } from 'src/common/interfaces';
import { reduceAsync, getRandomItem, probability } from 'src/common/utils';
import { job as jobConfig, auth as authConfig } from 'src/config';
import { getMessageBasedOnIntent } from 'src/common/wit';
class Job {
private ranges: number[][] = [];
public execute(browser: Browser) {
throw new Error('Execute must be provided.');
}
public constructor(ranges: number[][]) {
this.ranges = ranges;
}
public validateRanges(hour: number): boolean {
return Boolean(
this.ranges.find(
(range): boolean => {
const [startHour, endHour] = range;
return hour >= startHour && hour <= endHour;
},
),
);
}
}
class FollowJob extends Job {
public async execute(browser: Browser) {
try {
const hashtag = getRandomItem(jobConfig.hashtags);
const postsUrls = await browser.findPosts(hashtag, jobConfig.numberOfPosts);
await reduceAsync<string, void>(
postsUrls,
async (prev, url) =>
browser.getPage(url, async page => {
try {
const post = await browser.getPostInfo(page);
await browser.likePost(page, post);
console.log('liked');
await browser.followPost(page, post);
console.log('followed');
const message = getMessageBasedOnIntent(post.postIntent);
if (probability(jobConfig.commentProbability) && message) {
await browser.commentPost(page, post, message);
console.log('commented');
}
} catch (e) {
console.log('Failed to like/follow/comment.');
console.log(e);
}
}),
35,
);
console.log('FollowJob executed successfully.');
} catch (e) {
console.log('FollowJob failed to execute.');
console.log(e);
}
}
}
class UnfollowJob extends Job {
public async execute(browser: Browser) {
try {
const following = await browser.getFollowing(authConfig.username);
await reduceAsync<string, void>(
following,
async (result, username) => {
await browser.unfollowUser(username);
},
35,
);
console.log('UnfollowJob executed successfully');
} catch (e) {
console.log('UnfollowJob failed to execute.');
console.log(e);
}
}
}
const jobs = {
UnfollowJob,
FollowJob,
};
export { Job, jobs };