Skip to content

Commit 5ed99b1

Browse files
committed
get pub date html and detect feeds
1 parent 3b720bb commit 5ed99b1

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

Examples/ServiceExamples/Scripts/ExampleAlchemyLanguage.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void Start () {
3232
string unitySDK_release_html = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/unitySDK_release.html";
3333
string watson_beats_jeopardy_html = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/watson_beats_jeopardy.html";
3434
string microformats_html = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/microformats.html";
35+
string ycombindator_html = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/ycombinator_html.html";
3536
// Get Author URL POST
3637
// if(!m_AlchemyLanguage.GetAuthors(OnGetAuthors, m_ExampleURL_watsonJeopardy))
3738
// Log.Debug("ExampleAlchemyLanguage", "Failed to get authors URL POST!");
@@ -89,7 +90,11 @@ void Start () {
8990
// Log.Debug("ExampleAlchemyLanguage", "Failed to get entities by HTML POST");
9091

9192
// Detect Feeds URL POST
92-
// if(!m_AlchemyLanguage.DetectFeeds(OnDetectFeeds, "http://www.kotaku.com"))
93+
// if(!m_AlchemyLanguage.DetectFeeds(OnDetectFeeds, "http://time.com/newsfeed/"))
94+
// Log.Debug("ExampleAlchemyLanguage", "Failed to get feeds by URL POST");
95+
96+
// Detect Feeds HTML POST
97+
// if(!m_AlchemyLanguage.DetectFeeds(OnDetectFeeds, ycombindator_html))
9398
// Log.Debug("ExampleAlchemyLanguage", "Failed to get feeds by URL POST");
9499

95100
// Extract Keywords URL POST
@@ -125,12 +130,16 @@ void Start () {
125130
// Log.Debug("ExampleAlchemyLanguage", "Failed to get microformats by text POST");
126131

127132
// Get PublicationDate URL POST
128-
// if(!m_AlchemyLanguage.GetPublicationDate(OnGetPublicationDate, m_ExampleURL_watsonJeopardy))
129-
// Log.Debug("ExampleAlchemyLanguage", "Failed to get publication dates by text POST");
133+
if(!m_AlchemyLanguage.GetPublicationDate(OnGetPublicationDate, m_ExampleURL_watsonJeopardy))
134+
Log.Debug("ExampleAlchemyLanguage", "Failed to get publication dates by url POST");
135+
136+
// Get PublicationDate HTML POST
137+
if(!m_AlchemyLanguage.GetPublicationDate(OnGetPublicationDate, watson_beats_jeopardy_html))
138+
Log.Debug("ExampleAlchemyLanguage", "Failed to get publication dates by html POST");
130139

131140
// Get Relations URL POST
132141
// if(!m_AlchemyLanguage.GetRelations(OnGetRelations, m_ExampleURL_watsonJeopardy))
133-
// Log.Debug("ExampleAlchemyLanguage", "Failed to get relations by text POST");
142+
// Log.Debug("ExampleAlchemyLanguage", "Failed to get relations by text POST");
134143

135144
// Get Relations Text POST
136145
// if(!m_AlchemyLanguage.GetRelations(OnGetRelations, m_ExampleText_watsonJeopardy))
@@ -192,11 +201,11 @@ void Start () {
192201
// if(!m_AlchemyLanguage.GetRawText(OnGetText, m_ExampleURL_watsonJeopardy))
193202
// Log.Debug("ExampleAlchemyLanguage", "Failed to get raw text by text POST");
194203

195-
// Get Raw Text HTML POST
204+
// Get Title HTML POST
196205
// if(!m_AlchemyLanguage.GetTitle(OnGetTitle, watson_beats_jeopardy_html))
197206
// Log.Debug("ExampleAlchemyLanguage", "Failed to get title by text POST");
198207

199-
// Get Raw Text URL POST
208+
// Get Title URL POST
200209
// if(!m_AlchemyLanguage.GetTitle(OnGetTitle, m_ExampleURL_watsonJeopardy))
201210
// Log.Debug("ExampleAlchemyLanguage", "Failed to get title by text POST");
202211

Scripts/Services/AlchemyAPI/AlchemyLanguage.cs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -533,29 +533,49 @@ private void OnGetEntitiesResponse(RESTConnector.Request req, RESTConnector.Resp
533533

534534
#region FeedDetection
535535
private const string SERVICE_DETECT_FEEDS_URL = "/calls/url/URLGetFeedLinks";
536+
private const string SERVICE_DETECT_FEEDS_HTML = "/calls/html/HTMLGetFeedLinks";
536537
public delegate void OnDetectFeeds(FeedData feedData, string data);
537538

538-
public bool DetectFeeds(OnDetectFeeds callback, string url, string customData = default(string))
539+
public bool DetectFeeds(OnDetectFeeds callback, string source, string customData = default(string))
539540
{
540541
if (callback == null)
541542
throw new ArgumentNullException("callback");
542-
if (string.IsNullOrEmpty(url))
543-
throw new WatsonException("Please provide a url for GetEmotions.");
543+
if (string.IsNullOrEmpty(source))
544+
throw new WatsonException("Please provide a source for GetEmotions.");
544545
if (string.IsNullOrEmpty(mp_ApiKey))
545546
SetCredentials();
546547

547548
DetectFeedsRequest req = new DetectFeedsRequest();
548549
req.Callback = callback;
549-
req.Data = string.IsNullOrEmpty(customData) ? url : customData;
550+
req.Data = string.IsNullOrEmpty(customData) ? source : customData;
550551

551552
req.Parameters["apikey"] = mp_ApiKey;
552553
req.Parameters["outputMode"] = "json";
553554

554555
req.Headers["Content-Type"] = "application/x-www-form-urlencoded";
555556
req.Forms = new Dictionary<string, RESTConnector.Form>();
556-
req.Forms["url"] = new RESTConnector.Form(url);
557557

558-
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_DETECT_FEEDS_URL);
558+
string service;
559+
string normalizedSource = source.Trim().ToLower();
560+
if(normalizedSource.StartsWith("http://") || normalizedSource.StartsWith("https://"))
561+
{
562+
service = SERVICE_DETECT_FEEDS_URL;
563+
req.Forms["url"] = new RESTConnector.Form(source);
564+
}
565+
else if(Path.GetExtension(normalizedSource).EndsWith(".html") && !normalizedSource.StartsWith("http://") && !normalizedSource.StartsWith("https://"))
566+
{
567+
service = SERVICE_DETECT_FEEDS_HTML;
568+
string htmlData = default(string);
569+
htmlData = File.ReadAllText(source);
570+
req.Forms["html"] = new RESTConnector.Form(htmlData);
571+
}
572+
else
573+
{
574+
Log.Error("Alchemy Language", "Either a URL or a html page source is required for DetectFeeds!");
575+
return false;
576+
}
577+
578+
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, service);
559579
if(connector == null)
560580
return false;
561581

@@ -874,7 +894,7 @@ private void OnGetMicroformatsResponse(RESTConnector.Request req, RESTConnector.
874894

875895
#region GetPubDate
876896
private const string SERVICE_GET_PUBLICATION_DATE_URL = "/calls/url/URLGetPubDate";
877-
// private const string SERVICE_GET_PUBLICATION_DATE_HTML = "/calls/html/HTMLGetPubDate";
897+
private const string SERVICE_GET_PUBLICATION_DATE_HTML = "/calls/html/HTMLGetPubDate";
878898
public delegate void OnGetPublicationDate(PubDateData pubDateData, string data);
879899

880900
public bool GetPublicationDate(OnGetPublicationDate callback, string source, string customData = default(string))
@@ -905,12 +925,10 @@ private void OnGetMicroformatsResponse(RESTConnector.Request req, RESTConnector.
905925
}
906926
else if(Path.GetExtension(normalizedSource).EndsWith(".html") && !normalizedSource.StartsWith("http://") && !normalizedSource.StartsWith("https://"))
907927
{
908-
Log.Error("AlchemyLanguage", "PublicationDate by HTML is not supported!");
909-
return false;
910-
// service = SERVICE_GET_PUBLICATION_DATE_HTML;
911-
// string htmlData = default(string);
912-
// htmlData = File.ReadAllText(source);
913-
// req.Forms["html"] = new RESTConnector.Form(htmlData);
928+
service = SERVICE_GET_PUBLICATION_DATE_HTML;
929+
string htmlData = default(string);
930+
htmlData = File.ReadAllText(source);
931+
req.Forms["html"] = new RESTConnector.Form(htmlData);
914932
}
915933
else
916934
{

0 commit comments

Comments
 (0)