-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
107 lines (83 loc) · 2.77 KB
/
index.js
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
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const fsPromises = fs.promises;
const util = require('util');
const stream = require('stream');
const pipeline = util.promisify(stream.pipeline);
const MEDIA_FILE = 'instagram-media.json';
const igApiUrl = 'https://graph.instagram.com/me/media';
const fetchAllMediaPages = async (nextUrl, nextParams, nextPageIndex) => {
const accessToken = process.env.IG_ACCESS_TOKEN;
if (!accessToken) {
throw new Error('Missing required environment variable "IG_ACCESS_TOKEN."');
}
const url = nextUrl || igApiUrl;
const params = {
params: nextParams || {
access_token: accessToken,
fields: 'media_url,permalink'
}
};
const page = nextPageIndex || 0;
const result = await axios.get(url, params);
const data = result.data;
await Promise.all(data.data.map(async (m, i) => {
data.data[i].github_media_url = `https://mdb.github.io/feeder/feeds/${m.id}.jpg`;
return thisModule.downloadFile(m.media_url, m.id);
}));
const fileName = `instagram-media-${page}.json`;
const igNext = data.paging.next;
// Delete the previous field; the IG previous URL contains an access token.
delete data.paging.previous;
if (igNext) {
// Overwrite the next URL with GH next URL; the IG next URL contains an
// access token.
data.paging.next = `https://mdb.github.io/feeder/feeds/instagram-media-${page + 1}.json`;
}
await fsPromises.writeFile(fileName, JSON.stringify(data));
if (igNext) {
await fetchAllMediaPages(igNext, {}, page + 1)
}
};
const saveRecentMedia = async () => {
const media = await fsPromises.readFile(MEDIA_FILE);
await Promise.all(JSON.parse(media).data.map(async (m) => {
return downloadFile(m.media_url, m.id);
}));
};
const addGitHubUrlsToMediaJson = async () => {
const media = await fsPromises.readFile(MEDIA_FILE);
const newMedia = JSON.parse(media).data.map(m => {
// the IG API formats JSON properties in snake case
m.github_media_url = `https://mdb.github.io/feeder/feeds/${m.id}.jpg`;
return m;
});
await fsPromises.writeFile(MEDIA_FILE, JSON.stringify({
data: newMedia,
paging: media.paging
}));
};
const downloadFile = async (url, id) => {
if (!url) {
return Promise.resolve();
}
const mediaFile = await axios({
url: url,
responseType: 'stream'
});
await pipeline(mediaFile.data, fs.createWriteStream(path.join(__dirname, `${id}.jpg`)));
};
(async () => {
if (process.env.NODE_ENV === 'test') {
return;
}
try {
await fetchAllMediaPages();
} catch(error) {
console.error(error);
process.exit(1);
}
})();
const thisModule = { igApiUrl, fetchAllMediaPages, saveRecentMedia, downloadFile, addGitHubUrlsToMediaJson, MEDIA_FILE };
module.exports = thisModule;