Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 6096967cb1
Fetching contributors…

Cannot retrieve contributors at this time

file 131 lines (99 sloc) 3.048 kb
1 2 3 4 5 6 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
module HN2JSON

  # Public: Parse HTML to produce HackerNews entities

  class Parser
    def initialize response
      @doc = Nokogiri::HTML::DocumentFragment.parse response.html
    end


    def determine_type
      title = @doc.css('.title a')

      if title.length < 1
        return :comment
      else
        forms = @doc.css('td form')
        if forms.length === 1
          return :poll
        else
          forms = @doc.css('td')[10].css('form')
          if forms.length === 1
            return :post
          else
            return :discussion
          end
        end
      end

    end

    def get_attrs_comment entity

      parent_url = @doc.css('.comhead a')[2]['href']
      parent_regex = /id\=(.*)/
      parent = parent_regex.match(parent_url)[1]
      
      fulltext_nolinks = @doc.css('.comment')[0].to_s
      fulltext_nolinks = fulltext_nolinks.gsub(/\<a\shref\=['"](.*)['"].*rel\=.*\>.*\<\/a\>/, '\1')
      fulltext = fulltext_nolinks.gsub(/<\/?[^>]*>/, '')

      comhead = @doc.css('.comhead')[0]
      
      date_regex = /.*\s(.*\s.*\sago)/
      ago = date_regex.match(comhead.content)[1]
      date_posted = Chronic.parse(ago).to_s

      posted_by = comhead.css('a')[0].content

      comments = []

      full_comments = @doc.css('td > img[width="0"]').xpath("..").xpath("..").css('.default')

      full_comments.each do |comment|
        comment_id = comment.css('span a')[1]['href'].gsub("item?id=", '')
        comments.push comment_id
      end

      entity.add_attrs do |e|
        e.parent = parent
        e.fulltext = fulltext
        e.date_posted = date_posted
        e.comments = comments
        e.posted_by = posted_by
      end
    end


    def get_attrs_post entity

      subtext = @doc.css('.subtext')[0]

      date_regex = /.*\s(.*\s.*\sago)/
      ago = date_regex.match(subtext.content)[1]
      date_posted = Chronic.parse(ago).to_s

      posted_by = subtext.css('a')[0].content

      votes = subtext.css('span')[0].content.to_i

      comments = []

      full_comments = @doc.css('td > img[width="0"]').xpath("..").xpath("..").css('.default')

      full_comments.each do |comment|
        comment_id = comment.css('span a')[1]['href'].gsub("item?id=", '')
        comments.push comment_id
      end

      head = @doc.css('.title a')[0]

      title = head.content

      url = head['href']

      entity.add_attrs do |e|
        e.url = url
        e.title = title
        e.date_posted = date_posted
        e.comments = comments
        e.votes = votes
        e.posted_by = posted_by
      end
    end

# def get_attrs_poll entity
# entity.add_attrs do |e|
# e.title =
# e.fulltext =
# e.date_posted =
# e.posted_by =
# e.votes =
# e.comments =
# voting_on =
# end
# end
#
# def get_attrs_discussion entity
# entity.add_attrs do |e|
# e.title =
# e.fulltext =
# e.date_posted =
# e.posted_by =
# e.comments =
# e.votes =
# end
# end

  end

end
Something went wrong with that request. Please try again.