PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/fishpond.co.nz.js

https://gitlab.com/mba811/translators
JavaScript | 215 lines | 183 code | 12 blank | 20 comment | 24 complexity | e664d972a4657efd5e18894c2989d715 MD5 | raw file
  1. {
  2. "translatorID": "c436f3c7-4246-4ed3-a227-a538c8113a0e",
  3. "label": "fishpond.co.nz",
  4. "creator": "Sopheak Hean, Sebastian Karcher",
  5. "target": "^https?://www\\.fishpond\\.co\\.nz/",
  6. "minVersion": "1.0",
  7. "maxVersion": "",
  8. "priority": 100,
  9. "inRepository": true,
  10. "translatorType": 4,
  11. "browserSupport": "gcsibv",
  12. "lastUpdated": "2012-10-07 14:06:56"
  13. }
  14. /*
  15. Fishpond.co.nz Translator- Parses Fishpond.co.nz articles and creates Zotero-based metadata
  16. Copyright (C) 2011 Sopheak Hean, University of Waikato, Faculty of Education
  17. Contact: maxximuscool@gmail.com
  18. This program is free software: you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License as published by
  20. the Free Software Foundation, either version 3 of the License, or
  21. (at your option) any later version.
  22. This program is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. GNU General Public License for more details.
  26. You should have received a copy of the GNU General Public License
  27. along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. */
  29. function detectWeb(doc, url) {
  30. var definePath = '//td[contains(@class, "product_info")]//h1';
  31. var XpathObject = doc.evaluate(definePath, doc, null, XPathResult.ANY_TYPE, null).iterateNext();
  32. if (XpathObject) {
  33. return "book";
  34. } else {
  35. var definePath = '//td[@id="page_title"]/h1';
  36. var XpathObject = doc.evaluate(definePath, doc, null, XPathResult.ANY_TYPE, null).iterateNext();
  37. if (XpathObject) {
  38. return "multiple";
  39. }
  40. }
  41. }
  42. function scrape(doc, url) {
  43. var newItem = new Zotero.Item("book");
  44. var title = '//span[@class="fn"]';
  45. var titleObject = doc.evaluate(title, doc, null, XPathResult.ANY_TYPE, null).iterateNext();
  46. if (titleObject){
  47. newItem.title = titleObject.textContent;
  48. }
  49. var author = '//p[@id="product_author"]';
  50. var authorObject = doc.evaluate(author, doc, null, XPathResult.ANY_TYPE, null).iterateNext();
  51. if (authorObject){
  52. authorObject = authorObject.textContent;
  53. if (( authorObject.match(/By\s/)) && (authorObject.match(/\([A-Za-z]+\W[a-zA-Z]+\)/) )){
  54. authorObject = ZU.trimInternal(authorObject).replace(/By\s/, '').replace(/\([A-Za-z]+\W[a-zA-Z]+\)/, '').split(",");
  55. var i = 0;
  56. while (authorObject[i]){
  57. newItem.creators.push(Zotero.Utilities.cleanAuthor(authorObject[i], "author"));
  58. i++;
  59. }
  60. }
  61. else if (authorObject.match(/By\W/)) {
  62. authorObject = authorObject.replace(/By\s/, '').split(",");
  63. var i = 0;
  64. while (authorObject[i]){
  65. newItem.creators.push(Zotero.Utilities.cleanAuthor(authorObject[i], "author"));
  66. i++;
  67. }
  68. }
  69. }
  70. var date = '//table[@class="product_info_text"]/tbody/tr[3]';
  71. var dateObject = doc.evaluate(date, doc, null, XPathResult.ANY_TYPE, null).iterateNext();
  72. if (dateObject){
  73. dateObject = dateObject.textContent;
  74. if (dateObject.match(/Release Date:\s/)){
  75. newItem.date = dateObject.replace(/Release Date:\s/, '');
  76. } else {
  77. var d = new Date();
  78. date ='//span[@class="arrival_time"]';
  79. dateObject = doc.evaluate(date, doc, null, XPathResult.ANY_TYPE, null).iterateNext();
  80. if(dateObject){
  81. newItem.date = dateObject.textContent.replace(/Available\s/, '')+ " " +d.getFullYear()
  82. ;
  83. }
  84. }
  85. }
  86. var abstract = '//table[@class="product_info_text"]/tbody/tr/td/div[@class="description"]';
  87. var abstractObject = doc.evaluate(abstract, doc, null, XPathResult.ANY_TYPE, null).iterateNext();
  88. if (abstractObject){
  89. abstractObject = abstractObject.textContent;
  90. newItem.abstractNote = abstractObject;
  91. }
  92. var isbn = "//table/tbody/tr/td[2]/table[4]/tbody/tr[2]/td[2]";
  93. var isbnObject = doc.evaluate(isbn, doc, null, XPathResult.ANY_TYPE, null).iterateNext();
  94. if (isbnObject){
  95. newItem.ISBN = isbnObject.textContent;
  96. }
  97. var publisher = "//table/tbody/tr/td[2]/table[4]/tbody/tr[1]/td[2]/a";
  98. var publisherObject = doc.evaluate(publisher, doc, null, XPathResult.ANY_TYPE, null).iterateNext();
  99. if (publisherObject) {
  100. newItem.publisher= publisherObject.textContent;
  101. }
  102. newItem.attachments.push({title:"FishPond Record", mimeType:"text/html", url:doc.location.href});
  103. newItem.complete();
  104. }
  105. function doWeb(doc, url) {
  106. var articles = new Array();
  107. if (detectWeb(doc, url) == "multiple") {
  108. var items = new Object();
  109. var titles = '//div[@style="padding-bottom:1em;"]/a';
  110. var titleObject = doc.evaluate(titles, doc, null, XPathResult.ANY_TYPE, null);
  111. var next_title;
  112. while ( next_title = titleObject.iterateNext()) {
  113. items[next_title.href] = next_title.textContent;
  114. }
  115. Zotero.selectItems(items, function (items) {
  116. if (!items) {
  117. return true;
  118. }
  119. for (var i in items) {
  120. articles.push(i);
  121. }
  122. Zotero.Utilities.processDocuments(articles, scrape, function () {
  123. Zotero.done();
  124. });
  125. });
  126. } else {
  127. scrape(doc, url);
  128. }
  129. }
  130. /** BEGIN TEST CASES **/
  131. var testCases = [
  132. {
  133. "type": "web",
  134. "url": "http://www.fishpond.co.nz/Books/Pippi-Longstocking-Astrid-Lindgren/9780670014040?cf=3&rid=2103878406&i=3&keywords=lindgren",
  135. "items": [
  136. {
  137. "itemType": "book",
  138. "creators": [
  139. {
  140. "firstName": "Astrid",
  141. "lastName": "Lindgren",
  142. "creatorType": "author"
  143. }
  144. ],
  145. "notes": [],
  146. "tags": [],
  147. "seeAlso": [],
  148. "attachments": [
  149. {
  150. "title": "FishPond Record",
  151. "mimeType": "text/html",
  152. "url": "http://www.fishpond.co.nz/Books/Pippi-Longstocking-Astrid-Lindgren/9780670014040?cf=3&rid=2103878406&i=3&keywords=lindgren"
  153. }
  154. ],
  155. "title": "Pippi Longstocking",
  156. "abstractNote": "The classic novel about the little girl with crazy red pigtails and a flair for the outrageous is available once again in this large-format gift edition. Full color.",
  157. "libraryCatalog": "fishpond.co.nz"
  158. }
  159. ]
  160. },
  161. {
  162. "type": "web",
  163. "url": "http://www.fishpond.co.nz/Books/Best-of-Pippi-Longstocking-Astrid-Lindgren-Tony-Ross-Illustrated-by/9780192753373",
  164. "items": [
  165. {
  166. "itemType": "book",
  167. "creators": [
  168. {
  169. "firstName": "Astrid",
  170. "lastName": "Lindgren",
  171. "creatorType": "author"
  172. },
  173. {
  174. "firstName": "Tony",
  175. "lastName": "Ross",
  176. "creatorType": "author"
  177. }
  178. ],
  179. "notes": [],
  180. "tags": [],
  181. "seeAlso": [],
  182. "attachments": [
  183. {
  184. "title": "FishPond Record",
  185. "mimeType": "text/html"
  186. }
  187. ],
  188. "title": "The Best of Pippi Longstocking",
  189. "abstractNote": "Pippi Longstocking is as popular as ever, with dedicated fans all over the world. She's funny, feisty, and incredibly strong and has the most amazing adventures ever! Here's a chance to read three books about Pippi in one volume - Pippi Longstocking, Pippi Goes Aboard, and Pippi in the South Seas. * Pippi Longstocking has phenomenal sales and has been in print continuously for over forty years * Illustrated throughout by best-selling artist, Tony Ross, who has illustrated a new cover for this edition * Astrid Lindgren has won numerous awards including the Hans Christian Andersen Award and the International Book Award.",
  190. "libraryCatalog": "fishpond.co.nz"
  191. }
  192. ]
  193. },
  194. {
  195. "type": "web",
  196. "url": "http://www.fishpond.co.nz/c/Books/a/Astrid+Lindgren",
  197. "items": "multiple"
  198. }
  199. ]
  200. /** END TEST CASES **/