module LibgenApi

Constants

VERSION

Public Class Methods

change_mirror(new_mirror) click to toggle source
# File lib/libgen_api.rb, line 44
def self.change_mirror(new_mirror)
    @mirror = new_mirror
end
get_book(id, fields=["Title", "Author", "MD5"]) click to toggle source
# File lib/libgen_api.rb, line 76
def self.get_book(id, fields=["Title", "Author", "MD5"])
    fields = fields.join(',')

    res = HTTParty.get("http://#{@mirror}/json.php?ids=#{id}&fields=#{fields}")

    begin
        JSON.parse(res.body)[0]
    rescue JSON::ParserError, TypeError => e
        raise "An error has occured when parsing the JSON recieved. The error is:\n#{e}"
    end
end
get_books(ids, fields=["Title", "Author", "MD5"]) click to toggle source
# File lib/libgen_api.rb, line 88
def self.get_books(ids, fields=["Title", "Author", "MD5"])
    ids = ids.map(&:to_s).join(',')
    fields = fields.join(',')

    res = HTTParty.get("http://#{@mirror}/json.php?ids=#{ids}&fields=#{fields}")

    begin
        JSON.parse(res.body)
    rescue JSON::ParserError, TypeError => e
        raise "An error has occured when parsing the JSON recieved. The error is:\n#{e}"
    end
end
get_ids(page) click to toggle source

Results are in the same order as search results.

# File lib/libgen_api.rb, line 14
def self.get_ids(page)
    books = []

    table = page.search('table')[2]
    table.search('tbody').each do |tb|
        tb.search('tr').each do |tr|

            # Here we check to see if the name of th current text element is "ID:"
            # and then move onto to the next element in the loop which contains the id.
            found_id = false
            tr.search('td').each do |td|
                if found_id
                    books.push(td.text)
                    break
                end

                if td.text == "ID:"
                    found_id = true
                end
            end
        end
    end

    books
end
get_mirror() click to toggle source
# File lib/libgen_api.rb, line 40
def self.get_mirror
    @mirror
end