Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve video view count from Instagram API

Instagram recently started showing view counts on videos. Is there a way to pull this data from the API?

I read through the documentation but I could not find anything about "Views" only "Likes".

like image 406
Chris Avatar asked Feb 28 '16 18:02

Chris


3 Answers

Not available via public API yet.

like image 194
Reza_Rg Avatar answered Oct 08 '22 00:10

Reza_Rg


The only way I've found is to systematically scrape posts' permalinks using browser automation like Seleniuim (with some logic handling the formatting e.g. 5.6k views vs 1,046 views) and picking out the appropriate element. A simple GET request doesn't yield the desired DOM due to the lack of javascript detected.

In python:

from bs4 import BeautifulSoup
from selenium import webdriver

def insertViews(posts):
    driver = webdriver.PhantomJS('<path-to-phantomjs-driver-ignoring-escapes>')
    views_span_dom_path = '._9jphp > span'

    for post in posts:
        post_type = post.get('Type')
        link = post.get('Link')
        views = post.get('Views')

        if post_type == 'video':
            driver.get(link)
            html = driver.page_source

            soup = BeautifulSoup(html, "lxml")
            views_string_results = soup.select(views_span_dom_path)
            if len(views_string_results) > 0:
                views_string = views_string_results[0].get_text()
            if 'k' in views_string:
                views = float(views_string.replace('k', '')) * 1000
            elif ',' in views_string:
                views = float(views_string.replace(',', ''))
            elif 'k' not in views_string and ',' not in views_string:
                views = float(views_string)
        else:
            views = None

        post['Views'] = views
    driver.quit()
    return posts

The PhantomJS driver can be downloaded here.

like image 20
jpryda Avatar answered Oct 08 '22 00:10

jpryda


Yes you can get it, if you have your facebook and instagram accounts linked and your instagram account has a business profile, make following GET request:

https://graph.facebook.com/v3.0/instagram_video_id/insights/video_views

you will get response in this format:

{
  "data": [
    {
      "name": "video_views",
      "period": "lifetime",
      "values": [
        {
          "value": 123
        }
      ],
      "title": "Video Views",
      "description": "Total number of times the video has been seen",
      "id": "instagram_video_id/insights/video_views/lifetime"
    }
  ]
}
like image 38
Rahul Avatar answered Oct 07 '22 22:10

Rahul