Skip to content

Instantly share code, notes, and snippets.

@NonlinearFruit
Last active July 21, 2019 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NonlinearFruit/25234331099cec554b02551969fd7270 to your computer and use it in GitHub Desktop.
Save NonlinearFruit/25234331099cec554b02551969fd7270 to your computer and use it in GitHub Desktop.
Retrospector Book Plugin
// 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;
@NonlinearFruit
Copy link
Author

Retrospector is a desktop app for rating and reviewing media (movies, books, tv shows, etc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment