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

2216 - 3 #280

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions 2216/3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
secrets.yml
9 changes: 9 additions & 0 deletions 2216/3/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
source 'https://rubygems.org'

gem 'json'
gem 'mechanize'
gem 'ohm'
gem 'sass'
gem 'sinatra'
gem 'sinatra-asset-pipeline'
gem 'sinatra-static-assets'
98 changes: 98 additions & 0 deletions 2216/3/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
GEM
remote: https://rubygems.org/
specs:
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.0.5)
connection_pool (2.2.2)
domain_name (0.5.20180417)
unf (>= 0.0.5, < 1.0.0)
execjs (2.7.0)
ffi (1.9.25)
hiredis (0.6.1)
http-cookie (1.0.3)
domain_name (~> 0.5)
json (2.1.0)
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
rake (12.3.1)
rb-fsevent (0.10.3)
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
redic (1.5.0)
hiredis
sass (3.5.7)
sass-listen (~> 4.0.0)
sass-listen (4.0.0)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
sinatra (2.0.3)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.3)
tilt (~> 2.0)
sinatra-asset-pipeline (2.1.0)
coffee-script (~> 2.4)
rake (~> 12.3)
sass (~> 3.5)
sinatra (~> 2.0)
sprockets (~> 3.7)
sprockets-helpers (~> 1.2)
sinatra-static-assets (1.0.4)
sinatra (>= 1.1.0)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-helpers (1.2.1)
sprockets (>= 2.2)
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
json
mechanize
ohm
sass
sinatra
sinatra-asset-pipeline
sinatra-static-assets

BUNDLED WITH
1.15.1
4 changes: 4 additions & 0 deletions 2216/3/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'sinatra/asset_pipeline/task'
require_relative './controllers/application_controller'

Sinatra::AssetPipeline::Task.define! ApplicationController
2 changes: 2 additions & 0 deletions 2216/3/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require_relative './controllers/application_controller'
ApplicationController.run!
95 changes: 95 additions & 0 deletions 2216/3/assets/stylesheets/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
$body-color: rgb(219, 232, 175);
$header-and-footer-color: rgb(158, 203, 168);
$text-color: rgb(251, 255, 201);
$error-text-color: rgb(255, 0, 0);
$table-color: rgb(0, 0, 0);

html {
background-color: $body-color;
height: 100%;
min-height: 100%;
position: relative;
width: 100%;
}

body {
background-color: $body-color;
height: 100%;
margin: 0;
padding: 0;
width: 100%;
}

header {
background-color: $header-and-footer-color;
height: 10%;
margin: 0;
padding: 0;
text-align: center;
}

footer {
background-color: $header-and-footer-color;
bottom: 0;
height: 10%;
margin: 0;
padding: 0;
position: fixed;
text-align: center;
width: 100%;
}

main {
background-color: $body-color;
height: 100%;
text-align: center;
width: 100%;
}

.copyright {
color: $text-color;
padding: 2%;
}

h1 {
color: $text-color;
font-size: 200%;
margin: 0;
padding: 0;
padding-top: 1%;
}

.content {
margin: 15%;
}

th {
border: 1px solid $table-color;
}

td {
border: 1px solid $table-color;
}

table {
border: 1px solid $table-color;
border-collapse: collapse;
margin: 0 auto;
}

form {
padding-top: 5%;
}

.look-sub {
width: 30%;
}

.address {
width: 50%;
}

.error-message {
color: $error-text-color;
padding-top: 3%;
}
4 changes: 4 additions & 0 deletions 2216/3/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require_relative './controllers/application_controller'
Article.redis = Redic.new('redis://127.0.0.1:6379/0')
Comment.redis = Redic.new('redis://127.0.0.1:6379/1')
run ApplicationController
41 changes: 41 additions & 0 deletions 2216/3/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'sinatra'
require 'ohm'
require 'mechanize'
require 'sass'
require 'sinatra/asset_pipeline'
require 'sinatra/static_assets'
require 'json'
require 'open-uri'
require 'net/https'
require 'uri'
require_relative './helpers/helpers'

class ApplicationController < Sinatra::Base
set :root, File.dirname(__FILE__)
set :assets_precompile, %w[*.css]
set :assets_paths, ['assets/stylesheets']
set :assets_css_compressor, :sass

register Sinatra::AssetPipeline

get '/' do
error_text = ''
erb :index, locals: { error_text: error_text }
end

post '/' do
error_text = LinkVerificator.new(params['address']).check_link
RecordMaker.new(params['address']).make_record if error_text == ''
erb :index, locals: { error_text: error_text }
end

post '/posts/articles' do
articles = Article.all.to_a
erb :'posts/articles', locals: { articles: articles }
end

get '/posts/comments/:id' do
@comments = Comment.all.to_a.select { |comment| comment.article_id == params[:id] }
erb :'posts/comments'
end
end
5 changes: 5 additions & 0 deletions 2216/3/helpers/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Article < Ohm::Model
attribute :link
attribute :score
collection :comment, :Comment
end
35 changes: 35 additions & 0 deletions 2216/3/helpers/article_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class ArticleParser
attr_reader :link
MAX_NUM_OF_COMMENTS = 50

def initialize(link)
@link = link
end

def parse
parse_json
end

private

def make_post_id
agent = Mechanize.new
page = agent.get(link)
page.search('app[entity-id]').to_s.match(/\d+/).to_s
end

def make_objs
url = "https://comments.api.onliner.by/news/tech.post/#{make_post_id}/comments?limit=#{MAX_NUM_OF_COMMENTS}"
json = IO.open(url).read
JSON.parse(json)
end

def parse_json
comments = []
objs = make_objs
objs['comments'].each do |comment_inf|
comments << comment_inf['text']
end
comments
end
end
40 changes: 40 additions & 0 deletions 2216/3/helpers/azure_text_analytics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class AzureTextAnalytics
attr_reader :comments

def initialize(comments)
@comments = comments
end

def pull_out_score
objs = generate_json_array
objs.map { |obj| obj['documents'][0]['score'] }
end

private

def make_uri
uri = 'https://westcentralus.api.cognitive.microsoft.com'
path = '/text/analytics/v2.0/sentiment'
URI(uri + path)
end

def open_connection(uri)
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https')
end

def recieve_one_comment_json(comment, uri)
response = open_connection(uri) do |http|
http.request RequestMaker.make_request(uri, comments.index(comment), comment)
end
JSON.pretty_generate(JSON(response.body))
end

def recieve_json
uri = make_uri
comments.map { |comment| recieve_one_comment_json(comment, uri) }
end

def generate_json_array
recieve_json.map { |json| JSON.parse(json) }
end
end
5 changes: 5 additions & 0 deletions 2216/3/helpers/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Comment < Ohm::Model
attribute :comment_text
attribute :score
reference :article, :Article
end
8 changes: 8 additions & 0 deletions 2216/3/helpers/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require_relative './helpers/link_verificator'
require_relative './helpers/article_parser'
require_relative './helpers/article'
require_relative './helpers/comment'
require_relative './helpers/rating'
require_relative './helpers/request_maker'
require_relative './helpers/azure_text_analytics'
require_relative './helpers/record_maker'
13 changes: 13 additions & 0 deletions 2216/3/helpers/link_verificator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class LinkVerificator
attr_reader :link

def initialize(link)
@link = link
end

def check_link
error_message = ''
error_message = 'Invalid link. Please, enter another link.' unless link.include? 'https://tech.onliner.by/'
error_message
end
end
Loading