This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
2377 - 3 #299
Open
idealasgas
wants to merge
16
commits into
rubizza-camp:master
Choose a base branch
from
idealasgas:3_task
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
2377 - 3 #299
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b7e0e25
Created gitignore and working application
idealasgas 4e8b200
Fixed
idealasgas c7e1a8a
Minor dog fixes
idealasgas 39ee0e8
Fixing in process
idealasgas 3553059
Fixed parser
idealasgas d7a640f
Parser fully fixed
idealasgas 0cd59c9
azure_api fixed
idealasgas 1aa5a40
tCOLON fixed
idealasgas 260a9c3
REST
idealasgas 8828f01
hound doesn't work
idealasgas 0e00f88
Fix
idealasgas 58e18dc
converter created
idealasgas a272124
tCOLON & RCURLY
idealasgas 935b242
hound fix
idealasgas 65134d8
REST done
idealasgas 5f48a1b
typo fixed
idealasgas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
config.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Metrics/AbcSize: Assignment Branch Condition size for analyse is too high. [15.03/15]