-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathupload-photo.example.ts
83 lines (74 loc) · 2.17 KB
/
upload-photo.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
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
/* tslint:disable:no-console */
import { IgApiClient } from '../src';
import { readFile } from 'fs';
import { promisify } from 'util';
const readFileAsync = promisify(readFile);
const ig = new IgApiClient();
async function login() {
// basic login-procedure
ig.state.generateDevice(process.env.IG_USERNAME);
ig.state.proxyUrl = process.env.IG_PROXY;
await ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD);
}
(async () => {
await login();
const path = './myPicture.jpg';
const { latitude, longitude, searchQuery } = {
latitude: 0.0,
longitude: 0.0,
// not required
searchQuery: 'place',
};
/**
* Get the place
* If searchQuery is undefined, you'll get the nearest places to your location
* this is the same as in the upload (-configure) dialog in the app
*/
const locations = await ig.search.location(latitude, longitude, searchQuery);
/**
* Get the first venue
* In the real world you would check the returned locations
*/
const mediaLocation = locations[0];
console.log(mediaLocation);
const publishResult = await ig.publish.photo({
// read the file into a Buffer
file: await readFileAsync(path),
// optional, default ''
caption: 'my caption',
// optional
location: mediaLocation,
// optional
usertags: {
in: [
// tag the user 'instagram' @ (0.5 | 0.5)
await generateUsertagFromName('instagram', 0.5, 0.5),
],
},
});
console.log(publishResult);
})();
/**
* Generate a usertag
* @param name - the instagram-username
* @param x - x coordinate (0..1)
* @param y - y coordinate (0..1)
*/
async function generateUsertagFromName(name: string, x: number, y: number): Promise<{ user_id: number, position: [number, number] }> {
// constrain x and y to 0..1 (0 and 1 are not supported)
x = clamp(x, 0.0001, 0.9999);
y = clamp(y, 0.0001, 0.9999);
// get the user_id (pk) for the name
const { pk } = await ig.user.searchExact(name);
return {
user_id: pk,
position: [x, y],
};
}
/**
* Constrain a value
* @param value
* @param min
* @param max
*/
const clamp = (value: number, min: number, max: number) => Math.max(Math.min(value, max), min);