Skip to content

Commit

Permalink
feat: fix bugs, clean code and added code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dhohirpradana authored Feb 26, 2023
1 parent 5ec5a62 commit af2a40d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 158 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ jobs:
env:
IG_USERNAME: '${{ secrets.IG_USERNAME }}'
IG_PASSWORD: '${{ secrets.IG_PASSWORD }}'
UNSPLASH_ACCESS_KEY: '${{ secrets.UNSPLASH_ACCESS_KEY }}'
# UNSPLASH_ACCESS_KEY: '${{ secrets.UNSPLASH_ACCESS_KEY }}'
QUOTES_API_KEY: '${{ secrets.QUOTES_API_KEY }}'
run: ts-node app.ts
132 changes: 57 additions & 75 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ require("dotenv").config();
const ig = new IgApiClient();

async function login() {
console.log("🚀 Login");
ig.state.generateDevice(process.env.IG_USERNAME ?? "");
// await ig.simulate.preLoginFlow();
await ig.account.login(
process.env.IG_USERNAME ?? "",
process.env.IG_PASSWORD ?? ""
);
try {
await ig.account.login(
process.env.IG_USERNAME ?? "",
process.env.IG_PASSWORD ?? ""
);
console.log("✅ Login Success");

} catch (error) {
console.log("❌ Error login", error);
}
// await ig.simulate.postLoginFlow();
// process.nextTick(async () => await ig.simulate.postLoginFlow());
}
Expand All @@ -38,7 +45,6 @@ async function getRandPost() {
return posts[rand];
}
var randLine = getRandLine(results);
// console.log("🚀 ~ file: app.ts:35 ~ getRandPost ~ randLine", randLine)

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

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

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

if (randPost === "error") return getQuotes();
// console.log("🚀 ~ file: app.ts:80 ~ randPost", randPost)
if (randPost === "error") {
console.log("❌ Error getting posts");
console.log("🔄 Try to get quotes");
return getQuotes();
}

console.log("✅ Get Posts Success");

// var randPostName = randPost[0];
var randPostImage = randPost[1];
var randPostText = randPost[2];

console.log("🚀 randPostText", randPostText);
// console.log(`from ${randPostName}, image url ${randPostImage}`);
console.log("✅ Post Text", randPostText);
console.log("✅ Source", randPost[0]);


caption = randPostText;

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

imageBuffer = randPostImageBuffer;
} catch (error) {
console.log("🚀 ~ file: app.ts:117 ~ getPosts ~ error", error)
console.log("❌ Error getting image", error);
return getQuotes();
}
}
Expand All @@ -152,41 +162,55 @@ async function generateImage(text: string) {
caption: caption,
});

// delay for 5 seconds
await bluebird.delay(5000);
console.log("✅ Publish Feed Success");

// delay for random 5 seconds
await bluebird.delay(Math.floor(Math.random() * 5000) + 5000);

// like a publishPhoto
await ig.media.like({
mediaId: publishPhoto.media.id,
moduleInfo: {
module_name: "profile",
user_id: publishPhoto.media.user.pk,
username: publishPhoto.media.user.username,
},
d: 0,
});
try {
await ig.media.like({
mediaId: publishPhoto.media.id,
moduleInfo: {
module_name: "profile",
user_id: publishPhoto.media.user.pk,
username: publishPhoto.media.user.username,
},
d: 0,
});
console.log("✅ Like publish feed success");
} catch (error) {
console.log("❌ Error like publish feed", error);
}

} catch (error) {
console.log("🚀 ~ file: app.ts:88 ~ error", error);
if (publishFeedTry === 0) return;
console.log("❌ Error publish feed", error);

console.log("🔄 Try to get posts", publishFeedTry);

if (publishFeedTry === 0) return console.log("❌ Error publish feed after 3 times");

getPosts().then(() => {
publishFeed();
});
publishFeedTry--;
}
}

// delay for 3 seconds
await bluebird.delay(3000);
// delay for random 3 seconds
await bluebird.delay(Math.floor(Math.random() * 3000) + 3000);

// like 5 timeline feeds
console.log("🚀 Like Timeline Feeds 3 times");
var likeTimes = 3;

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

var likeTimes = 3;
items.forEach(async (item) => {
if (likeTimes === 0) return;
if (likeTimes === 0) return console.log("✅ Like Timeline Feeds 3 times finished");

await ig.media.like({
mediaId: item.id,
moduleInfo: {
Expand All @@ -203,48 +227,6 @@ async function generateImage(text: string) {
likeTimes--;
});
} catch (error) {
console.log("🚀 ~ file: app.ts:117 ~ error", error);
console.log("❌ Error get timeline feeds", error);
}
})();

// const unsplashAccessKey = process.env.UNSPLASH_ACCESS_KEY;

// const photos = await axios(
// "https://api.unsplash.com/photos/random?query=minimal",
// {
// headers: {
// Authorization: `Client-ID ${unsplashAccessKey}`,
// },
// }
// );

// const data = photos.data;
// var photo = data;
// var user = photo.user;
// var username = user.username;
// var credit = `\nPhoto by ${username} on Unsplash`;
// var link = photo.urls.regular;

// // share to story
// try {
// const story = await ig.publish.story({
// file: imageBuffer,
// stickerConfig: new StickerBuilder()
// .add(
// StickerBuilder.hashtag({
// tagName: "minimal",
// }).center()
// )
// // .add(
// // StickerBuilder.attachmentFromMedia(
// // (
// // await ig.feed.timeline().items()
// // )[0]
// // ).center()
// // )
// .build(),
// });
// console.log("🚀 ~ file: app.ts:73 ~ story", story);
// } catch (error) {
// console.log("🚀 ~ file: app.ts:88 ~ error", error);
// }
})();
2 changes: 0 additions & 2 deletions image.ts

This file was deleted.

55 changes: 0 additions & 55 deletions index.js

This file was deleted.

25 changes: 0 additions & 25 deletions test.ts

This file was deleted.

0 comments on commit af2a40d

Please sign in to comment.