diff --git a/README.md b/README.md index 4f65104..a503366 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +Note: THIS IS NOT PRODUCTON CODE. This SDK is example code to get you started making programmatic calls to AlchemyAPI. + ================================ AlchemyAPI .NET SDK: version 0.10 ================================ @@ -12,10 +14,10 @@ MONO INSTALLATION -To compile this module, enter the "module" directory and run the "build.sh" +To compile this module and generate the AlchemyAPI.dll file, enter the "module" directory and run the "build.sh" script. -To install this module, copy the AlchemyAPI.dll file into your desired +To install this module, copy the AlchemyAPI.dll file created from the previous step into your desired .NET import directory, or set your MONO_PATH environment variable to point to the directory containing the compiled AlchemyAPI.dll file. @@ -29,7 +31,7 @@ and other tasks. All code samples are within the "example" directory. To run these code samples you must first edit the (example/api_key.txt) file, -adding your assigned Orchestr8 API key. +adding your assigned AlchemyAPI API key. Compiling Code Samples: @@ -70,10 +72,10 @@ Windows .NET INSTALLATION -To compile this module, enter the "module" directory and run the "build" +To compile this module and generate the AlchemyAPI.dll file, enter the "module" directory and run the "build.bat" script. -To install this module, copy the AlchemyAPI.dll file into your desired +To install this module, copy the AlchemyAPI.dll file generated from the previous step into your desired .NET import directory. For purposes of running examples, the dll is copied into the example folder. @@ -86,11 +88,11 @@ and other tasks. All code samples are within the "example" directory. To run these code samples you must first edit the (example/api_key.txt) file, -adding your assigned Orchestr8 API key. +adding your assigned AlchemyAPI API key. Compiling Code Samples: - 1. Run "build" (Compiles code using .NET) + 1. Run "build.bat" (Compiles code using .NET) Running Code Samples: diff --git a/example/api_key.txt b/example/api_key.txt new file mode 100644 index 0000000..e69de29 diff --git a/example/build.bat b/example/build.bat index abdb262..21bb492 100644 --- a/example/build.bat +++ b/example/build.bat @@ -30,3 +30,5 @@ csc combined.cs -r:System.Data.dll -r:System.Web.dll -r:AlchemyAPI.dll csc taxonomy.cs -r:System.Data.dll -r:System.Web.dll -r:AlchemyAPI.dll csc image.cs -r:System.Data.dll -r:System.Web.dll -r:AlchemyAPI.dll + +csc ranked_image_keywords.cs -r:System.Data.dll -r:System.Web.dll -r:AlchemyAPI.dll -r:System.Drawing.dll diff --git a/example/build.sh b/example/build.sh index cc9f672..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 @@ -32,5 +34,5 @@ dmcs taxonomy.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dl dmcs image.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dll -dmcs ranked_image_keywords.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dll +dmcs ranked_image_keywords.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dll -r:System.Drawing.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 60144dc..5ca9a37 100644 --- a/module/AlchemyAPI.cs +++ b/module/AlchemyAPI.cs @@ -888,7 +888,7 @@ private string GET(string callName, string callPrefix, AlchemyAPI_BaseParams par Uri address = new Uri (uri.ToString()); HttpWebRequest wreq = WebRequest.Create(address) as HttpWebRequest; - wreq.Proxy = GlobalProxySelection.GetEmptyWebProxy(); + wreq.Proxy = null; byte[] postData = parameters.GetPostData(); @@ -913,7 +913,7 @@ private string POST(string callName, string callPrefix, AlchemyAPI_BaseParams pa Uri address = new Uri (_requestUri + callPrefix + "/" + callName); HttpWebRequest wreq = WebRequest.Create(address) as HttpWebRequest; - wreq.Proxy = GlobalProxySelection.GetEmptyWebProxy(); + wreq.Proxy = null; wreq.Method = "POST"; wreq.ContentType = "application/x-www-form-urlencoded"; @@ -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; @@ -955,8 +960,18 @@ private string DoRequest(HttpWebRequest wreq, AlchemyAPI_BaseParams.OutputMode o if (status.InnerText != "OK") { - System.ApplicationException ex = - new System.ApplicationException ("Error making API call."); + string errorMessage = "Error making API call."; + + try + { + XmlNode statusInfo = root.SelectSingleNode("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/results/statusInfo"); + errorMessage = statusInfo.InnerText; + } + catch + { + errorMessage = "An error occurred: Unable to access XmlNode /results/statusInfo"; + } + System.ApplicationException ex = new System.ApplicationException (errorMessage); throw ex; } @@ -970,14 +985,24 @@ private string DoRequest(HttpWebRequest wreq, AlchemyAPI_BaseParams.OutputMode o if (status.InnerText != "OK") { - System.ApplicationException ex = - new System.ApplicationException ("Error making API call."); + string errorMessage = "Error making API call."; + + try + { + XmlNode statusInfo = root.SelectSingleNode("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/results/statusInfo"); + errorMessage = statusInfo.InnerText; + } + catch + { + errorMessage = "An error occurred: Unable to access XmlNode /results/statusInfo"; + } + System.ApplicationException ex = new System.ApplicationException (errorMessage); throw ex; } } - return xml; + return response; } } diff --git a/module/build.bat b/module/build.bat index 1a1767d..38a3eca 100644 --- a/module/build.bat +++ b/module/build.bat @@ -1,4 +1,4 @@ -csc /target:library AlchemyAPI.cs AlchemyAPI_BaseParams.cs AlchemyAPI_CategoryParams.cs AlchemyAPI_ConceptParams.cs AlchemyAPI_ConstraintQueryParams.cs AlchemyAPI_EntityParams.cs AlchemyAPI_KeywordParams.cs AlchemyAPI_LanguageParams.cs AlchemyAPI_TextParams.cs AlchemyAPI_RelationParams.cs AlchemyAPI_TargetedSentimentParams.cs AlchemyAPI_CombinedDataParams.cs AlchemyAPI_TaxonomyParams.cs AlchemyAPI_ImageParams.cs -r:System.Data.dll -r:System.Web.dll +csc /target:library AlchemyAPI.cs AlchemyAPI_BaseParams.cs AlchemyAPI_CategoryParams.cs AlchemyAPI_ConceptParams.cs AlchemyAPI_ConstraintQueryParams.cs AlchemyAPI_EntityParams.cs AlchemyAPI_KeywordParams.cs AlchemyAPI_LanguageParams.cs AlchemyAPI_TextParams.cs AlchemyAPI_RelationParams.cs AlchemyAPI_TargetedSentimentParams.cs AlchemyAPI_CombinedDataParams.cs AlchemyAPI_TaxonomyParams.cs AlchemyAPI_ImageParams.cs AlchemyAPI_RankedImageKeywords.cs -r:System.Data.dll -r:System.Web.dll -r:System.Drawing.dll copy "AlchemyAPI.dll" "..\example\"