I know, I know... I said I’d be posting on IronRuby, but yet again I got distracted <sigh>. Back to that next week, I promise.
The good news is that, once again, I managed to get distracted by something pretty cool. :-)
Earlier in the week I’d stumbled across an article mentioning the Bing API (currently in version 2.0), which allows you to perform programmatic web searches using various web-service related technologies, such as REST, JSON & SOAP. I played around with that, for a while, importing the Bing web service into different versions of Visual Studio (both of which gave me the same run-time error, once I actually tried to execute them), and then decided to look for an alternative approach.
That was when I came across this post, introducing an implementation of a LINQ provider for Bing called BLinq (love that name! :-). The BLinq implementation posted there takes away the heavy lifting of working with Bing – I believe it uses REST under-the-hood, but to be honest I don’t even need to know that - and allows you to take advantage of the elegant syntax of LINQ.
I’ve threatened to talk about LINQ before, but this is the first time I’ve really used it. Anyone familiar with SQL should find LINQ straightforward: it basically integrates the ability to query various data-sources into your favourite .NET language (hence Language INtegrated Query). I haven’t looked into the BLinq implementation very deeply: I simply rebuilt the BLinq module, referenced it from an AutoCAD .NET project created using the new Wizard and integrated the code from the test project, in Program.cs.
A few comments about the various steps needed to get this working:
- Getting an Bing AppId
- You will need to submit an online request for your own Bing AppId
- You can replace the “TODO: INSERT YOUR APPID HERE” string below with this ID
- Mine was a 40-character hex string, presumably yours will be, too :-)
- Rebuilding BLinq
- You will need to use Visual Studio 2008 or higher (I’m using VS 2008 SP1) using at least the .NET Framework 3.5
- I ended up downloading and installing the Silverlight 3.0 SDK to get the right version of the System.ComponentModel.DataAnnotations assembly
- If you find certain errors such as DisplayAttribute, EditableAttribute and KeyAttribute undefined, you will probably also need to do this
- I added the assembly by browsing to (on my system) C:\Program Files\Microsoft SDKs\Silverlight\v3.0\Libraries\Client
- Building your client application
- As mentioned earlier, I did this using the AutoCAD 2010 .NET Wizard
- I added an assembly reference to the BLinq DLL, but that was the only one needed
- The default project includes references to a number of (probably) significant assemblies, such as System.Xml.Linq
We’re going to use BLinq to implement a command named BING which asks the user for a query string and then uses it to perform three searches using Bing: a standard web search, a local search in the area of San Rafael, California, and an image search. The results then get displayed at the AutoCAD command-line.
Here’s the C# code I used to implement our BING command:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System.Collections.Generic;
using System.Linq;
using BLinq;
namespace BLinqInAutoCAD
{
public class Commands
{
static private Editor _ed = null;
private static void WriteImages(
string query,
IEnumerable<ImageSearchResult> images
)
{
_ed.WriteMessage("\nImage Query: " + query);
_ed.WriteMessage("\n" + new string('=', 80));
foreach (ImageSearchResult image in images)
{
_ed.WriteMessage(
"\nTitle: " + image.Title
);
_ed.WriteMessage(
"\nUri: " + image.Uri.AbsoluteUri
);
_ed.WriteMessage(
"\nSize: " + image.Width + " x " + image.Height
);
_ed.WriteMessage(
"\nThumbnail: " + image.ThumbnailUri.AbsoluteUri
);
_ed.WriteMessage("\n");
}
_ed.WriteMessage("\n");
}
private static void WritePages(
string query,
IEnumerable<PageSearchResult> pages
)
{
_ed.WriteMessage("\nPage Query: " + query);
_ed.WriteMessage("\n" + new string('=', 80));
foreach (PageSearchResult page in pages)
{
_ed.WriteMessage("\nTitle: " + page.Title);
_ed.WriteMessage("\nUri: " + page.Uri.AbsoluteUri);
_ed.WriteMessage("\nDisplay: " + page.DisplayUrl);
_ed.WriteMessage("\nDescription");
_ed.WriteMessage("\n" + page.Description);
_ed.WriteMessage("\n");
}
_ed.WriteMessage("\n");
}
[CommandMethod("BING")]
public static void BingSearch()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
_ed = doc.Editor;
PromptStringOptions pso =
new PromptStringOptions(
"\nEnter search string: "
);
pso.AllowSpaces = true;
PromptResult pr = _ed.GetString(pso);
if (pr.Status == PromptStatus.OK && pr.StringResult != "")
{
string searchString = pr.StringResult;
BingContext bing =
new BingContext("TODO: INSERT YOUR APPID HERE");
IQueryable<PageSearchResult> pages1 =
from p in bing.Pages
where p.Query == searchString
select p;
pages1 = pages1.Take(2);
WritePages(searchString, pages1);
var q2 =
from p
in bing.Pages.SafeResults().LocalResults("San Rafael")
where p.Query == searchString
select p;
WritePages("Local search", q2);
var q3 =
from i in bing.Images
where i.Query == searchString
select i;
WriteImages("Image search", q3);
}
}
}
}
Here’s what happens when we run our BING command, entering the name of this blog as a search term:
Command: BING
Enter search string: Through the Interface
Page Query: Through the Interface
================================================================================
Title: Through the Interface
Uri: http://through-the-interface.typepad.com/
Display: through-the-interface.typepad.com
Description
A blog for developers working with AutoCAD and other Autodesk technologies
Title: Shearwater Flows Through the Interface - Spinner
Uri:
http://www.spinner.com/2008/08/29/shearwater-flows-through-the-interface/
Display: www.spinner.com/2008/08/29/shearwater-flows-through-the-interface
Description
Free Music Downloads, MP3 Blog, Indie Rock, Music Videos, Interviews, Songs and
Live Performances
Page Query: Local search
================================================================================
Title: IxDA Discussion: Job 001A, Interaction Designer, San Rafael, CA ...
Uri: http://www.interactiondesigners.com/discuss.php?post=27874
Display: www.interactiondesigners.com/discuss.php?post=27874
Description
... skilled at filtering brand and business objectives through ... project team
and will be expected to present and interface ... Job 001A, Interaction
Designer, San Rafael, CA, Groove11 ...
Title: Browse Reviews Near San Leandro | Yelp
Uri:
http://www.yelp.com/browse/reviews/recent?loc=San+Leandro%2C+CA&category=plumbin
g
Display:
www.yelp.com/browse/reviews/recent?loc=San+Leandro%2C+CA&category=plumbing
Description
I found out about this company through a friend at work ... T T. San Rafael, CA
... with had their work ethic and excellent customer interface.
Title: social events found near San Francisco, California, United States ...
Uri:
http://upcoming.yahoo.com/search/?category_id=4&loc=San+Francisco%2CCA&rt=1&page
=4
Display:
upcoming.yahoo.com/search/?category_id=4&loc=San+Francisco%2CCA&rt=1&page=4
Description
Dare to tread the San Andreas Fault where the earth's ... Four Points by
Sheraton S..., San Rafael, ca ... your movies, TV shows, music, and photos into
one interface..
Title: IxDA Discussion: JOB-Senior Interaction Designer-San Francisco ...
Uri: http://www.ixda.org/discuss.php?post=27990&search=designer
Display: www.ixda.org/discuss.php?post=27990&search=designer
Description
Develop the user interface for new features, including ... Job 001A,
Interaction Designer, San Rafael, CA, Groove11 ... Loc: NYC. CO: ArielPartners:
Recruiter/ Full-time ...
Title: Through the Interface: November 2006
Uri:
http://through-the-interface.typepad.com/through_the_interface/2006/11/index.htm
l
Display:
through-the-interface.typepad.com/through_the_interface/2006/11/index.html
Description
A blog for developers working with AutoCAD and other Autodesk technologies
Title: IxDA Discussion: JOB # Interaction Designer # Seattle, WA # Big Fish
...
Uri: http://www.interactiondesigners.com/discuss.php?post=27877
Display: www.interactiondesigners.com/discuss.php?post=27877
Description
Solid understanding of the fundamentals of user interface design; should have
experience working through the ... Job 001A, Interaction Designer, San Rafael,
CA, Groove11-Revamped ...
Title: SAN DIEGO HOTELS - CHEAP RATES GUARANTEED - SAN DIEGO, CA HOTELS
Uri: http://travel.modernhumorist.com/city.cfm/_San-Diego_CA
Display: travel.modernhumorist.com/city.cfm/_San-Diego_CA
Description
Cheap Hotels in San Diego, CA - California Choose from 188 hotels in San Diego,
CA - San ... travelers due to our easy accessibility to the freeway, the close
proximity to the loc ...
Title: Subject: Library of California Board actions February 14-16, 2001
Uri: http://www.library.ca.gov/loc/board/KeyActions/actions_Feb01.pdf
Display: www.library.ca.gov/loc/board/KeyActions/actions_Feb01.pdf
Description
... that the Library of California Board adopts the LoC ... County Library San
Mateo Union High School District San Rafael ... Ridge School Pleasant Valley
State Prison Gold Coast Interface ...
Title: localtweeps :: San Mateo, California
Uri: http://www.localtweeps.com/state/CA/San_Mateo/
Display: www.localtweeps.com/state/CA/San_Mateo
Description
SustainableSMC - San Mateo County. San Mateo, CA - http://www ... Senior User
Interface Designer at Gaia Interactive (gaiaonline ... San Rafael: 34:
Temecula: 33: Palo Alto: 33: Venice: 32: Chico: 32
Title: arch 1, 2008
Uri:
http://www.marin.ca.gov/depts/bs/main/sups/sdistr1/docs/March08Newsletter.pdf
Display: www.marin.ca.gov/depts/bs/main/sups/sdistr1/docs/March08Newsletter.pdf
Description
MMWD Tank Loc ation to be disc ussed with com- ... 00 a.m.-4:00 p.m. Mary
Silveira Elementary School, San Rafael ... disease, it is very common in the
urban-wildland interface ...
Image Query: Image search
================================================================================
Title: ... through the bbc motion
Uri: http://www.tomcampbell.com/pages/hdreel.html
Size: 211 x 284
Thumbnail:
http://ts1.images.live.com/images/thumbnail.aspx?q=975287422560&id=49d24255edf84
6166e9d7b69cc1f0366
Title: Behold, through the truth of ...
Uri: http://www.orthodoxdelmarva.org/
Size: 169 x 169
Thumbnail:
http://ts3.images.live.com/images/thumbnail.aspx?q=965845388446&id=09998a4971c97
12fcfb1bd0b7c48e6dc
Title: ... through caring, support and
Uri: http://csrbc.org/
Size: 288 x 173
Thumbnail:
http://ts4.images.live.com/images/thumbnail.aspx?q=958547176951&id=13b4b1d35283e
19c3d53387a3a2a6dd7
Title: Through the Interface ...
Uri:
http://through-the-interface.typepad.com/through_the_interface/personal/
Size: 455 x 480
Thumbnail:
http://ts4.images.live.com/images/thumbnail.aspx?q=1151060279387&id=aca0138b8489
8d0699bd738a77bbddf0
Title: ... read through the terms
Uri: http://www.wallstreetorganization.com/html/menuofservices.html
Size: 323 x 108
Thumbnail:
http://ts4.images.live.com/images/thumbnail.aspx?q=1100438768723&id=a1eb24b8240a
92b8a47c59da0a97ecaa
Title: ... through caring, support and
Uri: http://www.csrbc.org/
Size: 273 x 173
Thumbnail:
http://ts2.images.live.com/images/thumbnail.aspx?q=1170535684605&id=b7de190ffd3e
c8b24d5fbdf6d21b5e7a
Title: ... through the course
Uri: http://www.skinastc.com/update_dryland-06.html
Size: 254 x 192
Thumbnail:
http://ts2.images.live.com/images/thumbnail.aspx?q=1255470604085&id=5a686c11d820
4ce708dfbeb7340f9168
Title: ... through the slalom course
Uri: http://www.skinastc.com/update_dryland-06.html
Size: 254 x 192
Thumbnail:
http://ts1.images.live.com/images/thumbnail.aspx?q=1152025828944&id=866272997b3b
18a11f2c38ba06573d78
Title: ... through the collection of
Uri: http://www.typewritermuseum.org/collection/
Size: 209 x 139
Thumbnail:
http://ts2.images.live.com/images/thumbnail.aspx?q=1042642900713&id=927657174551
f10d8409b77865c3484a
Title: remotely controlled through ...
Uri: http://solar.psu.edu/2007/systems_lighting_controls.aspx?lang=en
Size: 255 x 179
Thumbnail:
http://ts2.images.live.com/images/thumbnail.aspx?q=1060407483821&id=32f483f0b57b
eba9967f3ae2f85bc898
As you can see, we get good and bad results back, much like any search run with a sufficiently vague query string. :-)
A command-line interface is clearly not the best way to display search our results – displaying them via WPF inside an AutoCAD palette would be much better – but this post is primarily focused on performing a search rather than worrying too much about the presentation side (we’ll see where it goes from here :-).
One last comment, in case people are wondering: I didn’t choose Bing over Google because I especially prefer it (I use both on a daily basis, depending on the results I get), but simply because I came across the code showing how to use it. It may very well be easy to integrate similar capabilities into your application to search Google programmatically – I’m not an expert in this area, by any means – but I will say that I was reasonably impressed with the capabilities provided by the Bing API and the BLinq framework, the little I’ve actually used them.