Created
December 1, 2012 23:28
-
-
Save pauladam/4185911 to your computer and use it in GitHub Desktop.
Readernaut library export. Export your readernaut library to csv for import to other services (Goodreads, LibraryThing etc)
This file contains hidden or 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
| args = require('system').args | |
| if args.length != 3 | |
| console.log 'Usage: phantomjs readernaut-export.coffee user pass > export.csv' | |
| phantom.exit() | |
| user = args[1] | |
| pass = args[2] | |
| loginPage = require('webpage').create() | |
| booksPage = require('webpage').create() | |
| bookList = [] | |
| injectjQuery = -> | |
| loginPage.injectJs 'jquery.min.js' | |
| loginPage.evaluate -> | |
| jQuery.noConflict() | |
| login = () -> | |
| evalu8 = (user, pass) -> | |
| jQuery('#id_username').val user | |
| jQuery('#id_password').val pass | |
| jQuery('form#login').submit() | |
| loginPage.evaluate evalu8, user, pass | |
| getBookList = () -> | |
| books = booksPage.evaluate () -> | |
| books = [] | |
| jQuery('[draggable=true]').each (i, el) -> | |
| isbn = el.href.split('/')[4] | |
| title = el.title | |
| detailUrl = el.href | |
| imageUrl = jQuery(el).css('background-image').slice(4).slice(0,-1) | |
| books.push [title, isbn, detailUrl, imageUrl ] | |
| return books | |
| return books | |
| nextBooksUrl = -> | |
| booksPage.evaluate -> | |
| if jQuery('a.ui-button:contains(Next)').length > 0 | |
| jQuery('a.ui-button:contains(Next)')[0].href | |
| else | |
| null | |
| enumerateBookList = (list) -> | |
| # | |
| printCsv = (list) -> | |
| csvHeader = "Title, Author, ISBN, My Rating, Average Rating, Publisher, Binding, Year Published, Original Publication Year, Date Read, Date Added, Bookshelves, My Review" | |
| console.log csvHeader | |
| for item in list | |
| [title, isbn, detailUrl, imageUrl] = item | |
| console.log "#{title}, #{}, #{isbn}, #{}, #{}, #{}, #{}, #{}, #{}, #{}, #{}, #{}, #{}, #{}" | |
| booksPageOnload = (status) -> | |
| if status == 'success' | |
| injectjQuery() | |
| books = getBookList() | |
| nextPage = nextBooksUrl() | |
| bookList = bookList.concat(books) | |
| if nextPage | |
| loadNextPage(nextPage) | |
| else | |
| enumerateBookList(bookList) | |
| printCsv(bookList) | |
| phantom.exit() | |
| booksPage.onLoadFinished = booksPageOnload | |
| loadNextPage = (bookPageUrl) -> | |
| booksPage.open bookPageUrl | |
| loginPage.onLoadFinished = (status) -> | |
| if status == 'success' | |
| injectjQuery() | |
| if !phantom.state | |
| login() | |
| booksPage.open "http://readernaut.com/#{user}/books/" | |
| else | |
| console.log('Connection failed.') | |
| phantom.exit() | |
| loginPage.onConsoleMessage = (msg) -> | |
| console.log(msg) | |
| booksPage.onConsoleMessage = (msg) -> | |
| console.log(msg) | |
| loginPage.open 'http://readernaut.com/registration/login/' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment