Created
April 1, 2017 21:37
-
-
Save shelvacu/2ff5f4538d807b90de65aa78692ddbd4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# CC0: To the extent possible under law, Shelvacu has waived all copyright and related or neighboring rights to Reddit Place Event Recorder. This work is published from: United States. | |
# A ugly but simple logger for every event (pixel placements, participation number updates) on www.reddit.com/place | |
# First you need to `gem install websocket-client-simple` | |
require 'websocket-client-simple' | |
require 'open-uri' | |
$logprefix = "pixel-placement" | |
$lines = 0 | |
$linelimit = 100_000 | |
$log_number = 0 | |
$fh = nil | |
$watchdog = false | |
def switch_fh | |
if !$fh.nil? | |
$fh.close | |
end | |
while File.exist?($logprefix+"."+$log_number.to_s+".log") || File.exist?($logprefix+"."+$log_number.to_s+".log.gz") | |
$log_number += 1 | |
end | |
$lines = 0 | |
fn = $logprefix+"."+$log_number.to_s+".log" | |
puts "opening new file #{fn}" | |
$fh = File.open(fn, "wt") | |
end | |
switch_fh | |
loop do | |
begin | |
first = true | |
puts "attempting to scrape" | |
data = open("https://www.reddit.com/r/place/").read | |
websocket_url = /"place_websocket_url": "([^"]+)"/.match(data)[1] | |
puts "websocket_url is #{websocket_url.inspect}" | |
#exit | |
ws = WebSocket::Client::Simple.connect( | |
websocket_url, | |
headers: {"User-Agent" => "Ruby/#{RUBY_VERSION} RubyWebsocketSimple/#{WebSocket::Client::Simple::VERSION} (place pixel recorder script by /u/shelvac2)"} | |
) do |ws| | |
ws.on :message do |msg| | |
puts msg.inspect if first | |
raise "ex" if msg.type == :close | |
print "."# if lines % 100 == 0 | |
if $lines >= $linelimit | |
switch_fh | |
end | |
$fh.puts "[#{Time.now.strftime("%FT%T %z")}] #{msg.data}" | |
$lines += 1 | |
first = false | |
$watchdog = false # pet the dog | |
end | |
ws.on :open do | |
puts "opened!" | |
end | |
ws.on :close do |e| | |
p e | |
end | |
ws.on :error do |e| | |
p e | |
end | |
end | |
loop do | |
sleep 5 | |
if $watchdog | |
raise "watchdog activated, reconnecting" | |
end | |
$watchdog = true # un-pet(?) the dog | |
end | |
rescue StandardError => e | |
p e | |
end | |
sleep 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment