Created
April 9, 2018 19:57
-
-
Save PrivateGER/44c8a9112aaceae5d84e588579626980 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'json' | |
require 'net/http' | |
require 'time' | |
$old_id = 0 | |
$total_length = 0 | |
$rant_count = 0 | |
def restore_data | |
if !File.zero?("log.txt") | |
data = File.open("log.txt") {|f| f.extend(Enumerable).inject {|_, ln| ln}}.gsub("\n", '').split(' ') | |
$total_length = data[1] | |
$rant_count = data[2] | |
else | |
File.open('log.txt', 'a') do |f| | |
f.write("0 0 0 \n") | |
end | |
end | |
end | |
def get_api_content(url) | |
uri = URI(url) | |
Net::HTTP.get(uri) | |
end | |
def count_substrings(string, substring) | |
string.each_char.each_cons(substring.size).map(&:join).count(substring) | |
end | |
restore_data | |
loop do | |
# We start by getting the first recent rant | |
rantjson = get_api_content('https://devrant.com/api/devrant/rants?app=3&limit=1&sort=recent') | |
rantjson = JSON.parse rantjson | |
rantid = rantjson["rants"][0]['id'] | |
rant_text = rantjson['rants'][0]['text'] | |
rant_text.downcase! | |
if rantjson['rants'][0]['id'] != $old_id | |
puts 'Analyzing new Rant with ID ' + rantid.to_s + '...' | |
length = rant_text.split(/[^[[:word:]]']+/).length.to_i | |
$rant_count = $rant_count.to_i + 1.to_i | |
$total_length = $total_length.to_i + length.to_i | |
puts $total_length.to_s + ' words in total' | |
puts $rant_count.to_s + ' rants' | |
puts "#{($total_length / $rant_count)} words in average" | |
File.open('log.txt', 'a') {|f| | |
f.write("#{rantid} #{$total_length} #{$rant_count} \n") | |
} | |
end | |
# puts rant_text | |
# puts $sum | |
$old_id = rantid | |
sleep 10 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment