diff --git a/example/build.sh b/example/build.sh index 2a1c9a8..ad13c71 100755 --- a/example/build.sh +++ b/example/build.sh @@ -6,6 +6,8 @@ dmcs concepts.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dl dmcs keywords.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dll +dmcs keywords_json.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dll + dmcs categories.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dll dmcs feed_links.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dll diff --git a/example/keywords_json.cs b/example/keywords_json.cs new file mode 100644 index 0000000..aa9fd30 --- /dev/null +++ b/example/keywords_json.cs @@ -0,0 +1,43 @@ + +using System; +using System.IO; +using AlchemyAPI; + + +public class TestApp +{ + static public void Main () + { + // Create an AlchemyAPI object. + AlchemyAPI.AlchemyAPI alchemyObj = new AlchemyAPI.AlchemyAPI(); + AlchemyAPI_KeywordParams alchemyParams = new AlchemyAPI_KeywordParams(); + alchemyParams.setOutputMode(AlchemyAPI_BaseParams.OutputMode.JSON); + + + // Load an API key from disk. + alchemyObj.LoadAPIKey("api_key.txt"); + + + // Extract topic keywords for a web URL. + string response = alchemyObj.URLGetRankedKeywords("http://www.techcrunch.com/", alchemyParams); + Console.WriteLine (response); + + + // Extract topic keywords for a text string. + response = alchemyObj.TextGetRankedKeywords("Hello there, my name is Bob Jones. I live in the United States of America. Where do you live, Fred?", alchemyParams); + Console.WriteLine (response); + + + // Load a HTML document to analyze. + StreamReader streamReader = new StreamReader("data/example.html"); + string htmlDoc = streamReader.ReadToEnd(); + streamReader.Close(); + + + // Extract topic keywords for a HTML document. + response = alchemyObj.HTMLGetRankedKeywords(htmlDoc, "http://www.test.com/", alchemyParams); + Console.WriteLine (response); + } + +} + diff --git a/module/AlchemyAPI.cs b/module/AlchemyAPI.cs index a936ec6..5ca9a37 100644 --- a/module/AlchemyAPI.cs +++ b/module/AlchemyAPI.cs @@ -939,13 +939,18 @@ private string DoRequest(HttpWebRequest wreq, AlchemyAPI_BaseParams.OutputMode o { StreamReader r = new StreamReader (wres.GetResponseStream()); - string xml = r.ReadToEnd(); + string response = r.ReadToEnd(); + + if (string.IsNullOrEmpty(response)) + throw new Exception ("The API request returned back an empty response. Please verify that the url is correct."); + + if (AlchemyAPI_BaseParams.OutputMode.XML != outputMode && AlchemyAPI_BaseParams.OutputMode.RDF != outputMode) + { + return response; // don't parse response, just return as is + } - if (string.IsNullOrEmpty(xml)) - throw new XmlException ("The API request returned back an empty response. Please verify that the url is correct."); - XmlDocument xmlDoc = new XmlDocument (); - xmlDoc.LoadXml(xml); + xmlDoc.LoadXml(response); XmlElement root = xmlDoc.DocumentElement; @@ -997,7 +1002,7 @@ private string DoRequest(HttpWebRequest wreq, AlchemyAPI_BaseParams.OutputMode o } } - return xml; + return response; } }