Instagram Getting recent media from a tag

Getting recent media from a tag

Given that you already have an instance of the InstagramService class, you can get a list of the most recent media from a given tag like:

@using Skybrud.Social.Instagram.Objects.Media
@using Skybrud.Social.Instagram.Responses.Media
@inherits WebViewPage<Skybrud.Social.Instagram.InstagramService>

@{

    // Make the call to the API
    InstagramGetRecentMediaResponse response = Model.Tags.GetRecentMedia("skybruddk");
    
    // Iterate through the media
    <table class="table list">
        @{
            int nr = 1;
            foreach (InstagramMedia media in response.Body.Data) {
                <tr>
                    <td><strong>@(nr++).</strong></td>
                    <td><code>@media.Id</code></td>
                    <td class="nw">@media.CreatedTime.ToLocalTime()</td>
                    <td>
                        <img src="@media.Thumbnail" style="float: left;" />
                        <div style="margin-left: 160px;">
                            @media.CaptionText
                        </div>
                    </td>
                </tr>
            }
        }
    </table>

}

The example above will give you the 20 most recent media of that tag. The Instagram API documentation doesn't specify a maximum value, but it appears to be 33 (for whatever reason).

To request more (or less) media than the default 20 - or to request the next page, your code could instead look like this:

@using Skybrud.Social.Instagram.Objects.Media
@using Skybrud.Social.Instagram.Options.Tags
@using Skybrud.Social.Instagram.Responses.Media
@inherits WebViewPage<Skybrud.Social.Instagram.InstagramService>

@{

    // Temporary list for storing the retrieved media
    List<InstagramMedia> temp = new List<InstagramMedia>();

    // Declare the initial options
    InstagramGetTagRecentMediaOptions options = new InstagramGetTagRecentMediaOptions {
        Tag = "skybruddk",
        Count = 10
    };
    
    // Make the call to the API for the first page
    InstagramGetRecentMediaResponse response = Model.Tags.GetRecentMedia(options);

    // Add the media to the list
    temp.AddRange(response.Body.Data);

    // No reason to make another request if the first request didn't return any media
    if (temp.Count == 0) {

        // Update the options
        options.MaxTagId = response.Body.Pagination.NextMaxId;

        // Make another call to the Instagram API
        response = Model.Tags.GetRecentMedia(options);

        // Add the media to the list
        temp.AddRange(response.Body.Data);

    }
    
    // Iterate through the media
    <table class="table list">
        @{
            int nr = 1;
            foreach (InstagramMedia media in temp) {
                <tr>
                    <td><strong>@(nr++).</strong></td>
                    <td><code>@media.Id</code></td>
                    <td class="nw">@media.CreatedTime.ToLocalTime()</td>
                    <td>
                        <img src="@media.Thumbnail" style="float: left;" />
                        <div style="margin-left: 160px;">
                            @media.CaptionText
                        </div>
                    </td>
                </tr>
            }
        }
    </table>

}

In the first example, we simply specified the name of the tag as the first parameter for the GetRecentMedia method. The method does however support a few different overloads - eg. one that lets you specify an instance of InstagramGetTagRecentMediaOptions.

This class has a number of properties that gives you better control of the parameters that are sent to the Instagram API. Like the GetRecentMedia method, this class also supports a number of different constructor overloads.