-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
200 lines (169 loc) · 7.79 KB
/
app.js
File metadata and controls
200 lines (169 loc) · 7.79 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
require('dotenv').config();
const config = require('./config.js');
const fs = require('fs');
const axios = require("axios");
const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
puppeteer.use(StealthPlugin());
function capture(userString, first, last, isHtml = false) {
const regex = new RegExp(`${first}(.*?)${last}`, 's');
const match = userString.match(regex);
if (match) {
let result = match[1];
if (!isHtml) {
result = result.replace(/\s\s+/g, '').trim();
}
return result;
}
return null;
};
function recursiveCapture(userString, start, final, isHtml = false) {
const regex = new RegExp(`${start}(.*?)${final}`, 'g');
let matches = [...userString.matchAll(regex)].map(match => match[1]);
if (!isHtml) {
matches = matches.map(match => match.replace(/<[^>]*>?/gm, '').trim());
}
return matches;
};
function randomChoice(array) {
return array[Math.floor(Math.random() * array.length)];
};
function updateConfig() {
const updatedConfig = JSON.stringify(config, null, 2); // Serialize the configuration object to JSON
fs.writeFileSync('./config.js', `module.exports = ${updatedConfig};`, 'utf-8');
};
async function getContent() {
const headers = {
"authority": "scripai.com",
"accept": "*/*",
"accept-language": "en-US,en;q=0.6",
"content-type": "application/json",
"origin": "https://scripai.com",
"referer": "https://scripai.com/linkedin-post",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
};
const data = {
prompt: {
title: randomChoice(config.skills) + " " + randomChoice(config.post_title_suffix),
description: randomChoice(config.post_description),
keywords: randomChoice(config.post_keywords),
platform: "LinkedIn",
language: randomChoice(config.post_language),
tone: randomChoice(config.post_tone)
},
slug: "linkedin-post"
};
let contentHttpRequestBlock;
while (true) {
try {
contentHttpRequestBlock = await axios.post("https://scripai.com/api/getGPTdata", data, {
headers
});
break;
}
catch (e) {
await new Promise(resolve => setTimeout(resolve, 2000));
continue;
}
};
const contentBlockResponse = contentHttpRequestBlock.data;
if (!JSON.stringify(contentBlockResponse).includes('"result":"\\n\\n')) {
return false;
};
return contentBlockResponse.result;
};
async function postContent() {
const browser = await puppeteer.launch({
executablePath: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe', // Specify your chrome path.
headless: "new" // Make it false (headless: false) if there are too many errors.
});
const pages = await browser.pages();
const page = pages[0];
try {
await page.goto("https://www.linkedin.com/login", {
waitUntil: 'networkidle0'
});
await page.type('#username', process.env.linkedin_email);
await page.type('#password', process.env.linkedin_pass);
await page.click('button[aria-label="Sign in"]');
const loginHttpRequestBlock = await page.waitForResponse(httpRequest => httpRequest.url() == 'https://www.linkedin.com/checkpoint/lg/login-submit');
if (parseInt(loginHttpRequestBlock.status().toString().substr(0, 1)) != 3) {
const loginBlockResponse = await loginHttpRequestBlock.text();
if (loginBlockResponse.includes("Wrong email")) {
console.error("Incorrect email entered.");
} else if (loginBlockResponse.includes("right password")) {
console.error("Incorrect password entered.");
} else {
console.error(`Unknown error occured, here's the response: ${loginBlockResponse}`);
};
await browser.close();
process.exit();
};
let dashboardHttpRequestBlock;
try {
dashboardHttpRequestBlock = await page.waitForResponse(httpRequest => httpRequest.url() == 'https://www.linkedin.com/feed/', {
timeout: 5000 // Increase it if your connection is slow.
});
} catch (error) {
console.error(`Ip banned / Slow connection speed.`);
await browser.close();
process.exit();
};
const dashboardBlockResponse = await dashboardHttpRequestBlock.text();
if (!dashboardBlockResponse.includes('recent_activity_nav_all","actionTarget":"https://www.linkedin.com/')) {
console.error(`Unknown error occured, here's the response: ${dashboardBlockResponse}`);
await browser.close();
process.exit();
};
// Add skills automatically in config.js if not found.
if (config.skills.length < 1) {
const username = capture(dashboardBlockResponse, 'recent_activity_nav_all","actionTarget":"https://www.linkedin.com/', '/recent-activity/",');
await page.goto(`https://www.linkedin.com/${username}/details/skills/`);
const skillHttpRequestBlock = await page.waitForResponse(httpRequest => httpRequest.url().includes('/api/graphql?includeWebMetadata=true&variables=(profileUrn') && httpRequest.url().includes('sectionType:skills'));
const skillBlockResponse = await skillHttpRequestBlock.text();
await page.goBack();
if (!skillBlockResponse.includes('"accessibilityText":"Edit ')) {
console.error(`Unknown error occured, here's the response: ${skillBlockResponse}`);
await browser.close();
process.exit();
};
const skillLists = recursiveCapture(skillBlockResponse, '"accessibilityText":"Edit ', '",');
config.skills = [...new Set(skillLists)];
updateConfig();
};
await page.waitForSelector('.artdeco-button.artdeco-button--muted.artdeco-button--4.artdeco-button--tertiary.ember-view.share-box-feed-entry__trigger');
await page.click('.artdeco-button.artdeco-button--muted.artdeco-button--4.artdeco-button--tertiary.ember-view.share-box-feed-entry__trigger');
const checkContentResponse = await getContent();
if (!checkContentResponse) {
console.error(`Unknown error occured, here's the response: ${checkContentResponse}`);
await browser.close();
process.exit();
};
await page.type('.ql-editor', checkContentResponse.substr(2)); // Removing the \n\n from response
await page.waitForTimeout(2000); // Wait for button to be clickable
await page.click('.share-actions__primary-action.artdeco-button.artdeco-button--2.artdeco-button--primary.ember-view');
await browser.close();
return true;
}
catch (error) {
console.error(`An error occurred: ${error.message}`);
await browser.close();
process.exit();
};
};
(async () => {
let nextPostTimestamp = 0;
while (true) {
const currentTimestamp = Date.now();
if (currentTimestamp >= nextPostTimestamp) {
const checkPostContentBlock = await postContent();
if (checkPostContentBlock) {
nextPostTimestamp = (currentTimestamp + ((Math.floor(Math.random() * 2) + 1) * 60 * 60 * 1000)) + 24 * 60 * 60 * 1000; // 24 hours * 60 minutes * 60 seconds * 1000 milliseconds
};
}
const timeToNextPost = nextPostTimestamp - Date.now();
if (timeToNextPost > 0) {
await new Promise(resolve => setTimeout(resolve, timeToNextPost));
}
};
})();