-
Notifications
You must be signed in to change notification settings - Fork 43
2269-3 #291
base: master
Are you sure you want to change the base?
2269-3 #291
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'mechanize' | ||
gem 'ohm' | ||
gem 'shotgun' | ||
gem 'sidekiq' | ||
gem 'sinatra' | ||
gem 'thin' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
concurrent-ruby (1.0.5) | ||
connection_pool (2.2.2) | ||
daemons (1.2.6) | ||
domain_name (0.5.20180417) | ||
unf (>= 0.0.5, < 1.0.0) | ||
eventmachine (1.2.7) | ||
hiredis (0.6.1) | ||
http-cookie (1.0.3) | ||
domain_name (~> 0.5) | ||
mechanize (2.7.6) | ||
domain_name (~> 0.5, >= 0.5.1) | ||
http-cookie (~> 1.0) | ||
mime-types (>= 1.17.2) | ||
net-http-digest_auth (~> 1.1, >= 1.1.1) | ||
net-http-persistent (>= 2.5.2) | ||
nokogiri (~> 1.6) | ||
ntlm-http (~> 0.1, >= 0.1.1) | ||
webrobots (>= 0.0.9, < 0.2) | ||
mime-types (3.1) | ||
mime-types-data (~> 3.2015) | ||
mime-types-data (3.2016.0521) | ||
mini_portile2 (2.3.0) | ||
mustermann (1.0.2) | ||
nest (3.1.1) | ||
redic | ||
net-http-digest_auth (1.4.1) | ||
net-http-persistent (3.0.0) | ||
connection_pool (~> 2.2) | ||
nokogiri (1.8.4) | ||
mini_portile2 (~> 2.3.0) | ||
ntlm-http (0.1.1) | ||
ohm (3.1.1) | ||
nest (~> 3) | ||
redic (~> 1.5.0) | ||
stal | ||
rack (2.0.5) | ||
rack-protection (2.0.3) | ||
rack | ||
redic (1.5.0) | ||
hiredis | ||
redis (4.0.1) | ||
shotgun (0.9.2) | ||
rack (>= 1.0) | ||
sidekiq (5.1.3) | ||
concurrent-ruby (~> 1.0) | ||
connection_pool (~> 2.2, >= 2.2.0) | ||
rack-protection (>= 1.5.0) | ||
redis (>= 3.3.5, < 5) | ||
sinatra (2.0.3) | ||
mustermann (~> 1.0) | ||
rack (~> 2.0) | ||
rack-protection (= 2.0.3) | ||
tilt (~> 2.0) | ||
stal (0.3.0) | ||
redic (~> 1.5) | ||
thin (1.7.2) | ||
daemons (~> 1.0, >= 1.0.9) | ||
eventmachine (~> 1.0, >= 1.0.4) | ||
rack (>= 1, < 3) | ||
tilt (2.0.8) | ||
unf (0.1.4) | ||
unf_ext | ||
unf_ext (0.0.7.5) | ||
webrobots (0.1.2) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
mechanize | ||
ohm | ||
shotgun | ||
sidekiq | ||
sinatra | ||
thin | ||
|
||
BUNDLED WITH | ||
1.16.2 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Bundler.require(:default) | ||
|
||
Dir.glob('./{models,controllers,helpers}/*.rb').each { |file| require_relative file } | ||
|
||
Article.redis = Redic.new('redis://127.0.0.1:6379/0') | ||
Comment.redis = Redic.new('redis://127.0.0.1:6379/0') | ||
|
||
map('/articles') { run ArticlesController } | ||
map('/') { run ApplicationController } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Class | ||
class ApplicationController < Sinatra::Base | ||
set :views, File.expand_path(File.join(__FILE__, '../../views')) | ||
|
||
get '/' do | ||
redirect '/articles' | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require_relative './application_controller.rb' | ||
|
||
# Controller | ||
class ArticlesController < ApplicationController | ||
# index | ||
get '/' do | ||
@articles = Article.all | ||
erb :'/articles/index' | ||
end | ||
|
||
# new | ||
get '/new' do | ||
erb :'/articles/create' | ||
end | ||
|
||
# show | ||
get '/:id' do | ||
params[:id] | ||
end | ||
|
||
# create | ||
post '/' do | ||
ArticleHelper.new(params[:url]) | ||
redirect '/' | ||
end | ||
|
||
# put | ||
put '/:id' do | ||
params[:id] | ||
end | ||
|
||
# delete | ||
delete '/:id' do | ||
params[:id] | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Class | ||
class ArticleHelper | ||
def initialize(url) | ||
@agent = Mechanize.new { |agnt| agnt.user_agent_alias = 'Mac Safari' } | ||
@agent.history_added = proc { sleep 1 } | ||
@article = Article.create(url: url, title: retrieve_title(url)) | ||
update_article(url) | ||
end | ||
|
||
attr_reader :agent, :article | ||
|
||
def retrieve_title(url) | ||
page = @agent.get(url) | ||
page.title | ||
end | ||
|
||
def update_article(url) | ||
helper = CommentHelper.new | ||
contents = helper.retrieve_comments(url) | ||
ratings = helper.retrieve_rating(contents) | ||
contents.each_with_index do |content, index| | ||
comment = Comment.create(content: content, rate: ratings[index]) | ||
@article.comments.add(comment) | ||
end | ||
article_rate = (ratings.sum / comments.size).to_i | ||
@article.update(rating: article_rate) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Class | ||
class CommentHelper | ||
def initialize | ||
@agent = Mechanize.new { |agnt| agnt.user_agent_alias = 'Mac Safari' } | ||
@agent.history_added = proc { sleep 1 } | ||
end | ||
|
||
attr_reader :agent, :data | ||
|
||
def retrieve_comments(path) | ||
article_id = @agent.get(path).css('.news_view_count').last.values[1] | ||
url = "https://comments.api.onliner.by/news/tech.post/#{article_id}/comments?limit=50&_=0.9841189675826583" | ||
response = agent.get(url) | ||
comments = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inject |
||
JSON.parse(response.body)['comments'].each { |elem| comments << elem['text'].tr("\n", ' ') } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JSON.parse(response.body, symbolize_names: true)[:comments].inject([]) { |comment, comments| comments << comment[:text].tr("\n", ' ') } |
||
end | ||
|
||
def retrieve_rating(comments) | ||
@data = { documents: [] } | ||
comments.each_with_index do |comment, index| | ||
@data[:documents] << { 'id' => index.to_s, 'language' => 'ru', 'text' => comment[:text] } | ||
end | ||
calculate_rating | ||
end | ||
|
||
def calculate_rating | ||
url = URI('https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. constant |
||
response = send_request(url) | ||
rating = [] | ||
JSON.parse(response.body)['documents'].map do |value| | ||
rating << (value['score'].to_i * 200 - 100) | ||
end | ||
rating | ||
end | ||
|
||
def send_request(url) | ||
Net::HTTP.start(url.host, url.port, use_ssl: url.scheme == 'https') do |http| | ||
http.request(build_request(url)) | ||
end | ||
end | ||
|
||
def build_request(url) | ||
key = YAML.safe_load(File.open('config.yml').read)['api_key'] | ||
request = Net::HTTP::Post.new(url, 'Content-Type' => 'application/json', 'Ocp-Apim-Subscription-Key' => key) | ||
request.body = @data.to_json | ||
request | ||
end | ||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Layout/TrailingBlankLines: 1 trailing blank lines detected. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Class that represents model of Article | ||
class Article < Ohm::Model | ||
attribute :url | ||
attribute :title | ||
set :comments, :Comment | ||
attribute :rating | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Class that represents model of Comment to a Article | ||
class Comment < Ohm::Model | ||
attribute :content | ||
attribute :rate | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<h1>New article</h1> | ||
<form action="/articles" method="post"> | ||
<div class="form-group"> | ||
<label for="article">Article's link</label> | ||
<input type="text" class="form-control" id="url" placeholder="Input link" name="url" value="" autofocus> | ||
</div> | ||
<button type="submit" class="btn btn-success">Add Link</button> | ||
</form> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<h1 align='center'>Article's List</h1> | ||
<br> | ||
<table class="table"> | ||
<tr> | ||
<th>ID</th> | ||
<th>Link</th> | ||
<th>Rating</th> | ||
</tr> | ||
<% @articles.each do |article| %> | ||
<tr> | ||
<td><%= article.id %></td> | ||
<td><p><a href='/articles/<%= article.id %>'><%= article.title %></a></p></td> | ||
<!-- <td><%#= article.rating %></td>--> | ||
</tr> | ||
<% end %> | ||
</table> | ||
<br> | ||
<div align="center"><a href='/articles/new' class='btn btn-primary'>Add Article</a></div> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new line? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Task #3</title> | ||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> | ||
<!-- <link rel="stylesheet" href="/css/style.css">--> | ||
</head> | ||
<body> | ||
|
||
<div class="container"> | ||
|
||
<%= yield %> | ||
|
||
</div> | ||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | ||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> | ||
</body> | ||
</html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
URL = 'https://comments.api.onliner.by/news/tech.post/%{article_id}/comments?limit=50&_=0.9841189675826583'.freeze
url = URL % { article_id: article_id }