var
$ = jQuery;
var
dbapi = {
appendScript:
function
(url){
if
((url)&&(url.length > 0))
$(
"<script/>"
).attr(
"src"
,url).attr(
"charset"
,
"utf-8"
).appendTo($(
"head"
)[0]);
},
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;
},
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;
},
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;
},
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);
});
});
},
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:
"想看..."
}
}