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'
42 changes: 42 additions & 0 deletions Alesia Yakutsionak/1/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
GEM
remote: https://rubygems.org/
specs:
axiom-types (0.1.1)
descendants_tracker (~> 0.0.4)
ice_nine (~> 0.11.0)
thread_safe (~> 0.3, >= 0.3.1)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
equalizer (0.0.11)
faraday (0.15.4)
multipart-post (>= 1.2, < 3)
ice_nine (0.11.2)
inflecto (0.0.2)
mini_portile2 (2.4.0)
multipart-post (2.0.0)
nokogiri (1.10.1)
mini_portile2 (~> 2.4.0)
redis (4.1.0)
telegram-bot-ruby (0.8.6.1)
faraday
inflecto
virtus
thread_safe (0.3.6)
virtus (1.0.5)
axiom-types (~> 0.1)
coercible (~> 1.0)
descendants_tracker (~> 0.0, >= 0.0.3)
equalizer (~> 0.0, >= 0.0.9)

PLATFORMS
ruby

DEPENDENCIES
nokogiri
redis
telegram-bot-ruby

BUNDLED WITH
2.0.1
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`
- create `secrets.yml` file with your Telegram Bot token (see `secrets.yml.samle`)
- run `run_bot.rb`
- write command (/wordplay) to bot in Telegram
48 changes: 48 additions & 0 deletions Alesia Yakutsionak/1/bot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'telegram/bot'
require 'redis'
require 'yaml'

class Bot
INVALID_COMMAND_TEXT = "I didn't understand you.".freeze
SECRETS_PATH = 'secrets.yml'.freeze

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

def run
Telegram::Bot::Client.run(load_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
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 wordplay_text
random_pun = select_random_pun
"Random wordplay [#{random_pun[:number]}]:\n#{random_pun[:text]}"
end

def select_random_pun
i = rand(0..@puns_count - 1)
{ number: i, text: @redis.get("pun_#{i}") }
end

def load_token
YAML.load_file(SECRETS_PATH)['TELEGRAM_TOKEN']
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
1 change: 1 addition & 0 deletions Alesia Yakutsionak/1/secrets.yml.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TELEGRAM_TOKEN: "your telegram token"