This repository was archived by the owner on Oct 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
126 lines (112 loc) · 3.49 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
let fs = require('fs')
let dp = require('despair')
let Corrin = require('corrin')
let cfg = config('config.json')
let IG = {
/*
getUser: async name => {
let body = await dp('https://www.instagram.com/web/search/topsearch', {
query: { query: name }
}).json()
if (!body.users.length) die(`Could not find any profiles matching '${name}'`)
let user = body.users[0].user
return {
id: user.pk,
name: user.full_name || user.username,
avatar: user.profile_pic_url
}
},
*/
getMedia: async (id, num) => {
let body = await dp('https://www.instagram.com/graphql/query/', {
query: {
query_hash: '58b6785bea111c67129decbe6a448951',
variables: JSON.stringify({ id: id, first: num })
}
}).json()
return body.data.user.edge_owner_to_timeline_media.edges.map(node => {
let item = node.node
return {
id: item.id,
video: item.is_video ? item.video_url : null,
url: item.display_url,
link: 'https://instagram.com/p/' + item.shortcode,
caption: item.edge_media_to_caption.edges[0] ? item.edge_media_to_caption.edges[0].node.text : '',
timestamp: new Date(item.taken_at_timestamp * 1000)
}
})
}
}
function config (file) {
if (!fs.existsSync(file)) die(`${file} not present!`)
let cfg = null
try {
cfg = JSON.parse(fs.readFileSync(file, { encoding: 'utf-8' }))
} catch (e) { die(`Could not parse ${file}!`) }
if (!cfg.legacyMode) cfg.legacyMode = {}
if (!cfg.overrides) cfg.overrides = {}
if (process.env.ID) cfg.id = process.env.ID
if (process.env.HOOK) cfg.hook = process.env.HOOK
if (process.env.INTERVAL) cfg.interval = process.env.INTERVAL
if (process.env.LEGACY) cfg.legacyMode.enabled = true
if (process.env.COLOR) cfg.legacyMode.color = process.env.COLOR
if (process.env.NAME) cfg.overrides.name = process.env.NAME
if (process.env.AVATAR) cfg.overrides.avatar = process.env.AVATAR
if (!cfg.id) die(`${file} is missing the "id" property!`)
if (!cfg.hook) die(`${file} is missing the "hook" property!`)
return cfg
}
function body (user) {
let res = { embeds: [] }
if (cfg.overrides.enabled) {
res.username = cfg.overrides.name
res.avatar_url = cfg.overrides.avatar
}
return res
}
function embed (item) {
let res = {
title: `${cfg.name} posted on Instagram!`,
color: cfg.legacyMode.color || undefined,
url: item.link,
timestamp: item.timestamp,
image: { url: item.url }
}
if (item.caption) res.title = item.caption
return res
}
function send (data) {
if (!cfg.hook) return
try {
dp.post(cfg.hook, {
data: data,
type: 'json'
}).catch(e => die(e.message))
} catch (e) { die(e.message) }
}
function notify (res) {
if (!res || !res.embeds) return
for (let i = 0; i < res.embeds.length; i++) {
console.log(`[${res.embeds[i].timestamp.toISOString()}] - ${res.embeds[i].url}`)
}
}
function die (err) {
console.error(`\x1b[31mError: ${err}\x1b[0m`)
process.exit(1)
}
async function main () {
let user = { id: cfg.id } // await IG.getUser(cfg.name)
let feed = new Corrin([() => IG.getMedia(user.id, 10), x => x.id], cfg.interval)
console.log(`Tracking Instagram posts of ${cfg.id}...`)
feed.on('new', async items => {
let res = body(user)
items['0'].forEach(item => res.embeds.push(embed(item)))
if (cfg.logging) notify(res)
if (!cfg.legacyMode.enabled) {
res.content = res.embeds.map(x => x.url).join('\n')
delete res.embeds
}
send(res)
})
}
main()