diff --git a/Alesia Yakutsionak/1/Gemfile b/Alesia Yakutsionak/1/Gemfile new file mode 100755 index 0000000..252e28e --- /dev/null +++ b/Alesia Yakutsionak/1/Gemfile @@ -0,0 +1,5 @@ +source 'https://rubygems.org' + +gem 'nokogiri' +gem 'redis' +gem 'telegram-bot-ruby' diff --git a/Alesia Yakutsionak/1/Gemfile.lock b/Alesia Yakutsionak/1/Gemfile.lock new file mode 100644 index 0000000..4874a30 --- /dev/null +++ b/Alesia Yakutsionak/1/Gemfile.lock @@ -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 diff --git a/Alesia Yakutsionak/1/README.md b/Alesia Yakutsionak/1/README.md new file mode 100755 index 0000000..b06262e --- /dev/null +++ b/Alesia Yakutsionak/1/README.md @@ -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 diff --git a/Alesia Yakutsionak/1/bot.rb b/Alesia Yakutsionak/1/bot.rb new file mode 100644 index 0000000..c7ecbaf --- /dev/null +++ b/Alesia Yakutsionak/1/bot.rb @@ -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) + 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 diff --git a/Alesia Yakutsionak/1/pun_parser.rb b/Alesia Yakutsionak/1/pun_parser.rb new file mode 100644 index 0000000..54beb2e --- /dev/null +++ b/Alesia Yakutsionak/1/pun_parser.rb @@ -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 + @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 diff --git a/Alesia Yakutsionak/1/run_bot.rb b/Alesia Yakutsionak/1/run_bot.rb new file mode 100644 index 0000000..6a93afd --- /dev/null +++ b/Alesia Yakutsionak/1/run_bot.rb @@ -0,0 +1,3 @@ +require_relative 'bot.rb' + +Bot.new.run diff --git a/Alesia Yakutsionak/1/run_parser.rb b/Alesia Yakutsionak/1/run_parser.rb new file mode 100644 index 0000000..5c131ed --- /dev/null +++ b/Alesia Yakutsionak/1/run_parser.rb @@ -0,0 +1,3 @@ +require_relative 'pun_parser.rb' + +PunParser.new.run diff --git a/Alesia Yakutsionak/1/secrets.yml.sample b/Alesia Yakutsionak/1/secrets.yml.sample new file mode 100644 index 0000000..35e1719 --- /dev/null +++ b/Alesia Yakutsionak/1/secrets.yml.sample @@ -0,0 +1 @@ +TELEGRAM_TOKEN: "your telegram token"