Last active
July 21, 2019 02:28
-
-
Save NonlinearFruit/25234331099cec554b02551969fd7270 to your computer and use it in GitHub Desktop.
Retrospector Book Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.goodreads.com/book/title.xml?author=Arthur+Conan+Doyle&key=zj1nZyl5vy2F4vGRt2Elg&title=Hound+of+the+Baskervilles | |
/** | |
This plugin is only meant to handle the 'Book' category. | |
It is not meant to for anything else. Assumptions: | |
- 'Year' factoid exists | |
- The title is either in the title or episode field | |
- Book episodes are of the form 'B1 Scarlet Thread, The' | |
- If there is a comma in the title, it is only followed by 'A', 'An' or 'The' | |
**/ | |
// 'Import' the necessary classes | |
var DataScraper = Java.type('retrospector.fxml.media.JsDataScraper'); | |
var Media = Java.type('retrospector.model.Media'); | |
var Factoid = Java.type('retrospector.model.Factoid'); | |
// Variables to set | |
var apiKey = 'zj1nZyl5vy2F4vGRt2Elg'; | |
var baseUrl = 'https://www.goodreads.com/book/title.xml?key='+apiKey; | |
// Extend the abstract 'JsDataScraper' class and create an instance | |
var scraper = new DataScraper{ | |
// This is the method that will be called and given a media object | |
autocomplete: function(media) { | |
// Get values and remove ', The' and 'E1' stuff | |
var title = media.getTitle().replace(/,.*/g, "").trim(); | |
var creator = media.getCreator().replace(/,.*/g, "").replace(/[A-Z]\d+/g, "").trim(); | |
var episode = media.getEpisode().replace(/,.*/g, "").replace(/[A-Z]\d+/g, "").trim(); | |
var category = media.getCategory(); | |
// Make the url | |
var url; | |
if (episode) | |
url = baseUrl + '&title=' + episode; | |
else if (title) | |
url = baseUrl + '&title=' + title; | |
else | |
return media; | |
// if (creator) // This causes Pride and Prejudice | |
// url += '&author=' + creator; // by Jane Austen to fail | |
// Get some data! | |
var xml = DataScraper.getUrlContent(url); | |
var dom = DataScraper.convertStringToDocument(xml); | |
// Get the media | |
var mediaElement = dom.getElementsByTagName('book').item(0); | |
// Add description | |
var description = mediaElement.getElementsByTagName('description').item(0).firstChild.nodeValue; | |
if (description) | |
media.setDescription(description); | |
// Add year factoid | |
var year = mediaElement.getElementsByTagName('work').item(0).getElementsByTagName('original_publication_year').item(0).firstChild; | |
if (year) | |
media.getFactoids().add(new Factoid('Year',year.nodeValue)); | |
return media; | |
} | |
}; | |
// 'Return' the scraper object | |
scraper; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Retrospector is a desktop app for rating and reviewing media (movies, books, tv shows, etc)