<commit_before>require 'open-uri'

class Book < ActiveRecord::Base
  has_many :book_authors
  has_many :authors, :through => :book_authors
  has_many :book_genres
  has_many :genres, :through => :book_genres

  LIKED_BOOKS = []

  def author_names=(author_names)
    author_names.each do |author_name|
      self.book_authors.build(:author => Author.find_or_create_by(name: author_name))
    end
  end

  def genre_names=(genre_names)
    genre_names.each do |genre_name|
      self.book_genres.build(:genre => Genre.find_or_create_by(name: genre_name))
    end
  end

  def recommend(liked_book)
    options = []
    Book.all.each do |book|
      if book.genres.count >= 3 && !book.genres.empty?
        options << book if book.genres.all? {|genre| liked_book.genres.include?(genre)}
      end
    end
    options.shift
    options
  end

  def self.liked_books
    LIKED_BOOKS
  end

  def self.grab_covers
    Book.all.each do |book|
      response = request(book.title).run
      stuff = JSON.parse(response.body)
      option = stuff["items"].first
      book.update(cover_link: option["volumeInfo"]["imageLinks"]["thumbnail"])
      puts "Grabbed cover for #{book.title}."
    end
  end

  def self.save_books
    urls = [
      # "http://www.goodreads.com/list/show/6",
      # "http://www.goodreads.com/list/show/7",
      "http://www.goodreads.com/list/show/264.Books_That_Everyone_Should_Read_At_Least_Once",
      # "http://www.goodreads.com/list/show/36.Best_Poetry_Books",
      # "http://www.goodreads.com/list/show/135.Best_Horror_Novels",
      "http://www.goodreads.com/list/show/15.Best_Historical_Fiction"
      # "http://www.goodreads.com/list/show/281.Best_Memoir_Biography_Autobiography",
      # "http://www.goodreads.com/list/show/735.Must_Read_Non_Fiction",
      # "http://www.goodreads.com/list/show/7205.Best_of_Little_Known_Authors",
      # "http://www.goodreads.com/list/show/348.Thrillers",
      # "http://www.goodreads.com/list/show/11.Best_Crime_Mystery_Books"
      # "http://www.goodreads.com/list/show/8166.Books_You_Wish_More_People_Knew_About",
      # "http://www.goodreads.com/list/show/183.Tales_of_New_York_City",
      # "http://www.goodreads.com/list/show/16.Best_Books_of_the_19th_Century",
      # "http://www.goodreads.com/book/most_read",
      # "http://www.goodreads.com/list/show/3.Best_Science_Fiction_Fantasy_Books",
      # "http://www.goodreads.com/list/show/30",
      # "http://www.goodreads.com/list/show/2602.Best_Female_Lead_Characters",
      # "http://www.goodreads.com/list/show/50.The_Best_Epic_Fantasy",
      # "http://www.goodreads.com/list/show/824.Best_Non_fiction_War_Books",
      # "http://www.goodreads.com/list/show/397.Best_Paranormal_Romance_Series",
      # "http://www.goodreads.com/list/show/338.Immigrant_Experience_Literature",
      # "http://www.goodreads.com/list/show/10925.Funny_Women_Memoirs",
      # "http://www.goodreads.com/list/show/29013.Best_Biographies",
      # "http://www.goodreads.com/list/show/495.Best_of_William_Shakespeare",
      # "http://www.goodreads.com/list/show/453.Best_Philosophical_Literature"
    ]
    urls.each do |url|
      puts "On #{url}"
      results = Book.find_titles(url)
      results.each do |result|
        Book.find_book(result[0], result[1])
      end
    end
  end

  def self.save_books_from(url)
    results = Book.find_titles(url)
    results.each do |result|
      Book.find_book(result[0], result[1])
    end
  end

  def self.find_titles(url)
    page = Nokogiri::HTML(open(url))
    results = page.css('div.leftContainer table.tableList tr td a span')
    count = results.count/2
    (0..count-1).map do |i|
      [results[i*2].text.split(" (").first, results[i*2 +1].text]
    end
  end

  def self.request(title)
    request = Typhoeus::Request.new(
    "https://www.googleapis.com/books/v1/volumes",
    params: { 
      q: CGI::escape("#{title}"), 
      filter: "partial", 
      key: ENV["GBOOKS_BACKUP_KEY"]<commit_msg>switched api keys<commit_after>require 'open-uri'

class Book < ActiveRecord::Base
  has_many :book_authors
  has_many :authors, :through => :book_authors
  has_many :book_genres
  has_many :genres, :through => :book_genres

  LIKED_BOOKS = []

  def author_names=(author_names)
    author_names.each do |author_name|
      self.book_authors.build(:author => Author.find_or_create_by(name: author_name))
    end
  end

  def genre_names=(genre_names)
    genre_names.each do |genre_name|
      self.book_genres.build(:genre => Genre.find_or_create_by(name: genre_name))
    end
  end

  def recommend(liked_book)
    options = []
    Book.all.each do |book|
      if book.genres.count >= 3 && !book.genres.empty?
        options << book if book.genres.all? {|genre| liked_book.genres.include?(genre)}
      end
    end
    options.shift
    options
  end

  def self.liked_books
    LIKED_BOOKS
  end

  def self.grab_covers
    Book.all.each do |book|
      response = request(book.title).run
      stuff = JSON.parse(response.body)
      option = stuff["items"].first
      book.update(cover_link: option["volumeInfo"]["imageLinks"]["thumbnail"])
      puts "Grabbed cover for #{book.title}."
    end
  end

  def self.save_books
    urls = [
      # "http://www.goodreads.com/list/show/6",
      # "http://www.goodreads.com/list/show/7",
      "http://www.goodreads.com/list/show/264.Books_That_Everyone_Should_Read_At_Least_Once",
      # "http://www.goodreads.com/list/show/36.Best_Poetry_Books",
      # "http://www.goodreads.com/list/show/135.Best_Horror_Novels",
      "http://www.goodreads.com/list/show/15.Best_Historical_Fiction"
      # "http://www.goodreads.com/list/show/281.Best_Memoir_Biography_Autobiography",
      # "http://www.goodreads.com/list/show/735.Must_Read_Non_Fiction",
      # "http://www.goodreads.com/list/show/7205.Best_of_Little_Known_Authors",
      # "http://www.goodreads.com/list/show/348.Thrillers",
      # "http://www.goodreads.com/list/show/11.Best_Crime_Mystery_Books"
      # "http://www.goodreads.com/list/show/8166.Books_You_Wish_More_People_Knew_About",
      # "http://www.goodreads.com/list/show/183.Tales_of_New_York_City",
      # "http://www.goodreads.com/list/show/16.Best_Books_of_the_19th_Century",
      # "http://www.goodreads.com/book/most_read",
      # "http://www.goodreads.com/list/show/3.Best_Science_Fiction_Fantasy_Books",
      # "http://www.goodreads.com/list/show/30",
      # "http://www.goodreads.com/list/show/2602.Best_Female_Lead_Characters",
      # "http://www.goodreads.com/list/show/50.The_Best_Epic_Fantasy",
      # "http://www.goodreads.com/list/show/824.Best_Non_fiction_War_Books",
      # "http://www.goodreads.com/list/show/397.Best_Paranormal_Romance_Series",
      # "http://www.goodreads.com/list/show/338.Immigrant_Experience_Literature",
      # "http://www.goodreads.com/list/show/10925.Funny_Women_Memoirs",
      # "http://www.goodreads.com/list/show/29013.Best_Biographies",
      # "http://www.goodreads.com/list/show/495.Best_of_William_Shakespeare",
      # "http://www.goodreads.com/list/show/453.Best_Philosophical_Literature"
    ]
    urls.each do |url|
      puts "On #{url}"
      results = Book.find_titles(url)
      results.each do |result|
        Book.find_book(result[0], result[1])
      end
    end
  end

  def self.save_books_from(url)
    results = Book.find_titles(url)
    results.each do |result|
      Book.find_book(result[0], result[1])
    end
  end

  def self.find_titles(url)
    page = Nokogiri::HTML(open(url))
    results = page.css('div.leftContainer table.tableList tr td a span')
    count = results.count/2
    (0..count-1).map do |i|
      [results[i*2].text.split(" (").first, results[i*2 +1].text]
    end
  end

  def self.request(title)
    request = Typhoeus::Request.new(
    "https://www.googleapis.com/books/v1/volumes",
    params: { 
      q: CGI::escape("#{title}"), 
      filter: "partial", 
      key: ENV["GOOGLE_BOOKS_API_KEY"]
{'fraction_non_alphanumeric': 0.15136632665911096, 'fraction_numerical': 0.021156025689459765, 'mean_word_length': 3.244788882950294, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 54, 'xml': 0}, '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': '1023445', 'n_tokens_mistral': 3148, 'n_tokens_neox': 2937, 'n_words': 347}