forked from dilame/instagram-private-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.example.ts
38 lines (34 loc) · 1.19 KB
/
session.example.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
import 'dotenv/config';
import { IgApiClient } from '../src';
function fakeSave(data: object) {
// here you would save it to a file/database etc.
// you could save it to a file: writeFile(path, JSON.stringify(data))
return data;
}
function fakeExists() {
// here you would check if the data exists
return false;
}
function fakeLoad() {
// here you would load the data
return '';
}
(async () => {
const ig = new IgApiClient();
ig.state.generateDevice(process.env.IG_USERNAME);
ig.state.proxyUrl = process.env.IG_PROXY;
// This function executes after every request
ig.request.end$.subscribe(async () => {
const serialized = await ig.state.serialize();
delete serialized.constants; // this deletes the version info, so you'll always use the version provided by the library
fakeSave(serialized);
});
if (fakeExists()) {
// import state accepts both a string as well as an object
// the string should be a JSON object
await ig.state.deserialize(fakeLoad());
}
// This call will provoke request.end$ stream
await ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD);
// Most of the time you don't have to login after loading the state
})();