如何通过豆瓣API获取图书和电影列表

简介:

一直在豆瓣上收藏看过的书和电影(其他功能基本没用过),准备做个页面可以同步显示豆瓣上收藏的所有图书和电影,这个功能可以通过豆瓣提供的API来实现,并不复杂,我只是做了简单的封装,需要的可以直接拿去用,有问题可以直接留言,运行后的效果看这里 Books 或这里 我的豆瓣 ,因为豆瓣限制一分钟内访问次数不能超过40次,所以如果多人同时访问前面的链接可能看不到效果,再传个截图上来:

几点说明:

1.登录豆瓣后,可以去 这里 申请豆瓣APIKey。(不使用API Key时每分钟请求不能超过10次;使用API Key时,对访问的限制较为宽松,为每分钟40次

2.豆瓣API每次调用 最多返回50个结果 ,如果你豆瓣上的书和电影超过50个,就要多次发起调用,这部分功能我的程序里已经自动处理了。

3.我封装的这段脚本提供了一些可选配置如下,参数的含义都比较明确,这里就不解释了(place是一个div的ID,可以用来做定位)。

defaults:{
        place: "douban" ,
        user: "" ,
        api: "" ,
        book:[{stus: "reading" ,maxnum:20},{stus: "read" ,maxnum:500},{stus: "wish" ,maxnum:100}],
        movie:[{stus: "watched" ,maxnum:500},{stus: "wish" ,maxnum:200}],
        bookreadingtitle: "正读..." ,
        bookreadtitle: "读过..." ,
        bookwishtitle: "想读..." ,
        moviewatchedtitle: "看过..." ,
        moviewishtitle: "想看..."
    }

4.在你的网页里参考下面代码增加引用和调用,就可以实现类似这个页面的效果。

<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv= "Content-Type"  content= "text/html; charset=utf-8"  />
<title>豆瓣列表</title>
<style type= "text/css" >
.douban-title {
     padding: 10px 10px 0px 0px;
     text-shadow: 0 1px 0 white,1px 2px 2px  #AAA;
     font-weight: bold;
     font-size:24px;
}
.douban-list a {
     padding: 10px 10px 10px 0px;
}
</style>
<script type= 'text/javascript'  src= 'jquery-1.4.2.js' ></script>
<script type= "text/javascript"  src= "dbapi_beta1_20120316.js" ></script>
</head>
<body>
<script>
     var  _defaults = {
         user: "justin79" , //这里换成你的豆瓣ID
         api: ""           //这里换成你的豆瓣APIKEY
     }
     dbapi.show(_defaults);
</script>
</body>
</html>

整个javascript代码如下: 

//批量读取豆瓣的图书和电影
//by justin 20120316
//http://fejustin.com
//--dbapi.begin--
var  $ = jQuery;
var  dbapi = {
     appendScript: function (url){
         if  ((url)&&(url.length > 0))
             $( "<script/>" ).attr( "src" ,url).attr( "charset" , "utf-8" ).appendTo($( "head" )[0]);
     },
     /**
      * 解析json数据为数组
      */
     parseJSON: function (json){
         var  items=[];
             $.each(json.entry, function (i,item){
                 var  link = {};
                 link.title = item[ "db:subject" ][ "title" ][ "$t" ];               
                 link.link = item[ "db:subject" ][ "link" ][1][ "@href" ]; //硬编码
                 link.src = item[ "db:subject" ][ "link" ][2][ "@href" ]; //硬编码
                 items.push(link);
             });           
         return  items;
     },
     render: function (items){
         var  html= '' ;       
         $.each(items, function (i,item){
             html+= '<a href="'
                 +item.link+ '" target="_blank"><img src="'
                 +item.src+ '" alt="' +item.title
                 + '" title="' +item.title+ '"border="0" /></a>' ;
         });
         return  html;
     },
     /**
      * todo: bookurl 和 movieurl 可以合并简化
      */
     bookurl: function (stus,begin,end){
         return  this .allurl( "book" ,stus,begin,end);
     },
     movieurl: function (stus,begin,end){
         return  this .allurl( "movie" ,stus,begin,end);
     },
     allurl: function   (typ,stus,begin,end) {
         if  (end ===0) return ;
         if (!dbapi[typ + stus + "_SHOW" ]){
             dbapi[typ + stus + "_SHOW" ] = function  (json) {
                 var  mainplace = $( "#" + this .opts.place);
                 if  (mainplace.length ===0){
                     mainplace = $( "<div/>" ).attr( "id" , this .opts.place).prependTo($( "body" ));
                 }
                 if  ($( "#" +typ+stus).length === 0){
                     var  title = this .defaults[typ+stus+ "title" ]? this .defaults[typ+stus+ "title" ]:
                                  ">>>" +typ.toUpperCase() + "-" +stus.toUpperCase()+ ">>>" ;
                     $( "<span/>" ).addClass( "douban-title" ).text(title).appendTo(mainplace);
                     $( "<div/>" ).attr( "id" ,typ+stus).addClass( "douban-list" ).appendTo(mainplace);                   
                 }                   
                 $( "#" +typ+stus).append( this .render( this .parseJSON(json)));               
             }
         }
         return  this .apiurl(typ, this .opts.user, this .opts.api,stus,begin,end);
     },
     apiurl: function (typ,user,key,stus,begin,end){
         var  url = "http://api.douban.com/people/" +user+ "/collection?cat=" +typ+ "&start-index=" +
             begin+ "&max-results=" +end+ "&status=" +stus+ "&alt=xd&callback=dbapi." +typ+stus+ "_SHOW" ;
         if  (key.length > 0)
             url += "&apikey=" +key;
         return  url;
     },
     /**
      * 将num按50分段生成数组集合
      * @param  {[type]} num 显示项目的个数
      * @return {[type]} 按50分段的数组
      */   
     fixNum: function (num){
         var  len = num;       
         var  index = 1;
         var  fixnums=[];
         if  (50>len> 0){
             fixnums.push({begin:index,end:len})
         } else {
             while  (len > 0) {
                 fixnums.push({begin:index,end:index+49})
                 len -= 50;
                 index +=50;
             };   
         }
         return  fixnums;
     },
     /**
      * 根据配置项显示豆瓣的图书和电影
      * @param  {[Object]} options [可选配置项]
      */
     show: function (options){
         this .opts = $.extend({}, this .defaults, options);
         var  books = [];
         var  movies = [];
         $.each( this .opts.book, function  (i,item) {
             books.push({stus:item.stus,indexs:dbapi.fixNum(item.maxnum)});   
         });
         $.each( this .opts.movie, function  (i,item) {
             movies.push({stus:item.stus,indexs:dbapi.fixNum(item.maxnum)});   
         });
 
         $.each(books, function (i,item){   
             $.each(item.indexs, function (t,idx){
                 setTimeout(dbapi.appendScript(dbapi.bookurl(item.stus,idx.begin,idx.end)),300);
             });               
         });
 
         $.each(movies, function (i,item){   
             $.each(item.indexs, function (t,idx){
                 setTimeout(dbapi.appendScript(dbapi.movieurl(item.stus,idx.begin,idx.end)),1000);
             });               
         });       
     },
     /**
      * 可选配置项
      * @type {Object}
      * todo:可以进一步把book和movie合并到一起,通过类型区分。
      */
     defaults:{
         place: "douban" ,
         user: "" ,
         api: "" ,
         book:[{stus: "reading" ,maxnum:20},{stus: "read" ,maxnum:500},{stus: "wish" ,maxnum:100}],
         movie:[{stus: "watched" ,maxnum:500},{stus: "wish" ,maxnum:200}],
         bookreadingtitle: "正读..." ,
         bookreadtitle: "读过..." ,
         bookwishtitle: "想读..." ,
         moviewatchedtitle: "看过..." ,
         moviewishtitle: "想看..."
     }
}
//--dbapi.end--

完整的实例下载:http://files.cnblogs.com/justinw/doubanAPI_Demo.rar

本文地址:http://www.cnblogs.com/justinw/archive/2012/03/16/doubanapi.html


本文转自Justin博客园博客,原文链接:http://www.cnblogs.com/justinw/archive/2012/03/16/doubanapi.html,如需转载请自行联系原作者

相关文章
|
6天前
|
API 开发者
如何获取淘宝/天猫购物车的商品列表 API 返回值说明
淘宝/天猫的购物车API返回值会包含当前用户购物车中的商品列表及相关信息。以下是API返回值可能包含的主要字段和信息的详细说明:
|
6天前
|
存储 缓存 安全
怎样获取淘宝客商品列表api API 返回值说明?
淘宝客(Taobao Affiliate)是淘宝的联盟营销平台,允许合作伙伴通过推广商品获得佣金。淘宝客商品列表API是用来获取淘宝商品信息的接口之一。以下是淘宝客商品列表API的返回值示例和说明。请注意,实际的返回值可能会因API版本、调用参数以及淘宝平台本身的更新而有所不同,因此强烈建议查阅淘宝官方API文档以获取最准确的信息。
|
6天前
|
数据采集 数据挖掘 API
淘系API接口推荐:淘宝搜索列表数据接口
淘系API接口推荐:淘宝搜索列表数据接口
233 0
|
6天前
|
API
uni-app 146朋友圈列表api开发
uni-app 146朋友圈列表api开发
21 0
|
6天前
|
API 网络安全
调用钉钉的API获取审批实例ID列表时返回的结果为
调用钉钉的API获取审批实例ID列表时返回的结果为【1月更文挑战第5天】【1月更文挑战第21篇】
52 1
|
5月前
|
JSON API 开发者
淘宝获取购物车的商品列表 API接口
淘宝提供了获取购物车商品列表 API 接口,允许开发者通过编程方式获取用户购物车中的商品列表。这个 API 接口可以帮助开发者更好地了解用户在购物车中添加了哪些商品,以及每个商品的基本信息,例如商品 ID、名称、价格、数量等。
|
6月前
|
JSON API 数据格式
快手API接口:根据关键词获取海量商品列表的秘诀
快手API接口:根据关键词获取海量商品列表的秘诀
|
6天前
|
数据挖掘 Java API
唯品会商品列表数据接口(Vip.item_search)丨唯品会 API 接口
唯品会商品列表数据接口(Vip.item_search)丨唯品会 API 接口
63 1
|
6天前
|
存储 缓存 运维
DataWorks操作报错合集之DataWorks根据api,调用查询文件列表接口报错如何解决
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
26 1
|
6天前
|
API Python
[AIGC] Python列表([])和字典({})常用API介绍
[AIGC] Python列表([])和字典({})常用API介绍

热门文章

最新文章