Skip to content
Permalink
Browse files

Added recent tags search to instagram

  • Loading branch information
codingmag committed Jun 23, 2016
1 parent 009306d commit debbf8becddf75d1eb229cc7bda2234036b44f28
@@ -7,6 +7,7 @@
namespace SocialMediaAdapters
{
using InstaSharp;
using InstaSharp.Models;
using InstaSharp.Models.Responses;

public static class InstagramAdapter
@@ -34,9 +35,22 @@ public static async Task<string> SearchAsync(double latitude, double longitude,
var media = new InstaSharp.Endpoints.Media(InstagramConfig, oAuthResponse);
var searchResults = await media.Search(latitude, longitude, 5000);

return SerializeResults(searchResults.Data);
}

public static async Task<string> TagsAsync(string query, OAuthResponse oAuthResponse)
{
var tags = new InstaSharp.Endpoints.Tags(InstagramConfig, oAuthResponse);
var searchResults = await tags.Recent(query);

return SerializeResults(searchResults.Data);
}

private static string SerializeResults(List<Media> mediaList)
{
var serializedResults = new StringBuilder();

foreach (var resultItem in searchResults.Data)
foreach (var resultItem in mediaList)
{
serializedResults.AppendFormat(
"<blockquote class=\"twitter-tweet\"><p>{0}</p><p><a href=\"{1}\"><img src=\"{2}\"/>{3}</a> - {4}</p></blockquote>",
@@ -48,21 +48,38 @@ public async Task<ActionResult> InstagramAuthAsync(string query)
[ActionName("InstagramSearch")]
public async Task<ActionResult> InstagramSearchAsync(string query, string code)
{
var oauthResponse = InstagramAdapter.GetOAuthResponse(code);
var model = new InstagramSearch() { Query = query, ResultsXml = string.Empty };

query = query.Replace(" ", string.Empty);
if (string.IsNullOrEmpty(query))
{
return this.View(model);
}

var model = new InstagramSearch() { Query = query };
if (oauthResponse == null || !query.Contains(',') || !query.ToLower().Contains("lat:") || !query.ToLower().Contains("lon:"))
query = query.Replace(" ", string.Empty);

var oauthResponse = InstagramAdapter.GetOAuthResponse(code);

if (oauthResponse == null || oauthResponse.User == null || oauthResponse.AccessToken == null)
{
model.ResultsXml = string.Empty;
return this.View(model);
}

var coordinates = query.Split(',');
var lat = Convert.ToDouble(coordinates[0].Replace("lat:", string.Empty), CultureInfo.InvariantCulture);
var lon = Convert.ToDouble(coordinates[1].Replace("lon:", string.Empty), CultureInfo.InvariantCulture);
model.ResultsXml = await InstagramAdapter.SearchAsync(lat, lon, oauthResponse);
if (query.StartsWith("lat:", StringComparison.InvariantCulture))
{
if (!query.Contains(',') || !query.ToLower().Contains("lat:") || !query.ToLower().Contains("lon:"))
{
return this.View(model);
}

var coordinates = query.Split(',');
var lat = Convert.ToDouble(coordinates[0].Replace("lat:", string.Empty), CultureInfo.InvariantCulture);
var lon = Convert.ToDouble(coordinates[1].Replace("lon:", string.Empty), CultureInfo.InvariantCulture);
model.ResultsXml = await InstagramAdapter.SearchAsync(lat, lon, oauthResponse);
}
else
{
model.ResultsXml = await InstagramAdapter.TagsAsync(query, oauthResponse);
}

return this.View(model);
}
@@ -212,7 +212,6 @@
<Content Include="Scripts\jquery-1.10.2.min.map" />
<Content Include="Views\Results\TwitterSearch.cshtml" />
<Content Include="Views\Results\InstagramSearch.cshtml" />
<Content Include="Views\Results\InstagramAuth.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />

This file was deleted.

@@ -1,6 +1,6 @@
@model SocialMediaQuery.Models.Results.TwitterSearch
@{
ViewBag.Title = "Twitter Query Results Page";
ViewBag.Title = "Instagram Query Results Page";
}

<div class="jumbotron">

0 comments on commit debbf8b

Please sign in to comment.
You can’t perform that action at this time.