Example: Get Instagram stats and likes on last photo

Just some lazy script I wrote based on html that shows number of posts, number of followers, number of accounts following and number of likes on last photo that was uploaded. It also shows this last photo. I’m pretty sure there is a better way of doing this but I’m just a beginner with JS so I’m still playing around.

// Gets page url
let url = "https://www.instagram.com/nkuutz/"
let req = new Request(url)
let html = await req.loadString()

// Starts table
let table = new UITable()

// Gets posts count
let postsStart = html.indexOf('"edge_owner_to_timeline_media":{"count":')
let postsEnd = html.indexOf(',"page_info":',postsStart+1)
let posts = html.substring(postsStart+40,postsEnd)

// Gets follower count
let followersStart = html.indexOf('"edge_followed_by":{"count":')
let followersEnd = html.indexOf('},"followed_by_viewer"',followersStart+1)
let followers = html.substring(followersStart+28,followersEnd)

// Gets following count
let followingStart = html.indexOf('"edge_follow":{"count":')
let followingEnd = html.indexOf('},"follows_viewer":',followingStart+1)
let following = html.substring(followingStart+23,followingEnd)

// Gets last post image
let imageStart = html.indexOf('"display_url":"')
let imageEnd = html.indexOf('","edge_liked_by":',imageStart+1)
let image = html.substring(imageStart+15,imageEnd)

// Gets last post likes
let likesStart = html.indexOf('"edge_liked_by":{"count":')
let likesEnd = html.indexOf('},"edge_media_preview_like"',likesStart+1)
let likes = html.substring(likesStart+25,likesEnd)

styleHTML ='<style>img {display: block;margin-left: auto;margin-right: auto;} td {font-size:55px; text-align:center; font-family:Helvetica; font-weight:300; } th {font-size:30px; font-family:Helvetica; font-weight:500}</style>'

statsHTML ='<table id="statsTable" style="width:75%" align="center"><tr><th>Posts</th><th>Followers</th><th>Following</th><th>Likes</th></tr><tr><td>'+posts+'</td><td>'+followers+'</td><td>'+following+'</td><td>'+likes+'</td></tr></table>'

imageHTML = '<img src="'+image+'" style="width:75%;" vspace="10"/>'

joinedHTML = styleHTML+statsHTML+imageHTML

if (config.runsWithSiri) {
  Speech.speak("Your last photo got "+likes+" likes on Instagram.")
}

WebView.loadHTML(joinedHTML,null,new Size(100,450))

Edit: added some space between stats and image.
Edit 2: changed .load() to .loadString()

3 Likes

Wow, this is really nice, thanks for sharing!

I’ve just copied this into Scriptable, but I get the following error unfortunately:

Error on line 34: SyntaxError: Unexpected EOF

Have I done something wrong?

Thanks again for sharing this, really smart idea!

1 Like

Change line 4.

let html = await req.load()

to

let html = await req.loadString()

The error is about the object not being a string so it doesn’t have that string fucntion defined for it.

Thanks @sylumer

I did try that, after reading that this had been changed in a recent beta, but I still get the error unfortunately.

Kind regards,

Andy

1 Like

Very strange.

I copied the script from the page when I read your post, tried it as is, reproduced the error, changed that line, reran it and I don’t get the error any more.

2 Likes

Okay, I’m a moron …

I’m at work currently, so I copied the script and emailed it to myself, and then copy and pasted out of iOS mail and … forgot to include the last line of the code! :roll_eyes:

Oops! I’ve added that last line and am now golden!

Thanks @sylumer

2 Likes

I’m glad you were able to solve it! I’ve edited the post so it works with the changes made to the API. Enjoy!

1 Like

This script doesn’t work anymore… the picture doesn’t load… someone know why ? And how to correct the code to see the picture ?