How to obtain the list of books and movies through the Douban API

Source: Internet
Author: User

I have been adding books and movies to my favorites on Douban (other functions have never been used). I am going to make a page to display all the books and movies added to my favorites on Douban, this function can be implemented through the APIS provided by Douban. It is not complicated. I just made a simple encapsulation,You can use the required information directly.If you have any questions, please leave a message directly. For the effect after running, check books or my Douban here. Because Douban cannot exceed 40 times in a minute, therefore, if multiple users access the previous link at the same time, they may not be able to see the effect:

Notes:

1. After logging on to Douban, you can apply for apikey here. (When no API key is usedThe number of requests per minute cannot exceed 10. When using the API key, the access limit is relatively loose, that is, 40 requests per minute.)

2. The Douban API can return up to 50 results each time it is called. If you have more than 50 books and movies on Douban, You need to initiate multiple calls.Program.

3. The script I encapsulated provides some optional configurations as follows. The parameter meanings are clear and will not be explained here (place is the ID of a div and can be used for positioning ).

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: "reading... ", bookreadtitle:" read... ", bookwishtitle:" want to read... ", moviewatchedtitle:" read... ", moviewishtitle:" want to see... "}

4. refer to the following on your webpage:CodeBy adding references and calls, you can achieve a page-like effect.

<HTML xmlns = "http://www.w3.org/1999/xhtml">  

The entire JavaScript code is as follows: 

// Read books and movies of Douban in batches // 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]) ;},/*** parses JSON data as an array */parsejson: function (JSON) {var items = []; $. each (JSON. entry, function (I, item) {var link ={}; Link. title = item ["DB: subject"] ["title"] ["$ t"]; L Ink. link = item ["DB: subject"] ["Link"] [1] ["@ href"]; // hard-coded link. src = item ["DB: subject"] ["Link"] [2] ["@ href"]; // hard-coded items. push (Link) ;}); return items ;}, render: function (items) {var html = ''; $. each (items, function (I, item) {HTML + = '<a href = "' + item. link + '"target =" _ blank ">  </a>';}); Return HTML;},/*** todo: bookurl and Mo Vieurl can be merged and simplified */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 ;}, /*** use num to generate an array set in 50 segments * @ Param {[type]} num to display the number of projects * @ return {[type]} an array in 50 segments */ 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) {fi Xnums. push ({begin: Index, end: index + 49}) len-= 50; index + = 50 ;}}return fixnums ;}, /*** @ * @ Param {[object]} options [optional configuration items] */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, it EM) {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);},/*** optional configuration Item * @ type {object} * todo: The book and movie can be further merged and distinguished by type. */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: "reading... ", bookreadtitle:" read... ", bookwishtitle:" want to read... ", moviewatchedtitle:" read... ", moviewishtitle:" want to see... "}}// -- dbapi. end --

Download the complete instance:Http://files.cnblogs.com/justinw/doubanAPI_Demo.rar

Address: http://www.cnblogs.com/justinw/archive/2012/03/16/doubanapi.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.