Skip to content
This repository was archived by the owner on Oct 30, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
================================
Expand All @@ -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.

Expand All @@ -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:

Expand Down Expand Up @@ -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.

Expand All @@ -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:

Expand Down
Empty file added example/api_key.txt
Empty file.
2 changes: 2 additions & 0 deletions example/build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion example/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

43 changes: 43 additions & 0 deletions example/keywords_json.cs
Original file line number Diff line number Diff line change
@@ -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);
}

}

49 changes: 37 additions & 12 deletions module/AlchemyAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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";

Expand All @@ -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;

Expand All @@ -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("/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;
}
Expand All @@ -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("/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;

}
}
Expand Down
2 changes: 1 addition & 1 deletion module/build.bat
Original file line number Diff line number Diff line change
@@ -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\"