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
2 changes: 2 additions & 0 deletions 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 Down
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);
}

}

17 changes: 11 additions & 6 deletions module/AlchemyAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down Expand Up @@ -997,7 +1002,7 @@ private string DoRequest(HttpWebRequest wreq, AlchemyAPI_BaseParams.OutputMode o
}
}

return xml;
return response;

}
}
Expand Down