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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28885e3
commit 27edc75
Showing
18 changed files
with
427 additions
and
0 deletions.
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,11 @@ | ||
# A sample Gemfile | ||
source 'https://rubygems.org' | ||
gem 'mechanize' | ||
gem 'pry' | ||
gem 'shotgun' | ||
gem 'thin' | ||
gem 'sinatra' | ||
gem 'ohm' | ||
gem 'redis' | ||
gem 'reek', require: false | ||
gem 'rubocop', require: false |
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,124 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
ast (2.4.0) | ||
axiom-types (0.1.1) | ||
descendants_tracker (~> 0.0.4) | ||
ice_nine (~> 0.11.0) | ||
thread_safe (~> 0.3, >= 0.3.1) | ||
codeclimate-engine-rb (0.4.1) | ||
virtus (~> 1.0) | ||
coderay (1.1.2) | ||
coercible (1.0.0) | ||
descendants_tracker (~> 0.0.1) | ||
connection_pool (2.2.2) | ||
daemons (1.2.6) | ||
descendants_tracker (0.0.4) | ||
thread_safe (~> 0.3, >= 0.3.1) | ||
domain_name (0.5.20180417) | ||
unf (>= 0.0.5, < 1.0.0) | ||
equalizer (0.0.11) | ||
eventmachine (1.2.7) | ||
hiredis (0.6.1) | ||
http-cookie (1.0.3) | ||
domain_name (~> 0.5) | ||
ice_nine (0.11.2) | ||
jaro_winkler (1.5.1) | ||
kwalify (0.7.2) | ||
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 | ||
parallel (1.12.1) | ||
parser (2.5.1.2) | ||
ast (~> 2.4.0) | ||
powerpack (0.1.2) | ||
pry (0.11.3) | ||
coderay (~> 1.1.0) | ||
method_source (~> 0.9.0) | ||
rack (2.0.5) | ||
rack-protection (2.0.3) | ||
rack | ||
rainbow (3.0.0) | ||
redic (1.5.0) | ||
hiredis | ||
redis (4.0.1) | ||
reek (5.0.2) | ||
codeclimate-engine-rb (~> 0.4.0) | ||
kwalify (~> 0.7.0) | ||
parser (>= 2.5.0.0, < 2.6, != 2.5.1.1) | ||
rainbow (>= 2.0, < 4.0) | ||
rubocop (0.58.2) | ||
jaro_winkler (~> 1.5.1) | ||
parallel (~> 1.10) | ||
parser (>= 2.5, != 2.5.1.1) | ||
powerpack (~> 0.1) | ||
rainbow (>= 2.2.2, < 4.0) | ||
ruby-progressbar (~> 1.7) | ||
unicode-display_width (~> 1.0, >= 1.0.1) | ||
ruby-progressbar (1.9.0) | ||
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) | ||
thin (1.7.2) | ||
daemons (~> 1.0, >= 1.0.9) | ||
eventmachine (~> 1.0, >= 1.0.4) | ||
rack (>= 1, < 3) | ||
thread_safe (0.3.6) | ||
tilt (2.0.8) | ||
unf (0.1.4) | ||
unf_ext | ||
unf_ext (0.0.7.5) | ||
unicode-display_width (1.4.0) | ||
virtus (1.0.5) | ||
axiom-types (~> 0.1) | ||
coercible (~> 1.0) | ||
descendants_tracker (~> 0.0, >= 0.0.3) | ||
equalizer (~> 0.0, >= 0.0.9) | ||
webrobots (0.1.2) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
mechanize | ||
ohm | ||
pry | ||
redis | ||
reek | ||
rubocop | ||
shotgun | ||
sinatra | ||
thin | ||
|
||
BUNDLED WITH | ||
1.16.2 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,3 @@ | ||
.col-md-3:hover { | ||
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); | ||
} |
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,10 @@ | ||
require 'bundler' | ||
Bundler.require | ||
|
||
|
||
Dir.glob('./{controllers,helpers,models}/*.rb').each { |file| require file} | ||
|
||
Article.redis = Redic.new('redis://127.0.0.1:6379/0') | ||
Comment.redis = Redic.new('redis://127.0.0.1:6379/0') | ||
|
||
map('/') { run ArticleController } |
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,5 @@ | ||
require 'sinatra' | ||
class ApplicationController < Sinatra::Base | ||
set :views, File.expand_path(File.join(__FILE__, '../../views')) | ||
set :public_folder, File.expand_path(File.join(__FILE__, '../../assets')) | ||
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,30 @@ | ||
require_relative './application_controller.rb' | ||
|
||
class ArticleController < ApplicationController | ||
get '/' do | ||
@title = 'Index' | ||
erb :index | ||
end | ||
|
||
not_found do | ||
status 404 | ||
erb :not_found | ||
end | ||
|
||
get '/registration' do | ||
@title = 'Registration' | ||
erb :registration | ||
end | ||
|
||
get '/article' do | ||
@title = 'Article' | ||
@array = Analyze.new.sort_comments | ||
erb :article | ||
end | ||
|
||
post '/registration' do | ||
@link = params[:link] | ||
ArticleCreator.new(@link).create | ||
redirect '/' | ||
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,9 @@ | ||
class ArticleCreator | ||
def initialize(link) | ||
@link = link | ||
end | ||
|
||
def create | ||
Article.create(link: @link, rating: 0) | ||
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,57 @@ | ||
require_relative 'scraper.rb' | ||
require_relative 'form_comments.rb' | ||
require 'net/https' | ||
require 'uri' | ||
require 'json' | ||
|
||
# Take messages from sorted by onliner's rating array | ||
class PackCreator | ||
def initialize | ||
@array_of_objects = Analyze.new.sort_array_of_objects | ||
@pack = [] | ||
end | ||
|
||
def create_hash_of_comments | ||
@array_of_objects.each { |obj| @pack << obj.message } | ||
end | ||
end | ||
|
||
# Forms azure body for request | ||
class AzureHash | ||
def initialize | ||
@pack = PackCreator.new.create_hash_of_comments | ||
end | ||
|
||
def form_hash | ||
data = {'documents' => []} | ||
@pack.each_with_index do |comment, index| | ||
data['documents'] << {'id' => (index + 1).to_s, 'text' => comment.message} | ||
end | ||
data | ||
end | ||
end | ||
|
||
ACCESS_KEY = '84696ed14a214431b4e25945879458e2'.freeze | ||
PATH = '/text/analytics/v2.0/sentiment'.freeze | ||
API_URI = 'https://westcentralus.api.cognitive.microsoft.com'.freeze | ||
|
||
uri = URI(API_URI + PATH) | ||
|
||
documents = AzureHash.new.form_hash | ||
|
||
puts 'Please wait a moment for the results to appear.' | ||
|
||
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 | ||
|
||
# p JSON.parse(response.body) | ||
|
||
JSON.parse(response.body).each do |variable| | ||
p variable | ||
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,32 @@ | ||
require_relative 'scraper.rb' | ||
|
||
# Comment object contains message and rating | ||
class OnlinerComment | ||
attr_reader :message, :rating | ||
def initialize(message, rating) | ||
@message = message | ||
@rating = rating | ||
end | ||
end | ||
|
||
# Forms an array of objects with message and rating | ||
class Analyze | ||
LIMIT = 50 | ||
def initialize | ||
@comments = JSONParser.new.comments_options | ||
@array = [] | ||
end | ||
|
||
def sort_array_of_objects | ||
array_of_objects.sort_by! { |obj| -obj.rating }.take(LIMIT) | ||
end | ||
|
||
private | ||
|
||
def array_of_objects | ||
@comments.each do |variable| | ||
@array << OnlinerComment.new(variable['text'], Helper.rating(variable['marks'])) | ||
end | ||
@array | ||
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,57 @@ | ||
require 'mechanize' | ||
require 'date' | ||
require 'json' | ||
require 'pry' | ||
|
||
# GetApiLink via link to news | ||
class GetApiLink | ||
ATTRIBUTE_PATTERN = 'span[news_id]'.freeze | ||
attr_reader :id | ||
|
||
def initialize | ||
@agent = Mechanize.new | ||
end | ||
|
||
def detect_news_id | ||
page = @agent.get('https://tech.onliner.by/2018/07/24/dji-4') | ||
page.search(ATTRIBUTE_PATTERN).to_s.match(/\d+/).to_s | ||
end | ||
end | ||
|
||
# Parse date from api in JSON format | ||
class ArticleParser | ||
URL_PATTERN = 'https://comments.api.onliner.by/news/tech.post/386437/comments?limit=500'.freeze | ||
def initialize | ||
@agent = Mechanize.new | ||
@id = GetApiLink.new.detect_news_id | ||
end | ||
|
||
def pull_comments | ||
link = URL_PATTERN.sub(/\d+/, @id) | ||
@page = @agent.get(link) | ||
end | ||
end | ||
|
||
# Parse JSON | ||
class JSONParser | ||
def initialize | ||
@json = ArticleParser.new.pull_comments | ||
end | ||
|
||
def comments_options | ||
read_json_body['comments'].map! { |comment| comment.slice('text', 'marks') } | ||
end | ||
|
||
private | ||
|
||
def read_json_body | ||
JSON.parse(@json.body) | ||
end | ||
end | ||
|
||
# Counting rating based on count of likes and dislikes | ||
class Helper | ||
def self.rating(marks) | ||
marks.flatten.select { |value| value.is_a?(Integer) }.sum | ||
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,7 @@ | ||
require_relative 'comment.rb' | ||
|
||
class Article < Ohm::Model | ||
attribute :link | ||
attribute :rating | ||
set :comments, :Comment | ||
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,4 @@ | ||
class Comment < Ohm::Model | ||
attribute :message | ||
attribute :rating | ||
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 @@ | ||
<table class="table table-hover"> | ||
<% @array.each do |x| %> | ||
<tr> | ||
<th><%= x.message %></th></tr> | ||
<% end %> | ||
</table> |
Oops, something went wrong.