This repository was archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·172 lines (168 loc) · 4.3 KB
/
cli.js
File metadata and controls
executable file
·172 lines (168 loc) · 4.3 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
#!/usr/bin/env node
"use strict";
const download = require("image-downloader");
const prof = require("./lib/Profile.js");
const os = require("os");
const dns = require("dns");
const ora = require("ora");
const chalk = require("chalk");
const logUpdate = require("log-update");
const spinner = ora();
const end = process.exit;
const arg = process.argv[2];
const fse = require("fs-extra");
const user = process.argv[3];
const pos = chalk.red.bold("›");
const dir = `${os.homedir()}/Instagram`;
fse.ensureDir(dir, err => {
if (err) {
logUpdate();
console.log(err.message, "\n");
throwIssue();
end(1);
}
});
const throwIssue = () => {
console.log(
"Please raise a issue here https://github.com/Tanuj69/instagram-api/issues if this is unexpected"
);
};
const dim = foll => {
return chalk.dim(foll);
};
const checkConnection = () => {
dns.lookup("instagram.com", err => {
if (err) {
logUpdate(`\n${pos} ${dim("Please check your internet connection!")}\n`);
throwIssue();
end(1);
}
logUpdate();
spinner.text = "Checking";
spinner.start();
});
};
const noUserArg = () => {
if (!user) {
logUpdate(`\n${pos} ${dim("<username/link> required\n")}`);
throwIssue();
end(1);
}
};
const returnBase = () => {
noUserArg();
checkConnection();
};
const errorMessage = () => {
logUpdate(`\n${pos} ${user} ${dim("is not an Instagram user!")}\n`);
throwIssue();
spinner.stop();
end(1);
};
const fetchProfile = () => {
logUpdate();
spinner.text = "Fetching Profile";
};
const argArray = ["-m", "--meta", "-D", "--download", "-f", "--full"];
if (!arg || arg === "-h" || arg === "--help" || argArray.indexOf(arg) === -1) {
console.log(`
${chalk.cyan("Usage")} : instaget ${chalk.cyan("[command]")} ${chalk.white(
"<username>"
)}
${chalk.cyan("Command")} :
-m, ${dim(
"--meta"
)} Get info like number of following,followers,Profile picture link etc.
-f, ${dim(
"--full"
)} Get all info in table format.
-D, ${dim(
"--download"
)} Download all available media (Profile picture, latest 12 posts ) \n
${chalk.cyan("Example")} : instaget -m selenagomez
instaget -f selenagomez
instaget -D selenagomez
`);
end(1);
}
logUpdate();
spinner.text = "Please Wait!";
spinner.start();
if (arg == "-D" || arg == "--download") {
fetchProfile();
returnBase();
const pro = new prof(user);
pro
.getData()
.then(async data => {
delete data.bio;
delete data.url;
let str = JSON.stringify(data);
const u = require("get-urls");
let URLArr = Array.from(u(str));
spinner.text = `Downloading to ${dir}`;
logUpdate();
URLArr = URLArr.filter(e => e.length > 45);
let flag = false;
for (let i = 0; i < URLArr.length; i++) {
let link = URLArr[i];
await download.image({
url: link,
dest: `${dir}/`
});
if (i == URLArr.length - 2) flag = true;
if (flag) {
spinner.text = "Downloaded";
logUpdate();
console.log("Downloading complete");
process.exit(1);
}
}
})
.catch(e => {
logUpdate();
console.log(e.message, "\n");
throwIssue();
process.exit(1);
});
}
if (arg == "-m" || arg == "--meta") {
fetchProfile();
returnBase();
const pro = new prof(user);
pro
.getData()
.then(data => {
logUpdate(`
${chalk.blue.bold("Username")}: ${data.username}
${chalk.blue.bold("Biography")}: ${data.bio}
${chalk.blue.bold("Followers")}: ${data.followers}
${chalk.blue.bold("Following")}: ${data.following}
${chalk.blue.bold("Total Posts")}: ${data.totalPosts}
${chalk.blue.bold("Profile URL")}: ${data.url}
${chalk.blue.bold("Profile Picture")}: ${data.profilePicHD}
`);
spinner.stop();
// console.log(data);
})
.catch(err => {
console.log(err);
errorMessage();
});
}
if (arg == "-f" || arg == "--full") {
fetchProfile();
returnBase();
const pro = new prof(user);
pro
.getData()
.then(data => {
spinner.stop();
console.table([data], ["username", "bio", "followers", "following", "totalPosts", "url"])
console.table(data.posts, ["dimensions", "likes", "comments", "url"])
})
.catch(err => {
console.log(err);
errorMessage();
});
}