Skip to content
Permalink
Browse files

Added google+ and YouTube search

  • Loading branch information
codingmag committed Jun 23, 2016
1 parent debbf8b commit 1506adde6159465a2a12857b3a17ab93b4863b8f
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SocialMediaAdapters
{
using System.ServiceModel.Discovery;
using System.Web.Mvc;
using System.Web.UI.WebControls;

using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Mvc;
using Google.Apis.Blogger.v3;
using Google.Apis.Plus.v1;
using Google.Apis.Services;
using Google.Apis.YouTube.v3;

public static class GoogleAdapter
{
public static string ApiKey;

public static string SearchGooglePlus(string query)
{
var service = new PlusService(new Google.Apis.Services.BaseClientService.Initializer() { ApiKey = ApiKey });
var activities = service.Activities.Search(query).Execute().Items;

var serializedResults = new StringBuilder();

foreach (var resultItem in activities)
{
serializedResults.AppendFormat(
"<blockquote class=\"twitter-tweet\"><p>{0}</p><p><a href=\"{1}\"><img src=\"{2}\"/>{3}</a> - {4}</p></blockquote>",
resultItem.Title,
resultItem.Url,
resultItem.Actor.Image.Url,
resultItem.Actor.DisplayName,
resultItem.Published);
}

return serializedResults.ToString();
}

public static string SearchYouTube(string query)
{
var service = new YouTubeService(new Google.Apis.Services.BaseClientService.Initializer() { ApiKey = ApiKey });
var videoRequest = service.Search.List("snippet");
videoRequest.Q = query;
videoRequest.MaxResults = 50;

var videos = videoRequest.Execute().Items;

var serializedResults = new StringBuilder();

foreach (var resultItem in videos)
{
serializedResults.AppendFormat(
"<blockquote class=\"twitter-tweet\"><p>{0}</p><p><a href=\"https://www.youtube.com/watch?v={1}\"><img src=\"{2}\"/>{3}</a> - {4}</p></blockquote>",
resultItem.Snippet.Title,
resultItem.Id.VideoId,
resultItem.Snippet.Thumbnails.Medium.Url,
resultItem.Snippet.ChannelTitle,
resultItem.Snippet.PublishedAt);
}

return serializedResults.ToString();
}
}
}
@@ -30,6 +30,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="BouncyCastle.Crypto, Version=1.7.4137.9688, Culture=neutral, PublicKeyToken=a4292a325f69b123, processorArchitecture=MSIL">
<HintPath>..\packages\BouncyCastle.1.7.0\lib\Net40-Client\BouncyCastle.Crypto.dll</HintPath>
<Private>True</Private>
</Reference>
<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>
@@ -38,6 +42,22 @@
<HintPath>..\packages\Google.Apis.1.13.1\lib\net45\Google.Apis.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Google.Apis.Auth, Version=1.13.1.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
<HintPath>..\packages\Google.Apis.Auth.1.13.1\lib\net45\Google.Apis.Auth.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Google.Apis.Auth.Mvc4, Version=1.13.1.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
<HintPath>..\packages\Google.Apis.Auth.Mvc.1.13.1\lib\net45\Google.Apis.Auth.Mvc4.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Google.Apis.Auth.PlatformServices, Version=1.13.1.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
<HintPath>..\packages\Google.Apis.Auth.1.13.1\lib\net45\Google.Apis.Auth.PlatformServices.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Google.Apis.Blogger.v3, Version=1.13.1.111, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
<HintPath>..\packages\Google.Apis.Blogger.v3.1.13.1.111\lib\portable-net45+netcore45+wpa81+wp8\Google.Apis.Blogger.v3.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>
@@ -46,6 +66,14 @@
<HintPath>..\packages\Google.Apis.1.13.1\lib\net45\Google.Apis.PlatformServices.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Google.Apis.Plus.v1, Version=1.13.1.534, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
<HintPath>..\packages\Google.Apis.Plus.v1.1.13.1.534\lib\portable-net45+netcore45+wpa81+wp8\Google.Apis.Plus.v1.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Google.Apis.YouTube.v3, Version=1.13.1.529, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
<HintPath>..\packages\Google.Apis.YouTube.v3.1.13.1.529\lib\portable-net45+netcore45+wpa81+wp8\Google.Apis.YouTube.v3.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>
@@ -111,6 +139,7 @@
<HintPath>..\packages\Rx-PlatformServices.2.2.5\lib\net45\System.Reactive.PlatformServices.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.ServiceModel.Discovery" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.1.2\lib\net45\System.Web.Helpers.dll</HintPath>
@@ -147,6 +176,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="GoogleAdapter.cs" />
<Compile Include="InstagramAdapter.cs" />
<Compile Include="InstagramOAuth.cs" />
<Compile Include="TwitterAdapter.cs" />
@@ -1,8 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="BouncyCastle" version="1.7.0" targetFramework="net45" />
<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.Auth" version="1.13.1" targetFramework="net45" />
<package id="Google.Apis.Auth.Mvc" version="1.13.1" targetFramework="net45" />
<package id="Google.Apis.Blogger.v3" version="1.13.1.111" targetFramework="net45" />
<package id="Google.Apis.Core" version="1.13.1" targetFramework="net45" />
<package id="Google.Apis.Plus.v1" version="1.13.1.534" targetFramework="net45" />
<package id="Google.Apis.YouTube.v3" version="1.13.1.529" 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" />
@@ -83,5 +83,39 @@ public async Task<ActionResult> InstagramSearchAsync(string query, string code)

return this.View(model);
}

public ActionResult GooglePlusSearch(string query)
{
var apiKey = System.Web.HttpContext.Current.Application["GoogleAPIKey"].ToString();
GoogleAdapter.ApiKey = apiKey;

var model = new GooglePlusSearch() { Query = query, ResultsXml = string.Empty };

if (string.IsNullOrEmpty(query))
{
return this.View(model);
}

model.ResultsXml = GoogleAdapter.SearchGooglePlus(query);

return this.View(model);
}

public ActionResult YouTubeSearch(string query)
{
var apiKey = System.Web.HttpContext.Current.Application["GoogleAPIKey"].ToString();
GoogleAdapter.ApiKey = apiKey;

var model = new YouTubeSearch() { Query = query, ResultsXml = string.Empty };

if (string.IsNullOrEmpty(query))
{
return this.View(model);
}

model.ResultsXml = GoogleAdapter.SearchYouTube(query);

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

public ActionResult RouteToController(string query, int dataSourceId)
{
if (dataSourceId == 1)
switch (dataSourceId)
{
return this.RedirectToAction("TwitterSearch", "Results", new { query = query });
}
else if (dataSourceId == 2)
{
return this.RedirectToAction("InstagramAuth", "Results", new { query = query });
case 1:
return this.RedirectToAction("TwitterSearch", "Results", new { query = query });
case 2:
return this.RedirectToAction("InstagramAuth", "Results", new { query = query });
case 3:
return this.RedirectToAction("GooglePlusSearch", "Results", new { query = query });
case 4:
return this.RedirectToAction("YouTubeSearch", "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 GooglePlusSearch
{
public string Query { get; set; }

public string ResultsXml { get; set; }
}
}
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

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

public string ResultsXml { get; set; }
}
}
@@ -170,8 +170,10 @@
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Models\DataSource.cs" />
<Compile Include="Models\Results\GooglePlusSearch.cs" />
<Compile Include="Models\Results\InstagramSearch.cs" />
<Compile Include="Models\Results\TwitterSearch.cs" />
<Compile Include="Models\Results\YouTubeSearch.cs" />
<Compile Include="Models\Search\Index.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Startup.cs" />
@@ -212,6 +214,8 @@
<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\GooglePlusSearch.cshtml" />
<Content Include="Views\Results\YouTubeSearch.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
@@ -26,6 +26,7 @@ public void Configuration(IAppBuilder app)
var twitterConsumerSecret = ConfigurationManager.AppSettings["TwitterConsumerSecret"];
var instagramConsumerKey = ConfigurationManager.AppSettings["InstagramClientKey"];
var instagramConsumerSecret = ConfigurationManager.AppSettings["InstagramClientSecret"];
var googleApiKey = ConfigurationManager.AppSettings["GoogleAPIKey"];

if (string.IsNullOrEmpty(twitterConsumerKey) || string.IsNullOrEmpty(twitterConsumerSecret))
{
@@ -37,10 +38,16 @@ public void Configuration(IAppBuilder app)
throw new NullReferenceException("Please check if InstagramClientKey and InstagramClientSecret values exist in the appSettings section of your web.config!");
}

if (string.IsNullOrEmpty(googleApiKey))
{
throw new NullReferenceException("Please check if GoogleApiKey exists in the appSettings section of your web.config!");
}

System.Web.HttpContext.Current.Application["TwitterConsumerKey"] = twitterConsumerKey;
System.Web.HttpContext.Current.Application["TwitterConsumerSecret"] = twitterConsumerSecret;
System.Web.HttpContext.Current.Application["InstagramClientKey"] = instagramConsumerKey;
System.Web.HttpContext.Current.Application["InstagramClientSecret"] = instagramConsumerSecret;
System.Web.HttpContext.Current.Application["GoogleAPIKey"] = googleApiKey;
}
}
}
@@ -0,0 +1,14 @@
@model SocialMediaQuery.Models.Results.GooglePlusSearch
@{
ViewBag.Title = "Google Plus Query Results Page";
}

<div class="jumbotron">
<h1>Google+ results for: @Model.Query</h1>
</div>

<div class="container-fluid">
<div class="row">
@Html.Raw(Model.ResultsXml)
</div>
</div>
@@ -0,0 +1,14 @@
@model SocialMediaQuery.Models.Results.YouTubeSearch
@{
ViewBag.Title = "YouTube Query Results Page";
}

<div class="jumbotron">
<h1>YouTube results for: @Model.Query</h1>
</div>

<div class="container-fluid">
<div class="row">
@Html.Raw(Model.ResultsXml)
</div>
</div>
@@ -13,6 +13,7 @@
<add key="TwitterConsumerSecret" value="Your Twitter Consumer Secret Here" />
<add key="InstagramClientKey" value="Your Instagram Client Key Here" />
<add key="InstagramClientSecret" value="Your Instagram Clent Secret Here" />
<add key="GoogleAPIKey" value="Your Google API Key Here" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />

0 comments on commit 1506add

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