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

2377 - 3 #299

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions 2377/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.yml
12 changes: 12 additions & 0 deletions 2377/3/sinatra/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'mechanize'
gem 'ohm'
gem 'sinatra'

group :development do
gem 'pry'
gem 'shotgun'
end
72 changes: 72 additions & 0 deletions 2377/3/sinatra/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.2)
connection_pool (2.2.2)
domain_name (0.5.20180417)
unf (>= 0.0.5, < 1.0.0)
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)
method_source (0.9.0)
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
pry (0.11.3)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
rack (2.0.5)
rack-protection (2.0.3)
rack
redic (1.5.0)
hiredis
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)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.5)
webrobots (0.1.2)

PLATFORMS
ruby

DEPENDENCIES
mechanize
ohm
pry
shotgun
sinatra

BUNDLED WITH
1.16.2
45 changes: 45 additions & 0 deletions 2377/3/sinatra/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'sinatra/base'
require_relative 'models/link'
require_relative 'models/comment'
require_relative 'parser'
require_relative 'azure_api'
require 'ohm'
require 'yaml'
# Main class for application
class MyApp < Sinatra::Base
post '/links/new' do
link = Link.create link: params[:link]
please = Parser.new
please.get_comments(link)
pray = TextAnalytics.new
link.comments.each do |comment|
pray.analyse(comment)
end
total = link.comments.inject(0) { |sum, comment| sum + comment.score.to_i } / link.comments.size
link.update score: total
redirect '/links'
end

get '/links' do
erb :active_page
end

get '/' do
erb :main
end

get '/clear' do
Ohm.redis.call('FLUSHALL')
redirect '/'
end

get '/links/:id' do
@comments = Link[params[:id]].comments

erb :links
end

# $0 is the executed file
# __FILE__ is the current file
run! if $PROGRAM_NAME == __FILE__
end
41 changes: 41 additions & 0 deletions 2377/3/sinatra/azure_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'net/https'
require 'uri'
require 'json'
require_relative 'converter'
# Analysis via Azure API
class TextAnalytics
def initialize
@uri = URI('https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment')
end

def analyse(comment)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/AbcSize: Assignment Branch Condition size for analyse is too high. [15.03/15]

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

private

def access_key
@access_key = YAML.load_file(File.join(Dir.pwd, 'config.yml'))['access_key']
end

def documents(comment)
documents = {}
documents['documents'] = [{ 'id' => '1', 'language' => 'ru', 'text' => comment.comment.to_s }]
documents
end

def update_comment(comment, response)
comment.update score: Converter.new(((JSON response.body)['documents'][0]['score'])).final_value
end

def request(comment)
request = Net::HTTP::Post.new(@uri)
request['Content-Type'] = 'application/json'
request['Ocp-Apim-Subscription-Key'] = access_key
request.body = documents(comment).to_json
request
end
end
14 changes: 14 additions & 0 deletions 2377/3/sinatra/converter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Converts things
class Converter
attr_reader :value
attr_reader :final_value
def initialize(value)
@value = value
@final_value = convert
end

def convert
proportion = 1 / value
200 / proportion - 100
end
end
6 changes: 6 additions & 0 deletions 2377/3/sinatra/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'ohm'
# Describes comment
class Comment < Ohm::Model
attribute :comment
attribute :score
end
7 changes: 7 additions & 0 deletions 2377/3/sinatra/models/link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'ohm'
# Describes link
class Link < Ohm::Model
attribute :link
list :comments, :Comment
attribute :score
end
29 changes: 29 additions & 0 deletions 2377/3/sinatra/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'open-uri'
require 'nokogiri'
require 'net/http'
require 'json'
# Parsing of comments
class Parser
ONLINER = 'https://comments.api.onliner.by/news/tech.post/'.freeze
def get_comments(link)
json_comments = JSON.parse(Net::HTTP.get_response(URI.parse(ONLINER + get_id(link) + '/comments?limit=50')).body)
comments = json_comments['comments'].each.map { |hash| hash['text'] }
push(comments, link)
end

private

def push(comments, link)
comments.each do |comment|
link.comments.push(Comment.create(comment: comment))
end
end

def response(link)
Net::HTTP.get_response(URI.parse(link.link)).body
end

def get_id(link)
Nokogiri::HTML(response(link)).xpath('//span[@news_id]').to_s.match(/\d+/).to_s
end
end
38 changes: 38 additions & 0 deletions 2377/3/sinatra/views/active_page.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>analysis</title>
<style>
td {
text-align: center; /* Выравниваем текст по центру ячейки */
}
</style>
</head>
<body>
<form action="links/new" method="POST">
<p align="center">
<input name="link"> <input type="submit" value="тык">
</p>
</form>
<form action="/clear" method="GET">
<p align="center"><input type="submit" value="почистить"></p>
</form>
<table align="center" cellspacing=5>
<tr> <td><b>ваша ссылочка</b></td> <td><b>рейтинг</b></td></tr>
<%if Link.all%>
<% Link.all.each do |link| %>
<tr>
<td> <%=link.link%> </td>
<td> <%= link.score%> <td>
<td>
<a title="просто тыкни" href=<%= "/links/#{link.id}" %> target="_blank"> <input type="submit" value="почитать комменты"> </a>
</td>
</tr>
<% end%>
<% end%>
</table>
</body>
</html>


19 changes: 19 additions & 0 deletions 2377/3/sinatra/views/links.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>analysis</title>
</head>
<body>
<table>
<%if @comments%>
<% @comments.each do |comment| %>
<tr>
<td> <%=comment.comment%> </td>
<td> <%=comment.score%> <td>
</tr>
<% end%>
<% end%>
</table>
</body>
</html>
15 changes: 15 additions & 0 deletions 2377/3/sinatra/views/main.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>analysis</title>
<style>
P { text-align: center}
</style>
</head>
<body>
<form action="/links/new" method="POST">
<p><input name="link"> <input type="submit" value="тык"></p>
</form>
</body>
</html>