<commit_before># Ruby script to add books to a (new) bookshelf in goodreads
# Pass in filename to script or barf

# Library for HTTP and URI stuff
require 'net/http'
require 'json'
require 'OAuth'

# goodreads key
key = ARGV[1]
secret = ARGV[2]

# Register app with goodreads
consumer = OAuth::Consumer.new(key,secret, :site => 'http://www.goodreads.com')
request_token = consumer.get_request_token
# open this URL in the browser and authorize
request_token.authorize_url


# Param check
if ARGV[0].nil? or ARGV[1].nil? or ARGV[2].nil?
    puts "Missing Parameter(s)"
    puts "First param is text file of books."
    puts "Second is your developer key."
    puts "Third is your secret."
    exit
elsif not File.exist?(ARGV[0])
    puts "Missing file"
    exit
else
    file = ARGV[0]
end

# parse filename to use as bookshelf name
shelf_name = File.basename(file, ".txt")

# Steps
# 1st - Read in text file of books
# NOTE: Will need to get author, otherwise results will be too broad
book_titles = Array.new

f = File.open(file, "r")
f.each_line do |line|
    book_titles.push line
end
f.close

# 2nd - Use book titles to retrieve list of ISBN numbers
# Google API is
# https://www.googleapis.com/books/v1/volumes?q=search+terms
# returns JSON (use ISBN_13 or 10?)
# {"items": [{"industry identifiers": [
# {"type": "ISBN_13","identifier": "9780141919959"}]}]}

# Persistent connections are not required
isbns = Array.new()

book_titles.each do |i|
    encoded_title = URI::encode i
    uri = URI("https://www.googleapis.com/books/v1/volumes?q=#{encoded_title}")
    # need author, too
    # ?q=#{i}+inauthor:#{author}
    res = Net::HTTP.get(uri) # => String
    parsed = JSON.parse(res) # => Hash
    # Brittle code ahead
    isbn = parsed["items"].first["volumeInfo"]["industryIdentifiers"][1]["identifier"]
    if isbn.nil?
        # get the isbn10 number
        isbn = parsed["items"].first["volumeInfo"]["industryIdentifiers"][0]["identifier"]
    end
    isbns.push(isbn)
    sleep(1)
end


# 3rd - Use ISBN numbers to retrieve goodreads ID numbers
goodreads_ids = Array.new()

isbns.each do |isbn|
    uri_gr = URI("https://www.goodreads.com/book/isbn_to_id?isbn=#{isbn}&key=#{key}")
    res_gr = Net::HTTP.get(uri_gr)
    goodreads_ids.push(res_gr)
end


# 4th - Create bookshelf on goodreads


# 5th - Add books, via their goodreads IDs, to bookshelf
# Takes a comma-separated list of book ids
# and adds them to the shelf name var (name of imported text list of books)
# requires OAuth
idlist = goodreads_ids.join(",")
uri_id = URI('https://goodreads.com/shelf/add_books_to_shelves.xml')
res_id = Net::HTTP.post_form(uri_id, 'bookids' => goodreads_ids, 'shelves' => shelf_name)

# 6th - Verify<commit_msg>added access token; changed post to get<commit_after># Ruby script to add books to a (new) bookshelf in goodreads
# Pass in filename to script or barf

# Library for HTTP and URI stuff
require 'net/http'
require 'json'
require 'OAuth'

# goodreads key
key = ARGV[1]
secret = ARGV[2]

# Register app with goodreads
consumer = OAuth::Consumer.new(key,secret, :site => 'http://www.goodreads.com')
request_token = consumer.get_request_token
# open this URL in the browser and authorize
request_token.authorize_url

#then
access_token = request_token.get_access_token

# Param check
if ARGV[0].nil? or ARGV[1].nil? or ARGV[2].nil?
    puts "Missing Parameter(s)"
    puts "First param is text file of books."
    puts "Second is your developer key."
    puts "Third is your secret."
    exit
elsif not File.exist?(ARGV[0])
    puts "Missing file"
    exit
else
    file = ARGV[0]
end

# parse filename to use as bookshelf name
shelf_name = File.basename(file, ".txt")

# Steps
# 1st - Read in text file of books
# NOTE: Will need to get author, otherwise results will be too broad
book_titles = Array.new

f = File.open(file, "r")
f.each_line do |line|
    book_titles.push line
end
f.close

# 2nd - Use book titles to retrieve list of ISBN numbers
# Google API is
# https://www.googleapis.com/books/v1/volumes?q=search+terms
# returns JSON (use ISBN_13 or 10?)
# {"items": [{"industry identifiers": [
# {"type": "ISBN_13","identifier": "9780141919959"}]}]}

# Persistent connections are not required
isbns = Array.new()

book_titles.each do |i|
    encoded_title = URI::encode i
    uri = URI("https://www.googleapis.com/books/v1/volumes?q=#{encoded_title}")
    # need author, too
    # ?q=#{i}+inauthor:#{author}
    res = Net::HTTP.get(uri) # => String
    parsed = JSON.parse(res) # => Hash
    # Brittle code ahead
    isbn = parsed["items"].first["volumeInfo"]["industryIdentifiers"][1]["identifier"]
    if isbn.nil?
        # get the isbn10 number
        isbn = parsed["items"].first["volumeInfo"]["industryIdentifiers"][0]["identifier"]
    end
    isbns.push(isbn)
    sleep(1)
end


# 3rd - Use ISBN numbers to retrieve goodreads ID numbers
goodreads_ids = Array.new()

isbns.each do |isbn|
    uri_gr = URI("https://www.goodreads.com/book/isbn_to_id?isbn=#{isbn}&key=#{key}")
    res_gr = Net::HTTP.get(uri_gr)
    goodreads_ids.push(res_gr)
end


# 4th - Create bookshelf on goodreads


# 5th - Add books, via their goodreads IDs, to bookshelf
# Takes a comma-separated list of book ids
# and adds them to the shelf name var (name of imported text list of books)
# requires OAuth

# works in browser (get instead of post) but in irb gives "not authorized."
idlist = goodreads_ids.join(",")
uri_id = URI("https://www.goodreads.com/shelf/add_books_to_shelves.xml?bookids=#{idlist}&shelves=#{shelf_name}")
res_id = Net::HTTP.get(uri_id)

# 6th - Verify
{'fraction_non_alphanumeric': 0.1292577984940839, 'fraction_numerical': 0.013983506633201864, 'mean_word_length': 4.058023572076156, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 8, 'lorem ipsum': 0, 'www.': 9, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '719472', 'n_tokens_mistral': 2009, 'n_tokens_neox': 1932, 'n_words': 573}