PageRenderTime 5340ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/jieshuhuiyou/service/impl/DoubanServiceImpl.java

https://bitbucket.org/psjay/ants-bookbase
Java | 199 lines | 168 code | 22 blank | 9 comment | 69 complexity | 6cfa4264bad28a6ebcd293d584cbd9b0 MD5 | raw file
  1. package com.jieshuhuiyou.service.impl;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.net.HttpURLConnection;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import org.springframework.stereotype.Service;
  9. import com.alibaba.fastjson.JSON;
  10. import com.alibaba.fastjson.JSONArray;
  11. import com.alibaba.fastjson.JSONObject;
  12. import com.jieshuhuiyou.entity.Book;
  13. import com.jieshuhuiyou.exceptions.DoubanException;
  14. import com.jieshuhuiyou.service.DoubanService;
  15. @Service
  16. public class DoubanServiceImpl implements DoubanService {
  17. private static final String DOUBAN_API_KEY = "04276510145b9c5c0c762f303d12d514";
  18. @SuppressWarnings("unused")
  19. private static final String DOUBAN_SECRET = "8065eaf7e69ed07f";
  20. private static final String DOUBAN_URL_BASE = "http://api.douban.com/book/";
  21. @Override
  22. public Book getBookByISBN(String isbn) throws DoubanException {
  23. try {
  24. URL url = new URL(DOUBAN_URL_BASE + "subject/isbn/" + isbn + "?apikey=" + DOUBAN_API_KEY
  25. + "&alt=json");
  26. String responseContent = doHttpRequest(url);
  27. if(responseContent == null) { //404
  28. return null;
  29. }
  30. return jsonStringToBook(responseContent);
  31. } catch (MalformedURLException e) {
  32. e.printStackTrace();
  33. }
  34. return null;
  35. }
  36. @Override
  37. public Book getBookByDoubanId(String doubanId) throws DoubanException {
  38. try {
  39. URL url = new URL(DOUBAN_URL_BASE + "subject/" + doubanId + "?apikey=" + DOUBAN_API_KEY
  40. + "&alt=json");
  41. String responseContent = doHttpRequest(url);
  42. if(responseContent == null) { //404
  43. return null;
  44. }
  45. return jsonStringToBook(responseContent);
  46. } catch (MalformedURLException e) {
  47. e.printStackTrace();
  48. }
  49. return null;
  50. }
  51. private String doHttpRequest(URL url) throws DoubanException {
  52. HttpURLConnection connection = null;
  53. BufferedReader bw = null;
  54. String content = "";
  55. try {
  56. connection = (HttpURLConnection) url.openConnection();
  57. connection.setConnectTimeout(10 * 1000);
  58. connection.setReadTimeout(8 * 1000);
  59. connection.connect();
  60. if(connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
  61. return null;
  62. }
  63. bw = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
  64. String temp;
  65. while((temp = bw.readLine()) != null){
  66. content += temp;
  67. }
  68. return content;
  69. } catch (IOException e) {
  70. //FIXME: there should be logged
  71. throw new DoubanException(e.getMessage(), "??????????");
  72. }
  73. }
  74. private Book jsonStringToBook(String jsonStr) {
  75. Book book = new Book();
  76. JSONObject jsonObject = JSON.parseObject(jsonStr);
  77. if(!jsonObject.containsKey("title")) {
  78. return null;
  79. }
  80. //set title
  81. book.setTitle(jsonObject.getJSONObject("title").getString("$t"));
  82. //set content
  83. book.setIntro(jsonObject.getJSONObject("summary").getString("$t"));
  84. //set author (separating with space)
  85. JSONArray authors = jsonObject.getJSONArray("author");
  86. String author = null;
  87. if(authors.size() > 0) {
  88. author = authors.getJSONObject(0).getJSONObject("name").getString("$t");
  89. if(authors.size() > 1) {
  90. for(int i = 1; i < authors.size(); i++) {
  91. author += " " + authors.getJSONObject(i).getJSONObject("name").getString("$t");
  92. }
  93. }
  94. }
  95. book.setAuthor(author);
  96. // set rating
  97. JSONObject ratingObject = jsonObject.getJSONObject("gd:rating");
  98. if(ratingObject != null) {
  99. float averageRating = 0;
  100. String averageStr = ratingObject.getString("@average");
  101. if(averageStr != null) {
  102. averageRating = Float.parseFloat(averageStr);
  103. book.setRate(averageRating);
  104. }
  105. }
  106. //FIXME: this implementation of coverting json string to Book object is bad
  107. // set attributes (isbn10, isbn13, author-intro, price, publisher, publishdate, pages, traslator)
  108. JSONArray attributes = jsonObject.getJSONArray("db:attribute");
  109. JSONObject attrItem = null;
  110. for(int i = 0; i < attributes.size(); i++) {
  111. attrItem = attributes.getJSONObject(i);
  112. if(attrItem != null) {
  113. if(attrItem.getString("@name") != null
  114. && attrItem.getString("@name") .equals("isbn10")) {
  115. if(attrItem.getString("$t") != null) {
  116. book.setIsbn10(attrItem.getString("$t"));
  117. }
  118. } else if(attrItem.getString("@name") != null
  119. && attrItem.getString("@name") .equals("isbn13")) {
  120. if(attrItem.getString("$t") != null) {
  121. book.setIsbn13(attrItem.getString("$t"));
  122. }
  123. } else if(attrItem.getString("@name") != null
  124. && attrItem.getString("@name") .equals("author-intro")) {
  125. if(attrItem.getString("$t") != null) {
  126. book.setAuthorIntro(attrItem.getString("$t"));
  127. }
  128. } else if(attrItem.getString("@name") != null
  129. && attrItem.getString("@name") .equals("price")) {
  130. if(attrItem.getString("$t") != null) {
  131. book.setPrice(attrItem.getString("$t"));
  132. }
  133. } else if(attrItem.getString("@name") != null
  134. && attrItem.getString("@name") .equals("publisher")) {
  135. if(attrItem.getString("$t") != null) {
  136. book.setPublisher(attrItem.getString("$t"));
  137. }
  138. } else if(attrItem.getString("@name") != null
  139. && attrItem.getString("@name") .equals("pubdate")) {
  140. if(attrItem.getString("$t") != null) {
  141. book.setPublishDate(attrItem.getString("$t"));
  142. }
  143. } else if(attrItem.getString("@name") != null
  144. && attrItem.getString("@name") .equals("pages")) {
  145. if(attrItem.getString("$t") != null) {
  146. String pagesStr = attrItem.getString("$t");
  147. pagesStr = pagesStr.replaceAll("\\D+", ""); // replace all non-digit chars
  148. book.setPages(Integer.parseInt(pagesStr));
  149. }
  150. } else if(attrItem.getString("@name") != null
  151. && attrItem.getString("@name") .equals("translator")) {
  152. if(attrItem.getString("$t") != null) {
  153. book.setTranslator(attrItem.getString("$t"));
  154. }
  155. }
  156. }
  157. }
  158. // set douban id
  159. String idString = jsonObject.getJSONObject("id").getString("$t");
  160. String doubanId = idString.replaceFirst("http://api.douban.com/book/subject/", "");
  161. book.setDoubanId(doubanId);
  162. //set image
  163. JSONArray links = jsonObject.getJSONArray("link");
  164. JSONObject linkItem = null;
  165. for(int i = 0; i < links.size(); i++) {
  166. linkItem = links.getJSONObject(i);
  167. if(linkItem.getString("@rel") != null
  168. && linkItem.getString("@rel").equals("image")) {
  169. String spic = linkItem.getString("@href");
  170. book.setSmallImg(spic);
  171. String mpic = spic.replaceFirst("spic", "mpic");
  172. book.setMiddleImg(mpic);
  173. }
  174. }
  175. return book;
  176. }
  177. }