-
Notifications
You must be signed in to change notification settings - Fork 11
/
reddit2insta.ts
99 lines (85 loc) · 2.67 KB
/
reddit2insta.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
97
98
99
import fs from "fs";
import axios from "axios";
import { join } from "path";
import { get } from "request-promise";
import { IgApiClient } from "instagram-private-api";
import {
animeme_from_reddit,
animeme_in_json,
animeme_to_insta,
} from "./interfaces";
let animemes: animeme_from_reddit[];
const posted_animemes_location = join(".", "posted_memes.json");
export const reddit2insta = async (
ig_uname: string,
ig_pass: string,
subreddit: string,
ig_hashtags: string,
top_x?: number
) => {
const ig = new IgApiClient();
ig.state.generateDevice(ig_uname);
// logging into instagram
try {
const auth = await ig.account.login(ig_uname, ig_pass);
if (!auth.pk) return "LOGIN FAILED";
} catch (error) {
console.log(error);
return "LOGIN BLOCKED";
}
// fetching past memes
const raw_pa = fs.readFileSync(posted_animemes_location);
const posted_animemes: animeme_in_json = JSON.parse(raw_pa.toString());
// fetching new memes from reddit
const res = await axios.get(
`https://www.reddit.com/r/${subreddit}/top.json?limit=${top_x ? top_x : 3}`
);
animemes = res.data.data.children;
// inspecting new meme
animemes.forEach(async (animeme, i) => {
console.log("\n" + animeme.data.title);
// check if meme is image and new
if (animeme.data.url.split(".")[0].split("://")[1] === "v") {
console.log(":( video desu");
return null;
}
if (posted_animemes[animeme.data.id]) {
console.log("seen this!");
return null;
}
const animeme_to_post: animeme_to_insta = {
title: animeme.data.title,
author: animeme.data.author,
url: animeme.data.url,
};
// generate buffer from image
const imageBuffer = await get({
url: animeme_to_post.url,
encoding: null,
});
// hashtag generation
const hashtag_list = ig_hashtags.split(" ");
const hashtags = hashtag_list.reduce((hashtags, hashtag) => {
return hashtags + ` #${hashtag}`;
});
try {
const publishResult = await ig.publish.photo({
file: imageBuffer,
caption: `credits: u/${animeme_to_post.author}\n${animeme_to_post.title} ¯\\_(ツ)_/¯\n.\n.\n.\n.\n.\n#${hashtags}`,
});
console.log(animeme.data.url);
if (publishResult.status === "ok") {
// update list of posted memes
posted_animemes[animeme.data.id] = animeme_to_post;
fs.writeFileSync(
posted_animemes_location,
JSON.stringify(posted_animemes)
);
console.log("updated posted memes");
}
} catch (error) {
console.log("work on fixing that image size thing you sucker");
}
});
return "SUCCESS - UPLOADING NEW IMAGE MEMES";
};