Module: LibgenApi

Defined in:
lib/libgen_api.rb,
lib/libgen_api/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.change_mirror(new_mirror) ⇒ Object



44
45
46
# File 'lib/libgen_api.rb', line 44

def self.change_mirror(new_mirror)
    @mirror = new_mirror
end

.get_book(id, fields = ["Title", "Author", "MD5"]) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# 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"]) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# 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) ⇒ Object

Results are in the same order as search results.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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_mirrorObject



40
41
42
# File 'lib/libgen_api.rb', line 40

def self.get_mirror
    @mirror
end

.search(query, res, column) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/libgen_api.rb', line 48

def self.search(query, res, column)
    raise "query must be at least four characters long!" if query.length() < 4

    if res != 25 && res != 50 && res != 100
        raise "res must equal either 25, 50 or 100"
    end
    
    id_collection = []
    page = 1
    loop do
        url = "http://#{@mirror}/search.php?req=#{query}&lg_topic=libgen&open=0&view=detailed&res=#{res}&column=#{column}&page=#{page}"
        response = HTTParty.get(url)

        doc = Nokogiri::HTML(response.body)
        page_ids = get_ids(doc)

        id_collection = id_collection + page_ids

        if page_ids.length() != res
            break
        end

        page += 1
    end

    id_collection
end