How to resolve the algorithm Retrieve and search chat history step by step in the Ruby programming language
How to resolve the algorithm Retrieve and search chat history step by step in the Ruby programming language
Table of Contents
Problem Statement
Summary: Find and print the mentions of a given string in the recent chat logs from a chatroom. Only use your programming language's standard library. Details: The Tcl Chatroom is an online chatroom. Its conversations are logged. It's useful to know if someone has mentioned you or your project in the chatroom recently. You can find this out by searching the chat logs. The logs are publicly available at http://tclers.tk/conferences/tcl/. One log file corresponds to the messages from one day in Germany's current time zone. Each chat log file has the name YYYY-MM-DD.tcl where YYYY is the year, MM is the month and DD the day. The logs store one message per line. The messages themselves are human-readable and their internal structure doesn't matter. Retrieve the chat logs from the last 10 days via HTTP. Find the lines that include a particular substring and print them in the following format: The substring will be given to your program as a command line argument. You need to account for the possible time zone difference between the client running your program and the chat log writer on the server to not miss any mentions. (For example, if you generated the log file URLs naively based on the local date, you could miss mentions if it was already April 5th for the logger but only April 4th for the client.) What this means in practice is that you should either generate the URLs in the time zone Europe/Berlin or, if your language can not do that, add an extra day (today + 1) to the range of dates you check, but then make sure to not print parts of a "not found" page by accident if a log file doesn't exist yet. The code should be contained in a single-file script, with no "project" or "dependency" file (e.g., no requirements.txt for Python). It should only use a given programming language's standard library to accomplish this task and not rely on the user having installed any third-party packages. If your language does not have an HTTP client in the standard library, you can speak raw HTTP 1.0 to the server. If it can't parse command line arguments in a standalone script, read the string to look for from the standard input.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Retrieve and search chat history step by step in the Ruby programming language
This code fetches webpages from a given URL and checks if they contain a given string. The URL is generated by adding a number of days to the current time and formatting it in a specific timezone. The webpage is fetched using the Net::HTTP library and the resulting HTML is split into lines. The lines are then filtered to only include those that contain the given string. If any matches are found, the URL and the matching lines are printed to the console.
The main function takes an optional argument, which is the string to search for. It then generates URLs for the last 10 days and checks each webpage for the given string. If any matches are found, they are printed to the console.
Source code in the ruby programming language
#! /usr/bin/env ruby
require 'net/http'
require 'time'
def gen_url(i)
day = Time.now + i*60*60*24
# Set the time zone in which to format the time, per
# https://coderwall.com/p/c7l82a/create-a-time-in-a-specific-timezone-in-ruby
old_tz = ENV['TZ']
ENV['TZ'] = 'Europe/Berlin'
url = day.strftime('http://tclers.tk/conferences/tcl/%Y-%m-%d.tcl')
ENV['TZ'] = old_tz
url
end
def main
back = 10
needle = ARGV[0]
(-back..0).each do |i|
url = gen_url(i)
haystack = Net::HTTP.get(URI(url)).split("\n")
mentions = haystack.select { |x| x.include? needle }
if !mentions.empty?
puts "#{url}\n------\n#{mentions.join("\n")}\n------\n"
end
end
end
main
You may also check:How to resolve the algorithm Show the epoch step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm K-d tree step by step in the D programming language
You may also check:How to resolve the algorithm Pick random element step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Create a two-dimensional array at runtime step by step in the F# programming language
You may also check:How to resolve the algorithm Boyer-Moore string search step by step in the Yabasic programming language