Created
March 1, 2013 16:16
-
-
Save emre/5065711 to your computer and use it in GitHub Desktop.
tweet freezer
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
| function TweetFreezer(casper_instance) { | |
| this.casper = casper_instance; | |
| this.tweet_url = false; | |
| this.set_tweet_url = function(tweet_url) { | |
| this.tweet_url = tweet_url; | |
| return this; | |
| }; | |
| /* extracts tweet id from the url */ | |
| this.find_tweet_id = function() { | |
| var match = this.tweet_url.match("status/([0-9]*)?"); | |
| if (match) { | |
| return match[1]; | |
| } | |
| return false; | |
| }; | |
| this.freeze_tweet = function () { | |
| console.log('freezing.'); | |
| console.log(this.tweet_url); | |
| tweet_id = this.find_tweet_id(); | |
| casper.start(this.tweet_url, function() { | |
| this.captureSelector( tweet_id+ ".png", '.permalink-tweet-container'); | |
| this.echo('{"status": "success"}'); | |
| }); | |
| casper.run(); | |
| }; | |
| this.init = function() { | |
| tweet_url = casper.cli.get(0); | |
| if(typeof tweet_url == 'undefined') { | |
| console.log('{"status": "error"}'); | |
| casper.exit(); | |
| } | |
| this.set_tweet_url(tweet_url); | |
| if(!this.is_tweet_url_valid()) { | |
| console.log({'status': "error"}); | |
| casper.exit(); | |
| } | |
| this.freeze_tweet(); | |
| }; | |
| this.is_tweet_url_valid = function() { | |
| var match = this.tweet_url.match("https?://twitter.com/[a-zA-Z_-]+/status/[0-9]+/?"); | |
| if(match) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| } | |
| var casper = require("casper").create(); | |
| tweet_freezer = new TweetFreezer(casper); | |
| tweet_freezer.init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment