Skip to content

Commit af2a40d

Browse files
feat: fix bugs, clean code and added code improvements
1 parent 5ec5a62 commit af2a40d

File tree

5 files changed

+58
-158
lines changed

5 files changed

+58
-158
lines changed

.github/workflows/node.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ jobs:
7272
env:
7373
IG_USERNAME: '${{ secrets.IG_USERNAME }}'
7474
IG_PASSWORD: '${{ secrets.IG_PASSWORD }}'
75-
UNSPLASH_ACCESS_KEY: '${{ secrets.UNSPLASH_ACCESS_KEY }}'
75+
# UNSPLASH_ACCESS_KEY: '${{ secrets.UNSPLASH_ACCESS_KEY }}'
7676
QUOTES_API_KEY: '${{ secrets.QUOTES_API_KEY }}'
7777
run: ts-node app.ts

app.ts

Lines changed: 57 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ require("dotenv").config();
1010
const ig = new IgApiClient();
1111

1212
async function login() {
13+
console.log("🚀 Login");
1314
ig.state.generateDevice(process.env.IG_USERNAME ?? "");
1415
// await ig.simulate.preLoginFlow();
15-
await ig.account.login(
16-
process.env.IG_USERNAME ?? "",
17-
process.env.IG_PASSWORD ?? ""
18-
);
16+
try {
17+
await ig.account.login(
18+
process.env.IG_USERNAME ?? "",
19+
process.env.IG_PASSWORD ?? ""
20+
);
21+
console.log("✅ Login Success");
22+
23+
} catch (error) {
24+
console.log("❌ Error login", error);
25+
}
1926
// await ig.simulate.postLoginFlow();
2027
// process.nextTick(async () => await ig.simulate.postLoginFlow());
2128
}
@@ -38,7 +45,6 @@ async function getRandPost() {
3845
return posts[rand];
3946
}
4047
var randLine = getRandLine(results);
41-
// console.log("🚀 ~ file: app.ts:35 ~ getRandPost ~ randLine", randLine)
4248

4349
// split randLine by |჻|
4450
var randLineSplit = randLine.split("|჻|");
@@ -84,7 +90,6 @@ async function generateImage(text: string) {
8490

8591
var source = ["posts"];
8692
var randSource = source[Math.floor(Math.random() * source.length)];
87-
// console.log("🚀 ~ file: app.ts:65 ~ randSource", randSource);
8893

8994
let imageBuffer: Buffer;
9095
let caption: string;
@@ -108,15 +113,20 @@ async function generateImage(text: string) {
108113
console.log("🚀 Get Posts");
109114
var randPost = await getRandPost();
110115

111-
if (randPost === "error") return getQuotes();
112-
// console.log("🚀 ~ file: app.ts:80 ~ randPost", randPost)
116+
if (randPost === "error") {
117+
console.log("❌ Error getting posts");
118+
console.log("🔄 Try to get quotes");
119+
return getQuotes();
120+
}
121+
122+
console.log("✅ Get Posts Success");
113123

114-
// var randPostName = randPost[0];
115124
var randPostImage = randPost[1];
116125
var randPostText = randPost[2];
117126

118-
console.log("🚀 randPostText", randPostText);
119-
// console.log(`from ${randPostName}, image url ${randPostImage}`);
127+
console.log("✅ Post Text", randPostText);
128+
console.log("✅ Source", randPost[0]);
129+
120130

121131
caption = randPostText;
122132

@@ -128,7 +138,7 @@ async function generateImage(text: string) {
128138

129139
imageBuffer = randPostImageBuffer;
130140
} catch (error) {
131-
console.log("🚀 ~ file: app.ts:117 ~ getPosts ~ error", error)
141+
console.log("❌ Error getting image", error);
132142
return getQuotes();
133143
}
134144
}
@@ -152,41 +162,55 @@ async function generateImage(text: string) {
152162
caption: caption,
153163
});
154164

155-
// delay for 5 seconds
156-
await bluebird.delay(5000);
165+
console.log("✅ Publish Feed Success");
166+
167+
// delay for random 5 seconds
168+
await bluebird.delay(Math.floor(Math.random() * 5000) + 5000);
157169

158170
// like a publishPhoto
159-
await ig.media.like({
160-
mediaId: publishPhoto.media.id,
161-
moduleInfo: {
162-
module_name: "profile",
163-
user_id: publishPhoto.media.user.pk,
164-
username: publishPhoto.media.user.username,
165-
},
166-
d: 0,
167-
});
171+
try {
172+
await ig.media.like({
173+
mediaId: publishPhoto.media.id,
174+
moduleInfo: {
175+
module_name: "profile",
176+
user_id: publishPhoto.media.user.pk,
177+
username: publishPhoto.media.user.username,
178+
},
179+
d: 0,
180+
});
181+
console.log("✅ Like publish feed success");
182+
} catch (error) {
183+
console.log("❌ Error like publish feed", error);
184+
}
185+
168186
} catch (error) {
169-
console.log("🚀 ~ file: app.ts:88 ~ error", error);
170-
if (publishFeedTry === 0) return;
187+
console.log("❌ Error publish feed", error);
188+
189+
console.log("🔄 Try to get posts", publishFeedTry);
190+
191+
if (publishFeedTry === 0) return console.log("❌ Error publish feed after 3 times");
192+
171193
getPosts().then(() => {
172194
publishFeed();
173195
});
174196
publishFeedTry--;
175197
}
176198
}
177199

178-
// delay for 3 seconds
179-
await bluebird.delay(3000);
200+
// delay for random 3 seconds
201+
await bluebird.delay(Math.floor(Math.random() * 3000) + 3000);
202+
203+
// like 5 timeline feeds
204+
console.log("🚀 Like Timeline Feeds 3 times");
205+
var likeTimes = 3;
180206

181207
try {
182-
// like 5 user feed
183208
const feed = ig.feed.timeline();
184209
const items = await feed.items();
185-
// console.log("🚀 ~ file: app.ts:117 ~ items", items)
186210

187-
var likeTimes = 3;
188211
items.forEach(async (item) => {
189-
if (likeTimes === 0) return;
212+
if (likeTimes === 0) return console.log("✅ Like Timeline Feeds 3 times finished");
213+
190214
await ig.media.like({
191215
mediaId: item.id,
192216
moduleInfo: {
@@ -203,48 +227,6 @@ async function generateImage(text: string) {
203227
likeTimes--;
204228
});
205229
} catch (error) {
206-
console.log("🚀 ~ file: app.ts:117 ~ error", error);
230+
console.log("❌ Error get timeline feeds", error);
207231
}
208-
})();
209-
210-
// const unsplashAccessKey = process.env.UNSPLASH_ACCESS_KEY;
211-
212-
// const photos = await axios(
213-
// "https://api.unsplash.com/photos/random?query=minimal",
214-
// {
215-
// headers: {
216-
// Authorization: `Client-ID ${unsplashAccessKey}`,
217-
// },
218-
// }
219-
// );
220-
221-
// const data = photos.data;
222-
// var photo = data;
223-
// var user = photo.user;
224-
// var username = user.username;
225-
// var credit = `\nPhoto by ${username} on Unsplash`;
226-
// var link = photo.urls.regular;
227-
228-
// // share to story
229-
// try {
230-
// const story = await ig.publish.story({
231-
// file: imageBuffer,
232-
// stickerConfig: new StickerBuilder()
233-
// .add(
234-
// StickerBuilder.hashtag({
235-
// tagName: "minimal",
236-
// }).center()
237-
// )
238-
// // .add(
239-
// // StickerBuilder.attachmentFromMedia(
240-
// // (
241-
// // await ig.feed.timeline().items()
242-
// // )[0]
243-
// // ).center()
244-
// // )
245-
// .build(),
246-
// });
247-
// console.log("🚀 ~ file: app.ts:73 ~ story", story);
248-
// } catch (error) {
249-
// console.log("🚀 ~ file: app.ts:88 ~ error", error);
250-
// }
232+
})();

image.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

index.js

Lines changed: 0 additions & 55 deletions
This file was deleted.

test.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)