从 javascript 中的 API 获取电影数据 [英] fetch movie data from API's in javascript

查看:135
本文介绍了从 javascript 中的 API 获取电影数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 15k 部电影的 json 对象,其中包含像这样的 IMDb ID

I have a json object of >15k movies containing IMDb ID's like this

0: ObjectID: "1."
   IdIMDb: "tt2322441"
   Title: "Fifty Shades of Grey"
   Year: 2015
1: ObjectID: "2."
   IdIMDb: "tt1617661"
   (...)

我希望用来自其他 api 的数据来完成这些数据

And I'm looking to complete this data with data from other api's

我的问题是:用这个 api 中的数据完成我的数据的最有效方式是什么?
我计划只运行这个程序一次并存储结果,以便它可以遵守 api 限制.

My question is: what is the most efficient manner to complete my data with data from this api ?
I plan to run this program only once and store the result so it can respect api restrictions.

我的第一个想法是做这样的事情

My first idea was to do something like this

data.forEach(function (d, i) {
    d.Poster = OMDbApi(d.IdIMDb);
    ...
}

function OMDbApi(i) {
    $.ajax({
        url:"http://www.omdbapi.com/?i="+i+"&plot=short&r=json",
        crossDomain: true,
        dataType: "jsonp",
        success: function (response) {
            return response.Poster;
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) { 
            error = 1;
        } 
    });
}

感谢您提供的任何帮助:-)

Thank you for any help you can provide :-)

推荐答案

var totalCalls = 15000;
    var calls = 0;
    data.forEach(function (d, i) {
    OMDbApi(I, d.IdIMDb); //posterCall
}

function OMDbApi(index, id) {
    $.ajax({
        url:"http://www.omdbapi.com/?i="+id+"&plot=short&r=json",
        crossDomain: true,
        dataType: "jsonp",
                    dataObj : index,
        success: function (response) {
            window.data[this.dataObj].Poster = response.poster;
                            calls++;
                            if (calls == totalCalls)
                            {
                               alert("We're done");
                            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) { 
            error = 1;
        } 
    });
}

这与 Ajax 的异步特性一起工作.

This work with the asynchronous nature of Ajax.

您可能希望跟踪您提出的所有请求,并在所有请求完成时显示一条消息.我添加了一个关于如何跟踪的简单示例.本质是你知道你要打多少电话.因此,您需要计算 id 的数量并将该数量乘以完成日期所需的调用.例如:每个对象都要进行3次调用才能完成数据;海报、导演和运行时.有 14500 个对象.这将导致总共 3*14500 = 43500 次调用.当调用完成时,脚本将 1 添加到变量调用.当调用次数等于 totalCalls 时,会显示警报.

You might want to keep track of all the requests you make and show a message when all the requests are finished. I've added a simple example of how to keep track. The essence is that you know how many calls you're going to make. So you need to count the amount of ids and multiply that amount by the calls needed to complete the date. For example: every object has to make three calls to complete the data; poster, director and runtime. There are 14500 objects. This will result in a total of 3*14500 = 43500 calls. The script add 1 to the variable calls when a call is completed. When the calls equals the totalCalls an alert is shown.

这篇关于从 javascript 中的 API 获取电影数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆