Skip to content

Instantly share code, notes, and snippets.

@gisikw
Last active May 8, 2016 22:24
Show Gist options
  • Save gisikw/947d579dd4789b11dead to your computer and use it in GitHub Desktop.
Save gisikw/947d579dd4789b11dead to your computer and use it in GitHub Desktop.
Log the homepage position of a Reddit post.
var page = require("webpage").create(),
url = 'http://reddit.com?limit=100';
page.open(url, function (status) {
page.onUrlChanged = function(targetUrl) {
console.log('New URL: ' + targetUrl);
phantom.exit();
};
page.onCallback = function(msg) {
page.render((new Date()).getTime()+'-reddit.jpg');
phantom.exit();
};
page.viewportSize = {
width: 1280,
height: 720
};
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit();
} else {
page.evaluate(function () {
setTimeout(function () {
window.callPhantom();
}, 15000);
});
}
});
require 'open-uri'
require 'json'
FINDER_BLOCK = ->(c){c['data']['url'] == "https://youtu.be/mvK0UzFNw1Q"}
LIMIT = 100
@cleanPos = 0
@pos = 0
while true
begin
posts = JSON.parse(open("http://www.reddit.com/.json?limit=#{LIMIT}").read)['data']['children']
newCleanPos = posts.reject{|c|c['data']['over_18']}.find_index(&FINDER_BLOCK) + 1
newPos = posts.find_index(&FINDER_BLOCK) + 1
if (newPos != @pos) || (newCleanPos != @cleanPos)
@cleanPos = newCleanPos
@pos = newPos
str = "#{Time.now}: Video now at #{@pos} (#{@cleanPos} clean)"
open('redditLog.txt', 'a'){|f| f << str}
puts str
# Take a screenshot for posterity
`phantomjs redditShot.js`
end
# Be nice to Reddit
sleep 2.5
rescue
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment