Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alesia Yakutsionak - 1 #53

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions Alesia Yakutsionak/1/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gem 'nokogiri'
gem 'redis'
gem 'telegram-bot-ruby'
8 changes: 8 additions & 0 deletions Alesia Yakutsionak/1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
To test the program:

-run "bundle install"
-run "redis-server" to start redis
-run "ruby run_parser.rb"
-put Your_Telegram_Bot_Token in BOT_TOKEN
-run "run_bot.rb"
-write command (/wordplay) to bot in Telegram
40 changes: 40 additions & 0 deletions Alesia Yakutsionak/1/bot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'telegram/bot'
require 'redis'

class Bot
INVALID_COMMAND_TEXT = "I didn't understand you.".freeze
BOT_TOKEN = 'Your_Telegram_Bot_Token'.freeze
als6k marked this conversation as resolved.
Show resolved Hide resolved

def initialize
@redis = Redis.new
@puns_count = @redis.keys('pun_*').count
end

def run
Telegram::Bot::Client.run(BOT_TOKEN) do |bot|
bot.listen do |message|
reply(bot, message)
end
end
end

private

def reply(bot, message)
als6k marked this conversation as resolved.
Show resolved Hide resolved

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 reply is too high. [16.12/15]

case message.text
when /^hey|hi|hello$/i
bot.api.sendMessage(chat_id: message.chat.id, text: "Hey, #{message.from.first_name}")
when '/wordplay' then
random_pun = select_random_pun
wordplay_text = "Random wordplay [#{random_pun[:number]}]:\n#{random_pun[:text]}"
bot.api.sendMessage(chat_id: message.chat.id, text: wordplay_text)
else
bot.api.sendMessage(chat_id: message.chat.id, text: INVALID_COMMAND_TEXT)
end
end

def select_random_pun
i = rand(0..@puns_count - 1)
{ number: i, text: @redis.get("pun_#{i}") }
end
end
52 changes: 52 additions & 0 deletions Alesia Yakutsionak/1/pun_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'redis'

class PunParser
BASE_URL = 'https://onelinefun.com'.freeze

def run
loop do
puts "Parsing #{@link}"
page = parse_page
@link = find_next_page_link(page)
break unless @link
end
save_puns_to_redis
end

def initialize
@link = '/puns/'
@redis = Redis.new
@puns = []
end

private

def parse_page
url = BASE_URL + @link
page = Nokogiri::HTML(URI.open(url))
page.css('article div p').each do |p|
pun = p.text
als6k marked this conversation as resolved.
Show resolved Hide resolved
@puns << pun
end
page
end

def find_next_page_link(page)
a_next = page.css('article div.p > a')
@link = if a_next && a_next.text =~ /^next/i
a_next.attr('href').value
else
nil
end
@link
end

def save_puns_to_redis
@puns.each_with_index do |pun, i|
@redis.set("pun_#{i}", pun)
end
end
end
3 changes: 3 additions & 0 deletions Alesia Yakutsionak/1/run_bot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require_relative 'bot.rb'

Bot.new.run
3 changes: 3 additions & 0 deletions Alesia Yakutsionak/1/run_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require_relative 'pun_parser.rb'

PunParser.new.run