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

2420_3 #286

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions 2420/3/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source 'http://rubygems.org'

gem 'capybara'
gem 'ohm'
gem 'redis'
gem 'selenium-webdriver'
gem 'shotgun'
gem 'sinatra'
66 changes: 66 additions & 0 deletions 2420/3/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
GEM
remote: http://rubygems.org/
specs:
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
capybara (3.4.1)
addressable
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
rack (>= 1.6.0)
rack-test (>= 0.6.3)
xpath (~> 3.1)
childprocess (0.9.0)
ffi (~> 1.0, >= 1.0.11)
ffi (1.9.25)
hiredis (0.6.1)
mini_mime (1.0.0)
mini_portile2 (2.3.0)
mustermann (1.0.2)
nest (3.1.1)
redic
nokogiri (1.8.4)
mini_portile2 (~> 2.3.0)
ohm (3.1.1)
nest (~> 3)
redic (~> 1.5.0)
stal
public_suffix (3.0.2)
rack (2.0.5)
rack-protection (2.0.3)
rack
rack-test (1.1.0)
rack (>= 1.0, < 3)
redic (1.5.0)
hiredis
redis (4.0.1)
rubyzip (1.2.1)
selenium-webdriver (3.13.1)
childprocess (~> 0.5)
rubyzip (~> 1.2)
shotgun (0.9.2)
rack (>= 1.0)
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)
tilt (2.0.8)
xpath (3.1.0)
nokogiri (~> 1.8)

PLATFORMS
ruby

DEPENDENCIES
capybara
ohm
redis
selenium-webdriver
shotgun
sinatra

BUNDLED WITH
1.16.2
10 changes: 10 additions & 0 deletions 2420/3/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'sinatra/base'
require 'ohm'

Dir.glob('./{helpers,controllers}/*.rb').each do |file|
require file
end

map('/') { run ApplicationController }
map('/table') { run TableController }
map('/analysis') { run AnalysisController }
13 changes: 13 additions & 0 deletions 2420/3/controllers/analysis_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# class AnalysisController
class AnalysisController < ApplicationController
get '/:id' do
@id = params[:id]
@first_id = Article[@id].comments.first.id
@last_id = Article[@id].comments.last.id
erb :analysis
end

post '/:id' do
erb :analysis
end
end
14 changes: 14 additions & 0 deletions 2420/3/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# class ApplicationController
class ApplicationController < Sinatra::Base
helpers ApplicationHelper

configure do
set :views, './views/'
set :root, File.expand_path('..', __dir__)
end

get '/' do
title 'My App'
erb :table
end
end
22 changes: 22 additions & 0 deletions 2420/3/controllers/table_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'ohm'
require 'redis'
# class TableController
class TableController < ApplicationController
get '/' do
erb :table
end

post '/' do
scores = []
article = Article.create(url: params[:new_url]) unless params[:new_url].empty?
comments = Pars.new(params[:new_url]).comments
comments.each do |comment|
score = ManipulationCommentScore.new(Analytics.new(comment).run).run
scores << score
comment_attribute = Comment.create(body: comment, comment_rating: score)
article.comments.push(comment_attribute)
end
article.update article_rating: ManipulationArticleScore.new(scores).run
erb :table
end
end
7 changes: 7 additions & 0 deletions 2420/3/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# module ApplicationHelper
module ApplicationHelper
def title(value = nil)
@title = value if value
@title || 'MyApp'
end
end
26 changes: 26 additions & 0 deletions 2420/3/helpers/manipulation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# class ManipulationCommentScore
class ManipulationCommentScore
attr_reader :score

def initialize(score)
@score = score
end

def run
result = score * 100 / 0.5 - 100 if score <= 0.5
result = (score - 0.5) * 100 / 0.5 if score >= 0.5
result.round
end
end
# class ManipulationArticleScore
class ManipulationArticleScore
attr_reader :scores

def initialize(scores)
@scores = scores
end

def run
scores.sum / scores.size.to_i
end
end
12 changes: 12 additions & 0 deletions 2420/3/helpers/models_article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'ohm'
# class Article
class Article < Ohm::Model
list :comments, :Comment
attribute :article_rating
attribute :url
end
# class Comment
class Comment < Ohm::Model
attribute :body
attribute :comment_rating
end
25 changes: 25 additions & 0 deletions 2420/3/helpers/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'capybara'

Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
# :reek:FeatureEnvy
# rubocop:disable Metrics/LineLength
class Pars
attr_reader :link

def initialize(link)
@link = link
end

def comments
session = Capybara::Session.new(:selenium)
session.visit(link)
session.find('a.button-style.button-style_subsidiary.button-style_big.news-form__button.news-form__button_width_full.news-form__button_font-weight_semibold').click
session.find_by_id('comments')
.all('.news-comment__speech.news-comment__speech_base' || '.news-comment__unit')
.first(20)
.map(&:text)
end
end
# rubocop:enable Metrics/LineLength
36 changes: 36 additions & 0 deletions 2420/3/helpers/text_analysis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'net/https'
require 'uri'
require 'json'
# :reek:FeatureEnvy
# :reek:TooManyStatements
# class Analytic
class Analytics
attr_reader :text

def initialize(text)
@text = text
end

def run
access_key = SECRET_KEY.freeze

uri = 'https://westcentralus.api.cognitive.microsoft.com'
path = '/text/analytics/v2.0/sentiment'

uri = URI(uri + path)

documents = { 'documents' => [{ 'id': '1', 'language': 'ru', 'text': text }] }
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request['Ocp-Apim-Subscription-Key'] = access_key
request.body = documents.to_json

response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
http.request(request)
end

information = JSON.pretty_generate(JSON(response.body))
information = information.gsub(/^\s+|\n|\r|\s+$|\D/, ' ').split
(information[0] + '.' + information[1]).to_f
end
end
17 changes: 17 additions & 0 deletions 2420/3/views/analysis.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<CENTER>
<h1>All comments</h1>
<table BORDER CELLPADDING=10 CELLSPACING=0>
<% for id in @first_id..@last_id %>
<tr>
<td><%= Comment[id].body %></td>
<td><%= Comment[id].comment_rating %></td>
</tr>
<%end%>

<form>
<button>
<a href="/table"> Comeback
</a>
</button>
</form>
</CENTER>
8 changes: 8 additions & 0 deletions 2420/3/views/layout.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title><%= title%></title>
</head>
<body style = 'background: url(https://images.wallpaperscraft.ru/image/setka_seryy_fon_svetlyy_83492_2048x1152.jpg); background-size: 100% 100%;'>
<%= yield %>
</body>
</html>
17 changes: 17 additions & 0 deletions 2420/3/views/table.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<CENTER>
<h1>Добавьте новый URL: </h1>
<form action="/table" method="POST">
<input type="text" name="new_url" />
<input type="submit" />
</form>
<table BORDER CELLPADDING=10 CELLSPACING=0>
<% Article.all.each do |article| %>
<tr>
<td><a href='#'><%= article.url %></a></td>
<td><%= article.article_rating %></td>
<td>
<a href=<%= "/analysis/#{article.id}" %>>Линка</a>
</td>
</tr>
<%end%>
</CENTER>