Skip to content

Instantly share code, notes, and snippets.

@MegaApuTurkUltra
Created November 7, 2015 01:27
Show Gist options
  • Save MegaApuTurkUltra/96d411f0ac1b6c2232a6 to your computer and use it in GitHub Desktop.
Save MegaApuTurkUltra/96d411f0ac1b6c2232a6 to your computer and use it in GitHub Desktop.
<?php
error_reporting(0);
function autoloader($class) {
include "./" . str_replace("\\", "/", $class . '.php');
}
spl_autoload_register('autoloader');
use PHPHtmlParser\Dom;
// disable caching
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
// determine which topic to generate an image for
$topicurl = "";
if(isset($_GET['topicurl'])){
$topicurl = $_GET['topicurl'];
} else {
$topicurl = $_SERVER['HTTP_REFERER'];
}
$page = parse_url($topicurl, PHP_URL_PATH);
$topicId = explode("/", $page)[3];
// collect all posts
$topicPosts = [];
for($pageId = 1, $continueGettingPages = TRUE; $continueGettingPages; $pageId++){
$topicPage = file_get_contents("https://scratch.mit.edu/discuss/topic/$topicId/?page=$pageId&cachebuster=".mt_rand());
$dom = new Dom;
$dom->setOptions([
'whitespaceTextNode' => false
]);
$dom->load($topicPage);
$posts = $dom->find('div.blockpost');
foreach($posts as $i=>$post){
$user = $post->find(".username")->text();
$content = $post->find(".post_body_html");
array_push($topicPosts, array("user" => $user, "content" => $content));
}
$continueGettingPages = sizeof($posts) > 0;
}
// go through each post
// start from back to go with user's final decision
$usersAccountedFor = [];
$supportYes = [];
$supportNo = [];
$supportIdk = [];
// find OP
$OP = $topicPosts[0]["user"];
array_push($supportYes, $OP);
for($i = sizeof($topicPosts) - 1; $i > 0; $i--){
$post = $topicPosts[$i];
if($post["user"] == $OP || in_array($post["user"], $usersAccountedFor)){
// we don't want users' old decisions or the OP
continue;
}
$user = $post["user"];
$content = $post["content"];
$textContent = $content->text();
$blockQuotes = $content->find("blockquote");
$textOfInterest = $textContent;
if(trim($textContent) === ""){
// post is entirely made of blockquotes, look at the last one
if(sizeOf($blockQuotes) == 0){
$textOfInterest = "undecided?";
} else {
$textOfInterest = $blockQuotes[sizeof($blockQuotes) - 1]->text();
}
}
// apply regex
// regex is from Zro :P
preg_match_all("/((?!you(?:'re|r)? )(?:not?|don'?t) )?(support)(?!(?!.*[;:,.!]).*\\?)/i", $textOfInterest, $matches, PREG_SET_ORDER);
//var_dump($user);
//var_dump($matches);
$didAction = FALSE;
if(sizeof($matches) > 0){
for($l = sizeof($matches) - 1; $l > -1; $l--){
//echo $l;
if(isset($matches[$l][1]) && $matches[$l][1] !== NULL && $matches[$l][1] !== ""){
//echo "no sup\n";
array_push($usersAccountedFor, $user); // user is now accounted for
array_push($supportNo, $user); // user is added to no support
$supportIdk = array_diff($supportIdk, array($user)); // yet another one of PHP's super swag functions
$didAction = TRUE;
break;
} else if(isset($matches[$l][2]) && $matches[$l][2] !== NULL && $matches[$l][2] !== ""){
//echo "sup\n";
array_push($usersAccountedFor, $user); // user is now accounted for
array_push($supportYes, $user); //user is added to yes support
$supportIdk = array_diff($supportIdk, array($user));
$didAction = TRUE;
break;
}
}
}
if(!$didAction){
if(!in_array($user, $supportIdk))
array_push($supportIdk, $user);
}
}
// last step: render image
// var_dump($supportYes, $supportNo, $supportIdk);
header('Content-Type: image/jpeg');
$im_width = 500;
$im_height = 20;
$im_font = "./ARIAL.TTF";
$im_fontsize = 12;
function wrap($fontSize, $angle, $fontFace, $string, $width){
$ret = '';
$arr = explode(' ', $string);
foreach ($arr as $word){
$teststring = $ret.' '.$word;
$testbox = imagettfbbox($fontSize, $angle, $fontFace, $teststring);
if ($testbox[2] > $width){
$ret.=($ret==""?"":"\n").$word;
} else {
$ret.=($ret==""?"":' ').$word;
}
}
return explode("\n", $ret);
}
$text = [
"Supporters (" . sizeof($supportYes) . "):",
implode(", ", $supportYes),
"[break]",
"Non-supporters (" . sizeof($supportNo) . "):",
implode(", ", $supportNo),
"[break]",
"Undecided/Unknown (" . sizeof($supportIdk) . "):",
implode(", ", $supportIdk),
];
$textFinal = [];
for($i = 0; $i < sizeof($text); $i++){
$whatToRender = $text[$i];
$wrapped = wrap($im_fontsize, 0, $im_font, $whatToRender, $im_width);
foreach($wrapped as $j => $line){
array_push($textFinal, $line);
$testbox = imagettfbbox($im_fontsize, 0, $im_font, $line);
$im_height += abs($testbox[3] - $testbox[5]);
}
}
$im = imagecreatetruecolor($im_width, $im_height);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$red = imagecolorallocate($im, 100, 0, 0);
$green = imagecolorallocate($im, 0, 100, 0);
imagefilledrectangle($im, 0, 0, $im_width, $im_height, $white);
$y = 20;
foreach($textFinal as $k => $line){
$color = $black;
if(strpos($line, "Supporters (") === 0)
$color = $green;
if(strpos($line, "Non-supporters (") === 0)
$color = $red;
if($line !== "[break]")
imagettftext($im, $im_fontsize, 0, 0, $y, $color, $im_font, $line);
$testbox = imagettfbbox($im_fontsize, 0, $im_font, $line);
$y += abs($testbox[3] - $testbox[5]);
}
imagejpeg($im, NULL, 90);
imagedestroy($im);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment