Instagram Getting recent media from a location

Getting recent media from a location

Based on the ID of a location, you can use the GetRecentMedia method to get recent media from that location.

An example on how to use this method could look something like:

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

@{

    // Make the request to the API
    InstagramGetLocationRecentMediaResponse response = Model.Locations.GetRecentMedia(241201111);
    
    // Iterate through the media
    foreach (InstagramMedia media in response.Body.Data) {

        <div>
            <a href="@media.Link"><img src="@media.LowRes" /></a>
            <div>
                @media.Location.Name
            </div>
            <div>
                @media.CaptionText
            </div>
        </div>

    }

}

Pagination

The result for the recent media is paginated, so the example above will only return the first page. And while not documented, the method in the Instagram API seems to have a default limit of 20 if nothing else is specified.

To specify any options for the call to the API, we can specify an instance of the InstagramGetLocationRecentMediaOptions instead. This class lets you limit the amount of media returned on each page, as well as a few options for either time or ID based pagination.

ID based pagination is the recommended approach, since it is the most reliable. Also, time based pagination is no longer documented by the Instagram API, yet it stills works.

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

@{

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

    // Declare the initial options
    InstagramGetLocationRecentMediaOptions options = new InstagramGetLocationRecentMediaOptions {
        LocationId = 241201111,
        Count = 10
    };

    // Make the call to the API for the first page
    InstagramGetLocationRecentMediaResponse response = Model.Locations.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.MaxId = temp.Last().Id;

        // Make another call to the Instagram API
        response = Model.Locations.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>

}