www.fgks.org   »   [go: up one dir, main page]

Help Center

Getting Started with the IMDb API

The IMDb API delivers content via AWS Data Exchange. This article has everything you need to request access and start querying the IMDb API.

Requesting an IMDb API trial

Free trials for the IMDb API are requested via IMDb's AWS Marketplace:

  1. Sign up for an AWS account
  2. Go to AWS Data Exchange and go to ‘Browse Catalog’. Search for IMDb and select ‘API’ under ‘Data available through’. This will show you the products below:
  3. FindDataset.png

    3. Select the product you wish to subscribe to. Here you can find more information about the product. Click ‘Continue to subscribe’.
    Subscribe1.png

    4. From here you can complete a subscription request for the product. You should review the product offer and subscription terms. Then you can complete a subscription request using the available form and click ‘Send subscription request to provider’.
    Screenshot 2021-11-26 at 08.54.58.pngScreenshot 2021-11-26 at 08.56.01.png

    5. Your subscription request will be processed. You can find your request under ‘Subscriptions requests’. Once approved the status will be updated here, you will also receive an email notification.
    SubscriptionRequest.png

    6. Once your request has been approved you can find your subscription details under ‘Subscriptions’. You are now ready to access the API. Please see the “Getting Started with the IMDb API” documentation.
    subscriptions.png

    7. To access the API you will need an endpoint, data-set-id, revision-id and asset-id for your product. These are unique for each product and can be found in the AWS DataExchange console under ‘Entitled data’. From here select the product you wish to access, and go to the most recent revision, then click on the API asset under this revision. Here you should see an ‘Asset Overview’ section. This will give details of the endpoint, data-set-id, revision-id and asset-id you will need. All three data-set use the same endpoint: https://atlas.us-east-1.datax.aws.a2z.com/prod/v1 .

Creating an Access Key

An access key is not sent to you automatically, you must generate one though the AWS console. You can generate your accessKeyId and secretAccessKey from the AWS console:

  1. Sign into you AWS account and in the navigation bar on the upper right, choose your account name or number and then choose My Security Credentials.
  2. Expand the "Access keys (access key ID and secret access key)" section.
  3. To create an access key, choose "Create New Access Key". If you already have two access keys, this button is disabled; you must delete an access key before you can create a new one. 
  4. When prompted, choose either "Show Access Key" or "Download Key File". This is your only opportunity to save your secret access key. After you've saved your secret access key in a secure location, chose "Close".
For more information on generating Access Keys see here.

Accessing the IMDb API

All IMDb APIs use GraphQL for querying the data sets. Using GraphQL and its well-defined schema makes it easy to query IMDb APIs as clients do not have to write custom validation logic on the results, and it also improves efficiency as clients can query exactly the fields they are interested in without wasting bandwidth and resources over-fetching and iterating through data. 

Once you subscribe to a plan on AWS Data Exchange, you will receive access to an endpoint (https://atlas.us-east-1.datax.aws.a2z.com/prod/v1/) that you can use to query IMDb data sets. Requests to the endpoint must be signed with Signature Version 4 (SigV4) so that AWS can identify who sent them.  Use the credentials (AWS access key ID and AWS secret access key) you create via your AWS account to sign your requests to query IMDb. 

To access the API you will need an endpoint, data-set-id, revision-id and asset-id for your product. These are unique for each product and can be found in the AWS Data Exchange console under "Entitled data". From here, select the product you wish to access, go to the most recent revision, then click on the API asset under this revision. Here, you should see an "Asset Overview" section. This will give details of the endpoint,  data-set-idrevision-id and asset-id you need.

All three data-set use the same endpoint:
https://atlas.us-east-1.datax.aws.a2z.com/prod/v1

Querying the IMDb API

For this example, we will use a GraphQL query to request data on Titanic (1997)
{
title(id: "tt0120338") {
runtime {
seconds
}
}
}
Save this in a file titanicRuntimeQuery.graphql. The id value in the query comes from the identifier IMDb uses for this title.


Example in TypeScript

Install Node.js
  • Create a project with the following command.
$ npm init
  • Install required packages.
$ npm install --save aws4
$ npm install --save-dev @types/node
  • Make a request with this sample code.
import { sign } from "aws4";
import { readFileSync } from "fs";
import { request } from "https";

const asyncRequest = async (
options: any
): Promise<{
statusCode?: number;
body: string;
}> => {
return new Promise((resolve, reject) => {
const req = request(options, function (res) {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
resolve({ statusCode: res.statusCode, body: data });
});
});
req.on("error", (err) => {
reject(err);
});
if (options.body) req.write(options.body);
req.end();
});
};

(async () => {
// replace this with your actual accessKeyId from AWS credentials here
const accessKeyId = "ABCDEFGHIJK12345";

// replace this with your actual secretAccessKey from AWS credentials here
const secretAccessKey = "Abcdefg/1234+hiJKLmnopqrstuvwxYz";

const service = "dataexchange";

const region = "us-east-1";

// query we saved earlier
const titanicRuntimeQuery = readFileSync("./titanicRuntimeQuery.graphql", "utf-8");

const body = JSON.stringify({ query: titanicRuntimeQuery });

// change to any desired ISO 639-1 country or language code
const headers = {
"Content-Type": "application/json",
"x-amzn-dataexchange-asset-id": "your-asset-id",
"x-amzn-dataexchange-revision-id": "your-revision-id",
"x-amzn-dataexchange-data-set-id": "your-data-set-id",
};

const host = "atlas.us-east-1.datax.aws.a2z.com";
const method = "POST";
const path = "/prod/v1";

// sign the request
const options = sign({
body,
headers,
host,
method,
path,
service,
region,
},
{
accessKeyId,
secretAccessKey
}
);

// make the request
const response = await asyncRequest(
options
);

// print the response body and status to console
console.log(response.statusCode);
console.log(response.body.trim());
})();
  • Evaluate the response
Executing this script should return a JSON response like this on the console:
{
"data": {
"title": {
"runtime": {
"seconds": 11640
}
}
},
"extensions": {
"disclaimer": "Use of the IMDb data provided by this API is governed by the AWS Data Subscription Agreement and IMDb Content License Agreement."
}
}
The response to our query “what is the runtime for Titanic?” is 11640 seconds.

Example in Java

  • Follow this guide to configure Maven to use aws-java-sdk.

  • Add this BOM to pom.xml, with the latest available version.

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.1000</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
  • Specify individual module from the SDK in pom.xml.
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
</dependency>
</dependencies>
  • Add the following maven-compiler-plugin configuration in pom.xml.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>dependency/</classpathPrefix>
<mainClass>org.example.basicapp.AccessingImdbApiExample</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
  • Make a request with this sample code.
import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.DefaultRequest;
import com.amazonaws.Request;
import com.amazonaws.auth.AWS4Signer;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.http.AmazonHttpClient;
import com.amazonaws.http.ExecutionContext;
import com.amazonaws.http.HttpMethodName;
import com.amazonaws.http.HttpResponse;
import com.amazonaws.http.HttpResponseHandler;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public final class AccessingImdbApiExample {

private static final String ENDPOINT = "https://atlas.us-east-1.datax.aws.a2z.com";
private static final String REGION = "us-east-1";
private static final String PATH = "prod/v1";

// replace this with your actual ACCESS_KEY_ID from AWS credentials here
private static final String ACCESS_KEY_ID = "ABCDEFGHIJK12345";

// replace this with your actual SECRET_ACCESS_KEY from AWS credentials here
private static final String SECRET_ACCESS_KEY = "Abcdefg/1234+hiJKLmnopqrstuvwxYz";

public static void main(String[] args) throws IOException {

// Query we saved earlier
String titanicRuntimeQuery = new String(Files.readAllBytes(Paths.get("src/titanicRuntimeQuery.graphql")), StandardCharsets.UTF_8);
String titanicRuntimeQueryString = "{\"query\":\"" + titanicRuntimeQuery.replace("\"", "\\\"").replaceAll("\\s+","") + "\"}";

// Construct a request.
Request<?> request = new DefaultRequest<Void>("dataexchange");
request.setEndpoint(URI.create(ENDPOINT));
request.setResourcePath(PATH);
request.setHttpMethod(HttpMethodName.POST);
request.addHeader("Content-Type", "application/json");
request.addHeader("x-amzn-dataexchange-asset-id", "your-asset-id");
request.addHeader("x-amzn-dataexchange-revision-id", "your-revision-id");
request.addHeader("x-amzn-dataexchange-data-set-id", "your-data-set-id");
request.setContent(new ByteArrayInputStream(titanicRuntimeQueryString.getBytes(StandardCharsets.UTF_8)));

// Create an AWSv4 signer
AWS4Signer signer = new AWS4Signer();
signer.setRegionName(REGION);
signer.setServiceName("dataexchange");

// Sign the request
signer.sign(request, new BasicAWSCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));

// Make the request
ClientConfiguration clientConfiguration = new ClientConfiguration().withMaxConnections(10);
AmazonHttpClient client = new AmazonHttpClient(clientConfiguration);
AmazonHttpClient.RequestExecutionBuilder builder = client.requestExecutionBuilder()
.request(request)
.executionContext(new ExecutionContext(true))
.errorResponseHandler(new HttpResponseHandler<AmazonServiceException>() {
@Override
public AmazonServiceException handle(HttpResponse httpResponse) {
AmazonServiceException ase = new AmazonServiceException("Amazon Service Exception.");
ase.setStatusCode(httpResponse.getStatusCode());
ase.setErrorCode(httpResponse.getStatusText());
return ase;
}

@Override
public boolean needsConnectionLeftOpen() {
return false;
}
});

// Get the response
HttpResponse response = builder
.execute(new HttpResponseHandler<String>() {
@Override
public String handle(HttpResponse httpResponse) {
System.out.println("Response body = " + inputStreamToString(httpResponse.getContent()));
return null;
}

@Override
public boolean needsConnectionLeftOpen() {
return false;
}
})
.getHttpResponse();
System.out.println("Response status code = " + response.getStatusCode());
}

private static String inputStreamToString(InputStream inputStream) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int length; (length = inputStream.read(buffer)) != -1; ) {
result.write(buffer, 0, length);
}
return result.toString("UTF-8");
}

}
  • Run the following commands
mvn install clean
mvn package
mvn dependency:copy-dependencies
java -jar target/myapp-1.0.0.jar
  • Evaluate the response
Executing this code should return a JSON response like this on the console:
{
"data": {
"title": {
"runtime": {
"seconds": 11640
}
}
},
"extensions": {
"disclaimer": "Use of the IMDb data provided by this API is governed by the AWS Data Subscription Agreement and IMDb Content License Agreement."
}
}
The response to our query “what is the runtime for Titanic?” is 11640 seconds.

If you are not familiar with our IMDb title and name IDs that serve as our system of organization for pages and make up the backbone of IMDb, everything you need to get started can be found in our Documentation and Data Dictionary.
Did this answer your question?
Thank you! We’d love to hear more about your experience. Please help us improve by taking this 2 minute survey.
Thank you! We’d love to hear more about your experience. Please help us improve by taking this 2 minute survey.
Thank you! We’d love to hear more about your experience. Please help us improve by taking this 2 minute survey.
Back to top