Searching using the Google custom search API and displaying links - c#

I am working on a personal assistant for home automation and so far it has basic features such as searching wolfram alpha and pulling weather conditions/forecasts but I wan't to enable it to search for things on google and display the results on screen.
After searching around the community it seems the recommended way is to use the Google Search API (which has been replaced with Google Custom Search API. So I have looked at some examples and am able to get the data out into a data grid on the windows form however. I want to show clickable links. How can I do this? I already have an API key and CX to use with the code but cannot get the proper output.
GoogleSearch search = new GoogleSearch()
{
Key = "KEY HERE",
CX = "CX HERE"
};
search.SearchCompleted += (a, b) =>
{
this.DataGridResults.ItemsSource = b.Response.Items;
};
search.Search(search_query.Text);

So I solved this problem after working on it for a long time. Turns out I was just using the list the method returned wrong. I attached a link to the original post that gave me the method and my completed solution which just outputs the titles and HTML links in a text box. You can do whatever you like with them from there.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
GoogleSearch search = new GoogleSearch()
{
Key = "API KEY HERE",
CX = "CX GOES HERE"
};
search.SearchCompleted += (a, b) =>
{
foreach (Item i in b.Response.Items)
{
results_box.Text = results_box.Text + Environment.NewLine + "Page Title: " + i.Title;
results_box.Text = results_box.Text + Environment.NewLine + "Link to Page " + i.Link;
};
};
search.Search(search_query.Text);
The method and original post can be found at http://kiwigis.blogspot.com/2011/03/google-custom-search-in-c.html

Related

Search for RSS Feeds on Google

I hope I am not just chasing a red herring here. I have seen some websites that you are able to search for RSS feeds by typing in some sort of term like "Technology news" and it would return a number of different feeds that you can chose from.
Most look to be where they are just searching their own curated database which is all fine and dandy, however there is one that looks like it uses Google to search for them. http://ctrlq.org/rss/
Does anyone know how this could be done and point me in the right direction to learn how it is done as it is bugging the life out of me? I have done a lot of searching but most seem to point to the depreciated Google Feed API that no longer works or using Google Alerts to create an RSS Feed which I am not wanting to do.
Ideally I would like to do this in C# so that I can easily deal with the results and save the relevant selected option in a database.
It also doesn't need to be Google that it is done in, if there are other options that are available then great :)
Cheers.
I was kinda intrigue by your question and this is what I've find out. First of all I went to the site http://ctrlq.org/rss/ and checked what is done after click on Search button:
function findfeeds() {
var q = $.trim($('#feedQuery').val());
if(q == "") {
resetfeeds();
return false;
}
$('#pleasewait').show();
google.feeds.findFeeds(q, function(result) {
if (!result.error) {
var html = '';
for (var i = 0; i < result.entries.length; i++) {
var entry = result.entries[i];
feedList[i] = entry.url;
var count = i+1;
html += '<div id="feed-' + i + '">';
html += ' <h3><img src="//s2.googleusercontent.com/s2/favicons?domain=' + entry.link + '"/> <a target="_blank" href="' + entry.link + '">' + removeHTMLTags(entry.title) + '</a></h3>';
html += ' <p class="snippet">' + removeHTMLTags(entry.contentSnippet) + '</p>';
html += ' <p class="feedURL">';
html += 'RSS Feed ⋅ ';
html += ' <span class="showhide" rel="' + i + '">Preview Feed</span></p>';
html += ' <div id="feedcontent-' + i + '"></div>';
html += '</div>';
}
$("#results").fadeOut('slow', function() {
$('html, body').animate({scrollTop:0}, 'slow');
$("#results").empty();
$("#results").append(html);
$("#results").show();
});
}
$('#pleasewait').hide();
});
return false;
}
This is the function called after click. I noticed it uses something named 'google.feeds.findFeeds' so a bit of searching and voilà: https://developers.google.com/feed/v1/devguide#optional. There is a google api which provides functionality for searching and browsing public rss feeds :) The site provides examples of use so you can read more there. I hope this covers all of your doubts ;)

Google map API- display alternative routes with distance on info window

I am using google map and I am bit stuck at one point. I want to display alternative routes for my source and destination point. Currently I am using the code which is working perfectly and displaying correct result but the only the problem is that this code displaying infowindow for all routes with distance and time. And it needs to be differently colored for alternative routes..
Please help me out.
var request = {
origin: source,
destination: destination,
travelMode: google.maps.TravelMode.DRIVING,
provideRouteAlternatives: true,
optimizeWaypoints
unitSystem: google.maps.UnitSystem.METRIC
};
directionsService.route(request,
function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
var step=2;
for (var i = 0, len = response.routes.length; i < len; i++) {
new google.maps.DirectionsRenderer({
map: mapObject,
directions: response,
routeIndex: i
});
stepDisplay.setContent(response.routes[i].legs[i].steps[step].distance.text + "<br>" + response.routes[i].legs[i].steps[step].duration.text + " ");
stepDisplay.setPosition(response.routes[i].legs[i].steps[step].end_location);
stepDisplay.open(mapObject);
}
} else {
$("#error").append("Unable to retrieve your route<br />");
}
});
I analyzed your code as much as I could (not really that experienced with JS). So I'm gonna go on ahead and try to address the concerns on your comment.
InfoWindow for all routes
Based on the code snippet you provided, the for loop already iterates over the response.routes. In here, I see you are already setting an InfoWindow (showing distance and time), based on the route:
stepDisplay.setContent(response.routes[i].legs[i].steps[step].distance.text + "<br>" + response.routes[i].legs[i].steps[step].duration.text + " ");
stepDisplay.setPosition(response.routes[i].legs[i].steps[step].end_location);
stepDisplay.open(mapObject);
-- It made me wonder as to why you still specified this as a problem. Then it hit me, is it that the only InfoWindow shown here is for the last route? If so, your probably not setting it to the map properly. So I looked around and found this JSFiddle Sample where I can try out if it's possible to show multiple InfoWindows. I just added the code where you include an InfoWindow like so:
directionsDisplay.setDirections(response);
var step = 1;
var infowindow2 = new google.maps.InfoWindow();
infowindow2.setContent(response.routes[0].legs[0].steps[step].distance.text + "<br>" + response.routes[0].legs[0].steps[step].duration.text + " ");
infowindow2.setPosition(response.routes[0].legs[0].steps[step].end_location);
infowindow2.open(map);
-- when you open the JSFiddle Sample above, just add the code snippet like above, below the directionsDisplay.setDirections(response); and it will show something like this:
[Multiple InfoWindow Map Screenshot][1]
Route lines should be different colors
For this concern, I found a similar post here. From the answer:
You can specify the color of the line when you create the DirectionsRenderer, using the optional DirectionsRendererOptions struct.
Here's a part of the snippet in the answer:
directionsDisplay = new google.maps.DirectionsRenderer({
polylineOptions: {
strokeColor: "red"
}
});
I tried it out and this is how it looked like:
[Route Different Color Screenshot][2]
How you set the color of each route is up to you though.
Hope this helps. Good luck. :)
PS: Included the Screenshot links in the comments.*

C# parsing web site with ajax loaded content

If I recive a web site with this function I get the whole page, but without the ajax loaded values.
htmlDoc.LoadHtml(new WebClient().DownloadString(url));
Is it possible to load the web site like in gChrome with all values?
You can use a WebBrowser control to get and render the page. Unfortunately, the control uses Internet Explorer and you have to change a registry value in order to force it to use the latest version and even then the implementation is very brittle.
Another option is to take a standalone browser engine like WebKit and make it work in .NET. I found a page explaining how to do this, but it's pretty dated: http://webkitdotnet.sourceforge.net/basics.php
I worked on a little demo app to get the content and this is what I came up with:
class Program
{
static void Main(string[] args)
{
GetRenderedWebPage("https://siderite.dev", TimeSpan.FromSeconds(5), output =>
{
Console.Write(output);
File.WriteAllText("output.txt", output);
});
Console.ReadKey();
}
private static void GetRenderedWebPage(string url, TimeSpan waitAfterPageLoad, Action<string> callBack)
{
const string cEndLine= "All output received";
var sb = new StringBuilder();
var p = new PhantomJS();
p.OutputReceived += (sender, e) =>
{
if (e.Data==cEndLine)
{
callBack(sb.ToString());
} else
{
sb.AppendLine(e.Data);
}
};
p.RunScript(#"
var page = require('webpage').create();
page.viewportSize = { width: 1920, height: 1080 };
page.onLoadFinished = function(status) {
if (status=='success') {
setTimeout(function() {
console.log(page.content);
console.log('" + cEndLine + #"');
phantom.exit();
}," + waitAfterPageLoad.TotalMilliseconds + #");
}
};
var url = '" + url + #"';
page.open(url);", new string[0]);
}
}
This uses the PhantomJS "headless" browser by way of the wrapper NReco.PhantomJS which you can get through "reference NuGet package" directly from Visual Studio. I am sure it can be done better, but this is what I did today. You might want to take a look at the PhantomJS callbacks so you can properly debug what is going on. My example will wait forever if the URL doesn't work, for example. Here is a useful link: https://newspaint.wordpress.com/2013/04/25/getting-to-the-bottom-of-why-a-phantomjs-page-load-fails/
No its not possible in your example. Since it will load content as a string. You should render that string in "browser engine" or find any components which would do that for you.
I would suggest you to look into abotx they just announce this feature so maybe would be interesting for you but its not free.

How to work with the BING REST Api

How exactly do you use the BING REST api (specifically the ROUTES part) to get a driving distance in ASP.NET.
I have searched high and low on Google for this answer and none is forthcoming.
I have found url strings such as:
http://dev.virtualearth.net/REST/v1/Routes/Driving?waypoint.0=redmond&heading=90&waypoint.1=seattle&du=mi&key=BingMapsKey
That's great! But how to call it from ASP?
I have also found this code:
private void GetResponse(Uri uri, Action<HttpResponse> callback)
{
WebClient wc = new WebClient();
wc.OpenReadCompleted += (o, a) =>
{
if (callback != null)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(HttpResponse));
callback(ser.ReadObject(a.Result) as HttpResponse);
}
};
wc.OpenReadAsync(uri);
}
Which is a "generic method to make web requests". But, again, how do you call it? I find it confusing that it doesn't require a return type.
In order to call it, I have found code like this:
string key = "YOUR_BING_MAPS_KEY or SESSION_KEY";
string query = "1 Microsoft Way, Redmond, WA";
Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}", query, key));
GetResponse(geocodeRequest, (x) =>
{
Console.WriteLine(x.ResourceSets[0].Resources.Length + " result(s) found.");
Console.ReadLine();
});
But when I add this to the project, I get every error under the sun coming up. So, I am stuck.
I am a total ASP beginner and haven't found any online documentation any help at all.
p.s. I do have a BING api key and do use it in the code above.
I am not an expert in this, but the below compiles for me. Also make sure to add the data constructs as mentioned in the BING documentation:
protected void Page_Load(object sender, EventArgs e)
{
string key = "YOUR KEY";
string query = "ADDRESS";
Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}", query, key));
GetResponse(geocodeRequest, (x) =>
{
Console.WriteLine(x.ResourceSets[0].Resources.Length + " result(s) found.");
Console.ReadLine();
});
}
Quoting from another stackoverflow question:
The bottom of the documentation you are using points to the Data contracts you need for the REST services which are available here: http://msdn.microsoft.com/en-us/library/jj870778.aspx
Simply create a empty C# file and copy in paste in the C# Data Contracts. Then add the namespace to this class:
using BingMapsRESTService.Common.JSON;

How to get Google Plus's post data ( likes - shares - comments )?

Using C# , I want to read the Shares , Comments and Likes of a Google + post like this https://plus.google.com/107200121064812799857/posts/GkyGQPLi6KD
That post is an activity. This page includes infomation on how to get infomation about activities. This page gives some examples of using the API. This page has downloads for the Google API .NET library, which you can use to access the Google+ APIs, with XML documentation etc.
You'll need to use the API Console to get an API key and manage your API usage.
Also take a look at the API Explorer.
Here's a working example:
Referencing Google.Apis.dll and Google.Apis.Plus.v1.dll
PlusService plus = new PlusService();
plus.Key = "YOURAPIKEYGOESHERE";
ActivitiesResource ar = new ActivitiesResource(plus);
ActivitiesResource.Collection collection = new ActivitiesResource.Collection();
//107... is the poster's id
ActivitiesResource.ListRequest list = ar.List("107200121064812799857", collection);
ActivityFeed feed = list.Fetch();
//You'll obviously want to use a _much_ better way to get
// the activity id, but you aren't normally searching for a
// specific URL like this.
string activityKey = "";
foreach (var a in feed.Items)
if (a.Url == "https://plus.google.com/107200121064812799857/posts/GkyGQPLi6KD")
{
activityKey = a.Id;
break;
}
ActivitiesResource.GetRequest get = ar.Get(activityKey);
Activity act = get.Fetch();
Console.WriteLine("Title: "+act.Title);
Console.WriteLine("URL:"+act.Url);
Console.WriteLine("Published:"+act.Published);
Console.WriteLine("By:"+act.Actor.DisplayName);
Console.WriteLine("Annotation:"+act.Annotation);
Console.WriteLine("Content:"+act.Object.Content);
Console.WriteLine("Type:"+act.Object.ObjectType);
Console.WriteLine("# of +1s:"+act.Object.Plusoners.TotalItems);
Console.WriteLine("# of reshares:"+act.Object.Resharers.TotalItems);
Console.ReadLine();
Output:
Title: Wow Awesome creativity...!!!!!
URL:http://plus.google.com/107200121064812799857/posts/GkyGQPLi6KD
Published:2012-04-07T05:11:22.000Z
By:Funny Pictures & Videos
Annotation:
Content: Wow Awesome creativity...!!!!!
Type:note
# of +1s:210
# of reshares:158

Categories