Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Commit

Permalink
Some fix in ArticleParser
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhenyaZhak committed Jul 30, 2018
1 parent 3257814 commit 4e2f376
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
35 changes: 2 additions & 33 deletions 2232/3/helpers/article_parser.rb
@@ -1,8 +1,6 @@
require_relative 'sentiment_parser'
require_relative 'comment_storage'
require_relative 'comments_parser'

class ArticleParser
LIMIT_COMMENTS = 50
attr_reader :link

def initialize(link)
Expand All @@ -18,41 +16,12 @@ def rating
end

def comments
@comments ||= initialize_comments
@comments ||= CommentsParser.new(page, link).call
end

private

def sentiments
SentimentParser.new(comment_parser).call
end

def page
@page ||= Mechanize.new.get(link)
end

def api_link
number = page.css('.news_view_count').attr('news_id').value
category = link.split('https://').last.split('.').first
"https://comments.api.onliner.by/news/#{category}.post/#{number}/comments?limit=9999"
end

def comment_parser
@comment_parser ||= response.map { |comment| [comment['author']['name'], comment['text'].tr("\n", ' ')] }
end

def rate(comment)
comment['marks']['likes'] + comment['marks']['dislikes']
end

def response
JSON.parse(Faraday.get(api_link).body)['comments'].sort_by { |comment| rate(comment) }.reverse.first(LIMIT_COMMENTS)
end

def initialize_comments
sentiments.map do |sentiment|
author_text = comment_parser[sentiment['id'].to_i]
CommentStorage.new(author_text[0], author_text[1], (sentiment['score'] * 200 - 100).to_i)
end
end
end
42 changes: 42 additions & 0 deletions 2232/3/helpers/comments_parser.rb
@@ -0,0 +1,42 @@
require_relative 'sentiment_parser'
require_relative 'comment_storage'

class CommentsParser
LIMIT_COMMENTS = 50

def initialize(page, link)
@page = page
@link = link
end

def call
sentiments.map do |sentiment|
author_text = comments[sentiment['id'].to_i]
CommentStorage.new(author_text[0], author_text[1], (sentiment['score'] * 200 - 100).to_i)
end
end

private

def sentiments
SentimentParser.new(comments).call
end

def api_link
number = @page.css('.news_view_count').attr('news_id').value
category = @link.split('https://').last.split('.').first
"https://comments.api.onliner.by/news/#{category}.post/#{number}/comments?limit=9999"
end

def comments
@comments ||= response.map { |comment| [comment['author']['name'], comment['text'].tr("\n", ' ')] }
end

def rate(comment)
comment['marks']['likes'] + comment['marks']['dislikes']
end

def response
JSON.parse(Faraday.get(api_link).body)['comments'].sort_by { |comment| rate(comment) }.reverse.first(LIMIT_COMMENTS)
end
end

0 comments on commit 4e2f376

Please sign in to comment.