Class: Mrmanga::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/mrmanga/parser.rb

Instance Method Summary collapse

Instance Method Details

#get_chapter_pages(manga, volume, chapter) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mrmanga/parser.rb', line 61

def get_chapter_pages(manga, volume, chapter)
  regex = /rm_h.init\( (.*), 0, false\);/

  link = "http://#{manga.info[:info][:site]}/#{manga.info[:info][:name]}/vol#{volume}/#{chapter}?mature=1"

  body = Faraday.get(link).body.tr("'", '"')

  match = regex.match body

  raise "Unmatchable link: #{link}" unless match

  JSON.parse(match[1]).map do |item|
    {
      link: item[1].to_s + item[0].to_s + item[2].to_s,
      width: item[3],
      height: item[4]
    }
  end
end

#get_manga(link) ⇒ Object



7
8
9
10
11
12
13
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mrmanga/parser.rb', line 7

def get_manga(link)
  parsed = parse_link(link)

  noko = Nokogiri::HTML(Faraday.get(link).body)

   = {
    Title: noko.css('h1.names > .name').first.text,
    Author: noko.css('.elem_author > a.person-link').map(&:text).join(', '),
    Keywords: noko.css('.elem_genre > a.element-link').map(&:text).join(', ')
  }

  some_chapter_link = noko.css('.chapters-link a').attr('href').value

  some_chapter_link.sub!(/\?mature=.?/, '')

  noko_first = Nokogiri::HTML(Faraday.get("http://#{parsed[:site]}#{some_chapter_link}?mature=1").body)

  volch_with_orig = noko_first.css('#chapterSelectorSelect > option').map { |el| [el.attr('value'), el.text] }

  volch = volch_with_orig.map(&:first)

  regex_volch = /^\/[\w]+\/vol(-?\d+)\/(-?\d+).*$/

  volch.map! do |vl|
    match = regex_volch.match vl

    raise "Wrong url: #{vl}" unless match

    match.to_a.drop(1).map(&:to_i)
  end

  volch.reverse!

  orig_names = {}

  volch.each do |item|
    orig_names[item[0]] = {} unless orig_names.key?(item[0])

    orig_names[item[0]][item[1]] = volch_with_orig.pop.last
  end

  manga = Mrmanga::Manga.new

  manga.add_original_chapters(orig_names)
  manga.add_info(
    site: parsed[:site],
    name: parsed[:name]
  )
  manga.()
  manga.add_volumes_and_chapters(volch)

  manga
end