Skip to content
Permalink
Browse files

Added instagram search option

  • Loading branch information
codingmag committed Jun 22, 2016
1 parent 210e073 commit 818b4d1ccbe879e568ff50b9742dc96d863c3bad
Showing with 19,969 additions and 15,713 deletions.
  1. +53 −0 SocialMediaAdapters/InstagramAdapter.cs
  2. +60 −0 SocialMediaAdapters/InstagramOAuth.cs
  3. +62 −0 SocialMediaAdapters/SocialMediaAdapters.csproj
  4. +4 −0 SocialMediaAdapters/app.config
  5. +11 −0 SocialMediaAdapters/packages.config
  6. +41 −0 SocialMediaQuery/Controllers/ResultsController.cs
  7. +5 −1 SocialMediaQuery/Controllers/searchController.cs
  8. +14 −0 SocialMediaQuery/Models/Results/InstagramSearch.cs
  9. +41 −3 SocialMediaQuery/SocialMediaQuery.csproj
  10. +14 −5 SocialMediaQuery/Startup.cs
  11. +5 −0 SocialMediaQuery/Views/Results/InstagramAuth.cshtml
  12. +16 −0 SocialMediaQuery/Views/Results/InstagramSearch.cshtml
  13. +14 −3 SocialMediaQuery/Web.config
  14. +7 −1 SocialMediaQuery/packages.config
  15. BIN packages/Newtonsoft.Json.5.0.6/Newtonsoft.Json.5.0.6.nupkg
  16. BIN packages/Newtonsoft.Json.5.0.6/lib/net20/Newtonsoft.Json.dll
  17. BIN packages/Newtonsoft.Json.5.0.6/lib/net35/Newtonsoft.Json.dll
  18. BIN packages/Newtonsoft.Json.5.0.6/lib/net40/Newtonsoft.Json.dll
  19. BIN packages/Newtonsoft.Json.5.0.6/lib/net45/Newtonsoft.Json.dll
  20. BIN packages/Newtonsoft.Json.5.0.6/lib/netcore45/Newtonsoft.Json.dll
  21. BIN packages/Newtonsoft.Json.5.0.6/lib/portable-net40%2Bsl4%2Bwp7%2Bwin8/Newtonsoft.Json.dll
  22. +0 −7,457 packages/Newtonsoft.Json.5.0.6/lib/portable-net40%2Bsl4%2Bwp7%2Bwin8/Newtonsoft.Json.xml
  23. BIN packages/Newtonsoft.Json.5.0.6/lib/portable-net45%2Bwp80%2Bwin8/Newtonsoft.Json.dll
  24. +691 −145 packages/{Newtonsoft.Json.5.0.6 → Newtonsoft.Json.7.0.1}/lib/net20/Newtonsoft.Json.xml
  25. +832 −555 packages/{Newtonsoft.Json.5.0.6/lib/net40 → Newtonsoft.Json.7.0.1/lib/net35}/Newtonsoft.Json.xml
  26. +4,153 −3,569 packages/{Newtonsoft.Json.5.0.6/lib/net45 → Newtonsoft.Json.7.0.1/lib/net40}/Newtonsoft.Json.xml
  27. +4,170 −3,311 packages/{Newtonsoft.Json.5.0.6/lib/net35 → Newtonsoft.Json.7.0.1/lib/net45}/Newtonsoft.Json.xml
  28. +754 −516 ...lib/netcore45 → Newtonsoft.Json.7.0.1/lib/portable-net40+sl5+wp80+win8+wpa81}/Newtonsoft.Json.xml
  29. +732 −147 ...%2Bwin8 → Newtonsoft.Json.7.0.1/lib/portable-net45+wp80+win8+wpa81+dnxcore50}/Newtonsoft.Json.xml
  30. +8,290 −0 packages/Newtonsoft.Json.8.0.3/lib/portable-net40+sl5+wp80+win8+wpa81/Newtonsoft.Json.xml
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SocialMediaAdapters
{
using InstaSharp;
using InstaSharp.Models.Responses;

public static class InstagramAdapter
{
public static InstagramConfig InstagramConfig;

public static string GetLoginLink()
{
var scopes = new List<OAuth.Scope>();
scopes.Add(InstaSharp.OAuth.Scope.Public_Content);

return InstaSharp.OAuth.AuthLink(InstagramConfig.OAuthUri + "authorize", InstagramConfig.ClientId, InstagramConfig.RedirectUri, scopes, InstaSharp.OAuth.ResponseType.Code);
}

public static OAuthResponse GetOAuthResponse(string code)
{
var auth = new InstagramOAuth(InstagramConfig);
var oauthResponse = auth.RequestToken(code);

return oauthResponse;
}

public static async Task<string> SearchAsync(double latitude, double longitude, OAuthResponse oAuthResponse)
{
var media = new InstaSharp.Endpoints.Media(InstagramConfig, oAuthResponse);
var searchResults = await media.Search(latitude, longitude, 5000);

var serializedResults = new StringBuilder();

foreach (var resultItem in searchResults.Data)
{
serializedResults.AppendFormat(
"<blockquote class=\"twitter-tweet\"><p>{0}</p><p><a href=\"{1}\"><img src=\"{2}\"/>{3}</a> - {4}</p></blockquote>",
resultItem.Caption.Text,
resultItem.Images.StandardResolution.Url,
resultItem.Images.Thumbnail.Url,
resultItem.User.Username,
resultItem.CreatedTime.Date);
}

return serializedResults.ToString();
}
}
}
@@ -0,0 +1,60 @@
namespace SocialMediaAdapters
{
using System.Net.Http;

using InstaSharp;

using InstaSharp.Models.Responses;

using System;
using System.Collections.Specialized;
using System.Net;
using System.Threading.Tasks;

using InstaSharp.Models;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class InstagramOAuth : OAuth
{
private readonly InstagramConfig config;

public InstagramOAuth(InstagramConfig config) : base(config)
{
this.config = config;
}

public OAuthResponse RequestToken(string code)
{
NameValueCollection parameters = new NameValueCollection
{
{ "client_id", this.config.ClientId },
{ "client_secret", this.config.ClientSecret },
{ "grant_type", "authorization_code" },
{
"redirect_uri",
this.config.RedirectUri
},
{ "code", code }
};

var client = new WebClient();
var result = client.UploadValues("https://api.instagram.com/oauth/access_token", "POST", parameters);
var response = System.Text.Encoding.Default.GetString(result);
var responseObject = (JObject)JsonConvert.DeserializeObject(response);

return new OAuthResponse()
{
AccessToken = (string)responseObject["access_token"],
User = new UserInfo()
{
Id = Convert.ToInt64(responseObject["user"]["id"]),
Username = responseObject["user"]["username"].ToString(),
FullName = responseObject["user"]["full_name"].ToString(),
ProfilePicture = responseObject["user"]["profile_picture"].ToString()
}
};
}
}
}
@@ -30,6 +30,26 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="bouncy_castle_hmac_sha_pcl, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\BouncyCastle-PCL.1.0.0.6\lib\bouncy_castle_hmac_sha_pcl.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Google.Apis, Version=1.13.1.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
<HintPath>..\packages\Google.Apis.1.13.1\lib\net45\Google.Apis.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Google.Apis.Core, Version=1.13.1.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
<HintPath>..\packages\Google.Apis.Core.1.13.1\lib\net45\Google.Apis.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Google.Apis.PlatformServices, Version=1.13.1.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
<HintPath>..\packages\Google.Apis.1.13.1\lib\net45\Google.Apis.PlatformServices.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="InstaSharp, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\InstaSharp.2.0.4\lib\portable-net403+sl5+wp8+win8\InstaSharp.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="LinqToTwitter.AspNet, Version=3.2.0.30483, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\linqtotwitter.4.1.0\lib\net45\LinqToTwitter.AspNet.dll</HintPath>
<Private>True</Private>
@@ -38,12 +58,43 @@
<HintPath>..\packages\linqtotwitter.4.1.0\lib\net45\LinqToTwitter.net.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<HintPath>..\packages\log4net.2.0.3\lib\net40-full\log4net.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Threading.Tasks.Extensions, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Threading.Tasks.Extensions.Desktop, Version=1.0.168.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Extensions, Version=2.2.29.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Net.Http.2.2.29\lib\net45\System.Net.Http.Extensions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Http.Primitives, Version=4.2.29.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Net.Http.2.2.29\lib\net45\System.Net.Http.Primitives.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Reactive.Core, Version=2.2.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll</HintPath>
<Private>True</Private>
@@ -90,8 +141,14 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Zlib.Portable, Version=1.11.0.0, Culture=neutral, PublicKeyToken=431cba815f6a8b5b, processorArchitecture=MSIL">
<HintPath>..\packages\Zlib.Portable.Signed.1.11.0\lib\portable-net4+sl5+wp8+win8+wpa81+MonoTouch+MonoAndroid\Zlib.Portable.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="InstagramAdapter.cs" />
<Compile Include="InstagramOAuth.cs" />
<Compile Include="TwitterAdapter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
@@ -100,6 +157,11 @@
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
<Error Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
@@ -6,6 +6,10 @@
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.29.0" newVersion="4.2.29.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
@@ -1,13 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="BouncyCastle-PCL" version="1.0.0.6" targetFramework="net45" />
<package id="Google.Apis" version="1.13.1" targetFramework="net45" />
<package id="Google.Apis.Core" version="1.13.1" targetFramework="net45" />
<package id="InstaSharp" version="2.0.4" targetFramework="net45" />
<package id="linqtotwitter" version="4.1.0" targetFramework="net45" />
<package id="log4net" version="2.0.3" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc" version="5.1.2" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.1.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="3.1.2" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.10" targetFramework="net45" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" />
<package id="Microsoft.Net.Http" version="2.2.29" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />
<package id="Rx-Core" version="2.2.5" targetFramework="net45" />
<package id="Rx-Interfaces" version="2.2.5" targetFramework="net45" />
<package id="Rx-Linq" version="2.2.5" targetFramework="net45" />
<package id="Rx-Main" version="2.2.5" targetFramework="net45" />
<package id="Rx-PlatformServices" version="2.2.5" targetFramework="net45" />
<package id="Zlib.Portable.Signed" version="1.11.0" targetFramework="net45" />
</packages>
@@ -6,8 +6,11 @@

namespace SocialMediaQuery.Controllers
{
using System.Globalization;
using System.Threading.Tasks;

using InstaSharp;

using SocialMediaAdapters;

using SocialMediaQuery.Models.Results;
@@ -25,5 +28,43 @@ public async Task<ActionResult> TwitterSearchAsync(string query)

return this.View(model);
}

[ActionName("InstagramAuth")]
public async Task<ActionResult> InstagramAuthAsync(string query)
{
var consumerKey = System.Web.HttpContext.Current.Application["InstagramClientKey"].ToString();
var consumerSecret = System.Web.HttpContext.Current.Application["InstagramClientSecret"].ToString();
var redirecturl = this.Request.Url.AbsoluteUri.Replace("InstagramAuth", "InstagramSearch");
redirecturl = redirecturl.Replace(query, "?query=" + query);
InstagramAdapter.InstagramConfig = new InstagramConfig(
consumerKey,
consumerSecret,
redirecturl,
string.Empty);

return this.Redirect(InstagramAdapter.GetLoginLink());
}

[ActionName("InstagramSearch")]
public async Task<ActionResult> InstagramSearchAsync(string query, string code)
{
var oauthResponse = InstagramAdapter.GetOAuthResponse(code);

query = query.Replace(" ", string.Empty);

var model = new InstagramSearch() { Query = query };
if (oauthResponse == null || !query.Contains(',') || !query.ToLower().Contains("lat:") || !query.ToLower().Contains("lon:"))
{
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);

return this.View(model);
}
}
}
@@ -16,7 +16,7 @@ public class SearchController : Controller
public ActionResult Index()
{
var model = new Index();
model.DataSources = new List<DataSource>() { new DataSource() { Id = 1, Name = "Twitter" } };
model.DataSources = new List<DataSource>() { new DataSource() { Id = 1, Name = "Twitter" }, new DataSource() { Id = 2, Name = "Instagram" } };
return this.View(model);
}

@@ -26,6 +26,10 @@ public ActionResult RouteToController(string query, int dataSourceId)
{
return this.RedirectToAction("TwitterSearch", "Results", new { query = query });
}
else if (dataSourceId == 2)
{
return this.RedirectToAction("InstagramAuth", "Results", new { query = query });
}

return this.RedirectToAction("Index");
}
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SocialMediaQuery.Models.Results
{
public class InstagramSearch
{
public string Query { get; set; }

public string ResultsXml { get; set; }
}
}

0 comments on commit 818b4d1

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