Created
January 19, 2011 17:51
-
-
Save marianzange/786545 to your computer and use it in GitHub Desktop.
A simple bot that extracts the daily lunch menu of my college's cafeteria and posts it to twitter.
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
<?php | |
// Load required libs and stuff | |
require_once('lib/twitteroauth/twitteroauth.php'); | |
require_once('config.php'); | |
require_once('lib/simple_html_dom.php'); | |
$tweets = Array(); | |
$content = Array(); | |
// Create a TwitterOauth object with consumer/user tokens. | |
$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET); | |
// Get the DOM from URL | |
$html = file_get_html('http://www.swo.uni-bayreuth.de/mensa/haupt_1klick.php?offset=0'); | |
// Find all article blocks | |
foreach($html->find('td[width="*"]') as $a) { | |
$content[] = iconv('ISO-8859-1', 'UTF-8//TRANSLIT',trim($a->plaintext, " ")); | |
} | |
// Get the the lunch menu items from the pile | |
$tweets[] = date("D") . ": " . $content[1] . ", " . $content[4]; | |
if (count($content) < 12) { | |
$tweets[] = date("D") . ": " . $content[7]; | |
} else { | |
$tweets[] = date("D") . ": " . $content[10] . ", " . $content[7]; | |
} | |
// Shorten if it's longer than 135 characters (135+hashtag = 140) | |
// Twitter API blocks messages longer than 140 characters. | |
foreach($tweets as $i => $t) { | |
if (strlen($t) > 135) { | |
$tweets[$i] = substr($t, 0, 134); | |
} | |
} | |
// Post to twitter | |
foreach($tweets as $t) { | |
$twitter->post('statuses/update', array('status' => $t . " #ubt")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment