-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathig-latest-post.js
510 lines (436 loc) · 14.8 KB
/
ig-latest-post.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: red; icon-glyph: camera-retro;
/* -----------------------------------------------
Script : ig-latest-post.js
Author : me@supermamon.com
Version : 2.0.1
Description :
Displays the latest instagram post of a selected
user or users. Tap the widget to open the
Instagram post in the app
Changelog:
v2.0.1 - Fixed "TypeError: null is not an object (evaluating 'req.response.cookies.forEach')"
v2.0.0 - Fixed 'format' error
- Now works with private users
- Now works in restricted regions
v1.3.0 - Pick the highest resolution photo
v1.2.0 - Option to pick up to 12 of the most
recent posts
v1.1.0 - Options to show likes and comments count
v1.0.0 - Initial release
----------------------------------------------- */
const DEBUG = false
const log = (args) => { if (DEBUG) {console.log(args)}}
// The script randomly chooses from this list of
// users. If a list if users is passed as a
// parameter on the widget configuration screen,
// it uses those instead. The list of users in the
// configuration screen must be comma-separated
const USERS = [
'beautifuldestinations',
'natgeotravel',
'igersmanila',
'cntraveler',
'the_philippines',
'nasachandraxray'
]
// stuff to display at the bottom of the widget
const SHOW_USERNAME = true
const SHOW_LIKES = true
const SHOW_COMMENTS = true
// pick up to 12 of the most recent posts and
// select randomly between those.
const MAX_RECENT_POSTS = 12
// desired interval in minutes to refresh the
// widget. This will only tell IOS that it's
// ready for a refresh, whether it actually
// refreshes is up to IOS
const REFRESH_INTERVAL = 5 //mins
// DO NOT EDIT BEYOND THIS LINE ------------------
// InstagramClient module ------------------------
// const InstagramClient = importModule('InstagramClient')
// EMBED
const InstagramClient = {
//----------------------------------------------
initialize() {
this.USES_ICLOUD = module.filename.includes('Documents/iCloud~')
this.fm = this.USES_ICLOUD ? FileManager.iCloud() : FileManager.local()
// track the number of login attempts
// so we don't get an infinite login screen
this.loginAttempts = 0
this.MAX_ATTEMPTS = 2
this.root = this.fm.joinPath(this.fm.documentsDirectory(), '/cache/igclient')
this.fm.createDirectory(this.root, true)
this.sessionPath = this.fm.joinPath(this.root, 'session.json')
this.sessionid = ''
},
//----------------------------------------------
async authenticate() {
let url = 'https://instagram.com/'
let req = new Request(url)
await req.load()
let result = {}
if (req.response.cookies) {
if (Array.isArray(req.response.cookies)) {
req.response.cookies.forEach(cookie => {
if (cookie.name == 'sessionid') {
result.sessionid = cookie.value;
result.expiresDate = cookie.expiresDate
result.cookies = req.response.cookies
}
})
}
}
if (!result.sessionid) {
if (this.loginAttempts < this.MAX_ATTEMPTS) {
this.loginAttempts++;
var resp = await this.presentAlert("You will now be presented with the Instagram login window.\nAuthentication happens on the Instagram website and your credentials will neither be captured nor stored.", ["Proceed", "Cancel"])
if (resp==1) {
this.loginAttempts = this.MAX_ATTEMPTS
throw new Error("login was cancelled")
return
}
let webview = new WebView()
await webview.loadURL(url)
await webview.present(false)
return await this.authenticate()
} else {
throw new Error('Maximum number of login attempts reached. Please launch the script again.')
}
} else {
result.cookies = req.response.cookies
await this.saveSession(result)
this.sessionid = result.sessionid
return result
}
},
//----------------------------------------------
async logout() {
log(`session exists - ${this.fm.fileExists(this.sessionPath)}`)
if (this.fm.fileExists(this.sessionPath)) {
log('deleting session file')
await this.fm.remove(this.sessionPath)
}
log('logging out')
var url = 'https://www.instagram.com/accounts/logout'
let webview = new WebView()
await webview.loadURL(url)
//await webview.present(false)
},
//----------------------------------------------
async startSession() {
var sessionCache = await this.readSession()
if (sessionCache) {
log(`cached sessionid ${sessionCache.sessionid}`)
log(`session expires on ${new Date(sessionCache.expiresDate)}`)
}
if (!sessionCache || new Date() >= new Date(sessionCache.expiresDate)) {
log('refreshing session cache');
sessionCache = await this.authenticate()
}
if (sessionCache) {
this.sessionid = sessionCache.sessionid
return InstagramClient
} else {
return null
}
},
//----------------------------------------------
async fetchData(url) {
log(`fetching ${url}`)
let req = new Request(url)
var cookies = await this.getCookies()
log(cookies)
req.headers = {
Cookie: `${cookies}`
}
try {
//var response = await req.loadJSON()
var response = await req.loadString()
log(response)
response = JSON.parse(response)
return response
} catch (e) {
throw new Error(e.message)
}
},
//----------------------------------------------
async getUserInfo(username) {
const url = `https://www.instagram.com/${username}/?__a=1`
const response = await this.fetchData(url)
if (Object.keys(response).length == 0) {
throw new Error(`Invalid user - ${username}`)
}
var user = response.graphql.user
return user
},
//----------------------------------------------
async getPostInfo(shortcode) {
const url = `https://www.instagram.com/p/${shortcode}/?__a=1`
const response = await this.fetchData(url)
if (Object.keys(response).length == 0) {
throw new Error(`Invalid post`)
}
return response
},
//----------------------------------------------
async readSession() {
log('reading session')
if (this.fm.fileExists(this.sessionPath)) {
log(`file found`)
if (this.USES_ICLOUD) {
await this.fm.downloadFileFromiCloud(this.sessionPath)
}
log(`reading session file`)
let result = await this.fm.read(this.sessionPath)
if (!result || !result.toRawString()) {
log(`error reading file`)
return undefined
} else {
var session = JSON.parse(result.toRawString())
//log(this.session)
return session
}
}
return undefined
},
//----------------------------------------------
async saveSession(json) {
if (this.fm.fileExists(this.sessionPath)) {
if (this.USES_ICLOUD) {
await this.fm.downloadFileFromiCloud(this.sessionPath)
}
}
await this.fm.writeString(this.sessionPath, JSON.stringify(json))
},
async getCookies() {
var session = await this.readSession()
log(session)
var cookies = session.cookies.map( cookie => {
log(`adding cookie ${cookie.name}`)
return `${cookie.name}=${cookie.value}`
}).join(';')
log(`returning cookies = ${cookies}`)
return cookies
},
//----------------------------------------------
async presentAlert(prompt="", items=["OK"], asSheet=false) {
let alert = new Alert()
alert.message = prompt
for (var n=0; n<items.length;n++) {
alert.addAction(items[n])
}
let resp = asSheet ? await alert.presentSheet() : await alert.presentAlert()
return resp
}
}
// InstagramClient module ends -------------------
// Wisget code -----------------------------------
InstagramClient.initialize()
//await InstagramClient.logout()
// only show the staus line is any of the
// status items are visible
const SHOW_STATUS_LINE = SHOW_USERNAME ||
SHOW_LIKES ||
SHOW_COMMENTS
// get usernames from the arguments if passed
let usernames = args.widgetParameter || USERS.join(',')
usernames = usernames.split(',')
// choose a random username and fetch for the user
// information
const username = getRandom(usernames)
const post = await getLatestPost(username,
MAX_RECENT_POSTS)
if (config.runsInWidget) {
let widget = post.has_error ?
await createErrorWidget(post) :
await createWidget(post)
Script.setWidget(widget)
} else {
const options = ['Small', 'Medium', 'Large', 'Cancel']
let resp = await presentAlert('Preview Widget', options)
if (resp==options.length-1) return
let size = options[resp]
let widget = post.has_error ?
await createErrorWidget(post) :
await createWidget(post, size.toLowerCase())
await widget[`present${size}`]()
}
Script.complete()
//------------------------------------------------
async function createWidget(data, widgetFamily) {
widgetFamily = widgetFamily || config.widgetFamily
const padd = widgetFamily=='large' ? 12 : 10
const fontSize = widgetFamily=='large' ? 14 : 10
const img = await download('Image', data.display_url)
const url = `https://www.instagram.com/p/${data.shortcode}`
const widget = new ListWidget()
var refreshDate = Date.now() + 1000*60*REFRESH_INTERVAL
widget.refreshAfterDate = new Date(refreshDate)
widget.url = url
widget.setPadding(padd,padd,padd,padd)
widget.backgroundImage = img
if (SHOW_STATUS_LINE) {
// add gradient with a semi-transparent
// dark section at the bottom. this helps
// with the readability of the status line
widget.backgroundGradient = newLinearGradient(
['#ffffff00','#ffffff00','#00000088'],
[0,.75,1])
// top spacer to push the bottom stack down
widget.addSpacer()
// horizontal stack to hold the status line
const stats = widget.addStack()
stats.layoutHorizontally()
stats.centerAlignContent()
stats.spacing = 3
if (SHOW_USERNAME) {
const eUsr = addText(stats, `@${data.username}`,'left', fontSize)
}
// center spacer to push items to the sides
stats.addSpacer()
if (SHOW_LIKES) {
const heart = addSymbol(stats, 'heart.fill', fontSize)
const likes = abbreviateNumber(data.likes)
const eLikes = addText(stats, likes, 'right', fontSize)
}
if (SHOW_COMMENTS) {
const msg = addSymbol(stats, 'message.fill', fontSize)
const comments = abbreviateNumber(data.comments)
const eComm = addText(stats, comments, 'right', fontSize)
}
}
return widget
}
//------------------------------------------------
function addSymbol(container, name, size) {
const sfIcon = SFSymbol.named(name)
const fIcon = sfIcon.image
const icon = container.addImage(fIcon)
icon.tintColor = Color.white()
icon.imageSize = new Size(size,size)
return icon
}
//------------------------------------------------
function addText(container, text, align, size) {
const txt = container.addText(text)
txt[`${align}AlignText`]()
txt.font = Font.systemFont(size)
txt.shadowRadius = 3
txt.textColor = Color.white()
txt.shadowColor = Color.black()
}
//------------------------------------------------
function getRandom(array) {
return array[Math.floor(Math.random() * array.length)];
}
//------------------------------------------------
function newLinearGradient(hexcolors, locations) {
let gradient = new LinearGradient()
gradient.locations = locations
gradient.colors = hexcolors
.map(color=>new Color(color))
return gradient
}
//------------------------------------------------
async function createErrorWidget(data) {
const widget = new ListWidget()
widget.addSpacer()
const text = widget.addText(data.message)
log(data.message)
text.textColor = Color.white()
text.centerAlignText()
widget.addSpacer()
return widget
}
//------------------------------------------------
async function download(dType, url) {
const req = new Request(url)
return await req[`load${dType}`](url)
}
//------------------------------------------------
async function getLatestPost(username, maxRecent) {
try {
var igSession = await InstagramClient.startSession()
} catch (e) {
log(`error encountered - ${e.message}`)
return {
has_error: true,
message: e.message
}
}
try {
var user = await InstagramClient.getUserInfo(username)
log(`user`)
} catch(e) {
log(`error encountered - ${e.message}`)
return {
has_error: true,
message: e.message
}
}
if (user.is_private && !user.followed_by_viewer) {
return {
has_error: true,
message: `not following user\n${username}`
}
}
maxRecent = maxRecent > 12 ? 12 : maxRecent
let idx = Math.floor(Math.random() * maxRecent)
var visible_posts = user.edge_owner_to_timeline_media.edges.length-1
idx = visible_posts < idx ? visible_posts : idx
const rec = user.edge_owner_to_timeline_media.edges[idx].node
try {
var resp = await InstagramClient.getPostInfo(rec.shortcode)
log(resp)
} catch(e) {
log(`error encountered when getting post - ${e.message}`)
return {
has_error: true,
message: e.message
}
}
const post = resp.graphql.shortcode_media;
let media_url = post.display_url
if (post.hasOwnProperty('display_resources')) {
log('has hi-res versions')
media_url = post.display_resources[post.display_resources.length-1].src
}
return {
has_error: false,
username: username,
shortcode: post.shortcode,
display_url: media_url,
is_video: post.is_video,
comments: post.edge_media_preview_comment.count,
likes: post.edge_media_preview_like.count
}
}
//------------------------------------------------
async function presentAlert(prompt,items,asSheet)
{
let alert = new Alert()
alert.message = prompt
for (const item of items) {
alert.addAction(item)
}
let resp = asSheet ?
await alert.presentSheet() :
await alert.presentAlert()
return resp
}
//------------------------------------------------
// found on : https://stackoverflow.com/a/32638472
// thanks @D.Deriso
function abbreviateNumber(num, fixed) {
if (num === null) { return null; } // terminate early
if (num === 0) { return '0'; } // terminate early
fixed = (!fixed || fixed < 0) ? 0 : fixed; // number of decimal places to show
var b = (num).toPrecision(2).split("e"), // get power
k = b.length === 1 ? 0 : Math.floor(Math.min(b[1].slice(1), 14) / 3), // floor at decimals, ceiling at trillions
c = k < 1 ? num.toFixed(0 + fixed) : (num / Math.pow(10, k * 3) ).toFixed(1 + fixed), // divide by power
d = c < 0 ? c : Math.abs(c), // enforce -0 is 0
e = d + ['', 'K', 'M', 'B', 'T'][k]; // append power
return e;
}