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

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

问题描述

我的> 15K电影包含IMDB编号的这样一个JSON对象

  0:对象ID:1。
   IdIMDb:tt2322441
   标题:灰色五十灯罩
   年份:2015年
1:对象ID:2。
   IdIMDb:tt1617661
   (...)
 

和我期待完成这一数据与其他的API

数据

我的问题是:什么是最有效的方式来与这个API数据完成我的数据
? 我计划运行此程序一次,并将结果存入因此它能够尊重API的限制。

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

  data.forEach(功能(D,I){
    d.Poster = OMDbApi(d.IdIMDb);
    ...
}

功能OMDbApi(ⅰ){
    $阿贾克斯({
        网址:http://www.omdbapi.com/?i=+ I +&放大器;情节=短的放大器; R = JSON
        跨域:真正的,
        数据类型:JSONP
        成功:函数(响应){
            返回response.Poster;
        },
        错误:函数(XMLHtt prequest,textStatus,errorThrown){
            错误= 1;
        }
    });
}
 

感谢您的帮助,您可以提供: - )

解决方案

  VAR totalCalls = 15000;
    变种调用= 0;
    data.forEach(功能(D,I){
    OMDbApi(Ⅰ,d.IdIMDb); // posterCall
}

功能OMDbApi(索引ID){
    $阿贾克斯({
        网址:http://www.omdbapi.com/?i=+编号+&放大器;情节=短的放大器; R = JSON
        跨域:真正的,
        数据类型:JSONP
                    dataObj:指数,
        成功:函数(响应){
            window.data [this.dataObj] .Poster = response.poster;
                            呼吁++;
                            如果(调用== totalCalls)
                            {
                               警报(我们正在做的);
                            }
        },
        错误:函数(XMLHtt prequest,textStatus,errorThrown){
            错误= 1;
        }
    });
}
 

这工作使用Ajax的异步性。

  

您可能希望将所有你做,并显示一条消息,当所有的请求都完成了要求的轨道。我已经添加了如何跟踪一个简单的例子。其实质是,你知道你要多少电话要打。所以,你需要计算ID的数量和完成日期所需的通话乘以数量。例如:每一个对象必须做出三个电话来完成数据的; 海报,导演和运行的。有14500的对象。这将导致整个 3×14500 = 43500 通话。该脚本添加1到变量呼叫时呼叫完成。当呼叫等于totalCalls示出一个警报。

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"
   (...)

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

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;
        } 
    });
}

This work with the asynchronous nature of Ajax.

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.

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

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