Skip to content

Instantly share code, notes, and snippets.

@focustrate
Created October 27, 2012 19:36
Show Gist options
  • Save focustrate/3965822 to your computer and use it in GitHub Desktop.
Save focustrate/3965822 to your computer and use it in GitHub Desktop.
Sports Guy Tube backend
<?php
/*
* this is what "powers" http://sportsguytube.blogspot.com/
* It pulls all the youtube links from Bill Simmons' tweets and then posts them to Blogger via email.
* I put it together in a couple hours one night and built the site on a whim.
* Simmons tweeted it out to his 1MM followers: https://twitter.com/sportsguy33/status/4176287276
*
*/
// would need to parse out urls from sports guy's feed
$url = 'http://twitter.com/statuses/user_timeline/32765534.atom';
$regex = '/http\:\/\/[^\" ]+/i';
$video_posts = array();
require_once('simplepie.inc');
// Grab feed
$feed = new SimplePie();
$feed->set_feed_url( $url );
$feed->enable_cache(false);
$feed->init();
$feed->handle_content_type();
$counter = 0;
foreach ($feed->get_items() as $item) {
$tweet = $item->get_description();
$tweet = trim(str_replace("sportsguy33: ", "", $tweet));
$tweet_date = $item->get_local_date();
$tweet_link = $item->get_link();
// only process last 15 min
if(floor((time() - strtotime($tweet_date))/60) >= 15){
break;
}
preg_match_all($regex, $tweet, $matches);
if(!empty($matches[0])) {
foreach($matches[0] as $key=>$link) {
// we should really replace his link with a 2ti.me link ;)
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); // follow redirects recursively
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return to php
$page = curl_exec($ch);
if(!curl_errno($ch))
{
$info = curl_getinfo($ch); // get curl info, including final url
$url_parts = parse_url($info['url']);
if(preg_match('/([0-9a-z])*\.?youtube\.com/i',$url_parts['host'])) {
parse_str($url_parts['query']);
$video_posts[$counter]['tweet'] = $tweet;
$video_posts[$counter]['date'] = $tweet_date;
$video_posts[$counter]['link'] = $tweet_link;
$video_posts[$counter]['vids'][$key]['id'] = $v;
preg_match('/<title>(.*)<\/title>/i',$page,$title);
$video_posts[$counter]['vids'][$key]['title'] = trim(str_replace('YouTube - ', '', $title[1]));
}
}
// Close handle
curl_close($ch);
}
}
$counter++;
}
$to = '[[redacted]]@blogger.com';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
foreach($video_posts as $post) {
$titles = array();
$inner_body = '<strong><a href="'.$post['link'].'">The Sports Guy said:</a></strong><br /> '.$post['tweet'].'<br /><br />';
foreach($post['vids'] as $vid) {
array_push($titles, $vid['title']);
$inner_body .= getEmbedCode($vid['id']).'<br /><br />';
}
$subject = implode(' -- ', $titles);
$body = '<html><head><title>'.$subject.'</title></head><body>'.$inner_body.'</body></html>';
//mail($to, $subject, $body, $headers);
echo $subject.'<br /><br />';
echo $body.'<br /><hr />';
//flush;
}
exit;
function getEmbedCode($vid)
{
if(!$vid) { return; }
$embed = <<<END
<object width="425" height="355">
<param name="movie" value="http://www.youtube.com/v/$vid"></param>
<param name="allowFullScreen" value="true"></param>
<embed src="http://www.youtube.com/v/$vid"
type="application/x-shockwave-flash"
width="425" height="355"
allowfullscreen="true"></embed>
</object>
END;
return $embed;
}
?>
@focustrate
Copy link
Author

I would write this differently now (this was written a few years ago). Sometimes brute force just works, though. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment