Skip to content
This repository was archived by the owner on Oct 30, 2018. It is now read-only.

Commit 4ed6e03

Browse files
committed
Added the image extraction api
1 parent c39f405 commit 4ed6e03

File tree

8 files changed

+194
-22
lines changed

8 files changed

+194
-22
lines changed

example/image.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.IO;
3+
using AlchemyAPI;
4+
5+
6+
public class TestApp
7+
{
8+
static public void Main ()
9+
{
10+
// Create an AlchemyAPI object.
11+
AlchemyAPI.AlchemyAPI alchemyObj = new AlchemyAPI.AlchemyAPI();
12+
13+
// Load an API key from disk.
14+
alchemyObj.LoadAPIKey("api_key.txt");
15+
16+
AlchemyAPI_ImageParams prms = new AlchemyAPI_ImageParams();
17+
prms.ImageMode = PageImageMode.AlwaysInfer;
18+
19+
string xml = alchemyObj.URLGetImage("http://www.theguardian.com/world/2013/dec/01/ukraine-largest-street-protests-orange-revolution", prms);
20+
21+
Console.WriteLine(xml);
22+
}
23+
}

example/image/AssemblyInfo.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
4+
// Information about this assembly is defined by the following attributes.
5+
// Change them to the values specific to your project.
6+
7+
[assembly: AssemblyTitle("image")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("")]
12+
[assembly: AssemblyCopyright("mike")]
13+
[assembly: AssemblyTrademark("")]
14+
[assembly: AssemblyCulture("")]
15+
16+
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
17+
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
18+
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
19+
20+
[assembly: AssemblyVersion("1.0.*")]
21+
22+
// The following attributes are used to specify the signing key for the assembly,
23+
// if desired. See the Mono documentation for more information about signing.
24+
25+
//[assembly: AssemblyDelaySign(false)]
26+
//[assembly: AssemblyKeyFile("")]
27+

example/image/image.csproj

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>10.0.0</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<RootNamespace>image</RootNamespace>
11+
<AssemblyName>image</AssemblyName>
12+
</PropertyGroup>
13+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
14+
<DebugSymbols>true</DebugSymbols>
15+
<DebugType>full</DebugType>
16+
<Optimize>false</Optimize>
17+
<OutputPath>bin\Debug</OutputPath>
18+
<DefineConstants>DEBUG;</DefineConstants>
19+
<ErrorReport>prompt</ErrorReport>
20+
<WarningLevel>4</WarningLevel>
21+
<Externalconsole>true</Externalconsole>
22+
</PropertyGroup>
23+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
24+
<DebugType>none</DebugType>
25+
<Optimize>true</Optimize>
26+
<OutputPath>bin\Release</OutputPath>
27+
<ErrorReport>prompt</ErrorReport>
28+
<WarningLevel>4</WarningLevel>
29+
<Externalconsole>true</Externalconsole>
30+
</PropertyGroup>
31+
<ItemGroup>
32+
<Reference Include="System" />
33+
</ItemGroup>
34+
<ItemGroup>
35+
<Compile Include="AssemblyInfo.cs" />
36+
<Compile Include="..\image.cs">
37+
<Link>image.cs</Link>
38+
</Compile>
39+
</ItemGroup>
40+
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
41+
<ItemGroup>
42+
<None Include="..\api_key.txt">
43+
<Link>api_key.txt</Link>
44+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
45+
</None>
46+
</ItemGroup>
47+
<ItemGroup>
48+
<ProjectReference Include="..\..\module\sdk\sdk.csproj">
49+
<Project>{799E354F-E323-408C-9733-7F1896342663}</Project>
50+
<Name>sdk</Name>
51+
</ProjectReference>
52+
</ItemGroup>
53+
</Project>

module/AlchemyAPI.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,32 @@ public string TextGetRankedTaxonomy (string text, AlchemyAPI_TaxonomyParams para
789789
}
790790
#endregion
791791

792+
#region GetImage
793+
public string HTMLGetImage (string html, string url)
794+
{
795+
return HTMLGetImage(html, url, new AlchemyAPI_ImageParams());
796+
}
797+
public string HTMLGetImage (string html, string url, AlchemyAPI_ImageParams parameters)
798+
{
799+
CheckHTML(html, url);
800+
parameters.setHtml(html);
801+
parameters.setUrl(url);
802+
803+
return POST ("HTMLGetImage", "html", parameters);
804+
}
805+
public string URLGetImage (string url)
806+
{
807+
return URLGetImage (url, new AlchemyAPI_ImageParams());
808+
}
809+
public string URLGetImage (string url, AlchemyAPI_ImageParams parameters)
810+
{
811+
CheckURL(url);
812+
parameters.setUrl(url);
813+
814+
return GET ("URLGetImage", "url", parameters);
815+
}
816+
#endregion
817+
792818
private void CheckHTML(string html, string url)
793819
{
794820
if (html.Length < 10)

module/AlchemyAPI_CombinedDataParams.cs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33

44
namespace AlchemyAPI
55
{
6-
public enum PageImageMode
7-
{
8-
TrustMetadata,
9-
AlwaysInfer
10-
};
6+
117

128
///
139
/// Enumeration that specifies what to extract from the text.
@@ -122,7 +118,7 @@ public override string getParameterString ()
122118
retString += "&extract=" + getExtractionString();
123119

124120
if (_imageMode != null)
125-
retString += "&extractMode=" + getImageModeString();
121+
retString += "&extractMode=" + AlchemyAPI_ImageParams.getImageModeString(_imageMode.Value);
126122

127123
if (_jsonpCallback != null)
128124
retString += "&jsonp=" + _jsonpCallback;
@@ -185,21 +181,5 @@ private string getExtractionString()
185181

186182
return ret;
187183
}
188-
189-
private string getImageModeString ()
190-
{
191-
switch (_imageMode)
192-
{
193-
case PageImageMode.TrustMetadata:
194-
return "trust-metadata";
195-
case PageImageMode.AlwaysInfer:
196-
return "always-infer";
197-
default:
198-
throw new ArgumentException("The specified image mode '" + _imageMode + "' is not supported.");
199-
}
200-
}
201-
202-
203-
204184
}
205185
}

module/AlchemyAPI_ImageParams.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Web;
3+
4+
namespace AlchemyAPI
5+
{
6+
public enum PageImageMode
7+
{
8+
TrustMetadata,
9+
AlwaysInfer
10+
};
11+
12+
public class AlchemyAPI_ImageParams : AlchemyAPI_BaseParams
13+
{
14+
private PageImageMode? _imageMode;
15+
private string _jsonpCallback;
16+
17+
public PageImageMode? ImageMode {
18+
get { return _imageMode; }
19+
set { _imageMode = value; }
20+
}
21+
22+
public string JsonPCallback {
23+
get { return _jsonpCallback; }
24+
set { _jsonpCallback = value; }
25+
}
26+
27+
public override string getParameterString ()
28+
{
29+
string retString = base.getParameterString ();
30+
31+
if (_imageMode != null)
32+
retString += "&extractMode=" + getImageModeString(_imageMode.Value);
33+
34+
if (_jsonpCallback != null)
35+
retString += "&jsonp=" + _jsonpCallback;
36+
37+
return retString;
38+
}
39+
40+
public static string getImageModeString (PageImageMode mode)
41+
{
42+
switch (mode)
43+
{
44+
case PageImageMode.TrustMetadata:
45+
return "trust-metadata";
46+
case PageImageMode.AlwaysInfer:
47+
return "always-infer";
48+
default:
49+
throw new ArgumentException("The specified image mode '" + mode + "' is not supported.");
50+
}
51+
}
52+
}
53+
}

module/sdk/sdk.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
<Compile Include="..\AlchemyAPI_TaxonomyParams.cs">
7676
<Link>AlchemyAPI_TaxonomyParams.cs</Link>
7777
</Compile>
78+
<Compile Include="..\AlchemyAPI_ImageParams.cs">
79+
<Link>AlchemyAPI_ImageParams.cs</Link>
80+
</Compile>
7881
</ItemGroup>
7982
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
8083
</Project>

monoDevelop/AlchemyAPI_CS_SDK/AlchemyAPI_CS_SDK.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "combined", "..\..\example\c
1111
EndProject
1212
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "taxonomy", "..\..\example\taxonomy\taxonomy.csproj", "{FD0F315B-C5F0-442F-B86C-E02886749B92}"
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "image", "..\..\example\image\image.csproj", "{B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{799E354F-E323-408C-9733-7F1896342663}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{799E354F-E323-408C-9733-7F1896342663}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{799E354F-E323-408C-9733-7F1896342663}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A}.Release|Any CPU.Build.0 = Release|Any CPU
2430
{DC4C1286-3B69-4826-AA74-AAC87B34E4ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2531
{DC4C1286-3B69-4826-AA74-AAC87B34E4ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
2632
{DC4C1286-3B69-4826-AA74-AAC87B34E4ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -38,6 +44,7 @@ Global
3844
{E61EB529-E86A-4273-940F-FA4A99378E92} = {7D111981-ED8F-4E06-B135-A72B4278C6D4}
3945
{DC4C1286-3B69-4826-AA74-AAC87B34E4ED} = {7D111981-ED8F-4E06-B135-A72B4278C6D4}
4046
{FD0F315B-C5F0-442F-B86C-E02886749B92} = {7D111981-ED8F-4E06-B135-A72B4278C6D4}
47+
{B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A} = {7D111981-ED8F-4E06-B135-A72B4278C6D4}
4148
EndGlobalSection
4249
GlobalSection(MonoDevelopProperties) = preSolution
4350
StartupItem = ..\..\example\taxonomy\taxonomy.csproj

0 commit comments

Comments
 (0)