Instantly share code, notes, and snippets.
Created
March 20, 2015 07:51
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save GusGold/439eef495fe4504a9482 to your computer and use it in GitHub Desktop.
Update a subreddit's sidebar with the current time (Can be expanded to update anything)
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 | |
define("COOKIES", __dir__."/_cookies.txt"); | |
$reddit_username = ""; | |
$reddit_password = ""; | |
$reddit_subreddit = ""; | |
function getCURL($url, $opt = array()){ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_USERAGENT, "RedditSideBarUpdater/1.0 by GusGold"); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIES); | |
curl_setopt($ch, CURLOPT_ENCODING, ""); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_AUTOREFERER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 30); | |
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); | |
curl_setopt($ch, CURLOPT_VERBOSE, false); | |
curl_setopt($ch, CURLOPT_HEADER, true); | |
curl_setopt_array($ch, $opt); | |
$response_raw = curl_exec($ch); | |
$header = str_replace("\r\n\r\n", "", substr($response_raw, 0, curl_getinfo($ch, CURLINFO_HEADER_SIZE))); | |
foreach (explode("\r\n", $header) as $i => $line){ | |
if ($i === 0){ | |
$headers['http_code'] = $line; | |
} else { | |
list ($key, $value) = explode(': ', $line); | |
$headers[$key] = $value; | |
} | |
} | |
$body = substr($response_raw, curl_getinfo($ch, CURLINFO_HEADER_SIZE)); | |
curl_close($ch); | |
return array( | |
"header" => $headers, | |
"body" => $body, | |
"cookies" => COOKIES); | |
} | |
function checkLoggedIn(){ //Check if the current cookies are valid by getting the authed users info | |
global $modHash; | |
$login = json_decode(getCURL("https://ssl.reddit.com/api/me.json", array( | |
CURLOPT_COOKIEFILE => COOKIES))["body"], true); | |
if(isset($login["data"]["name"])){ | |
if($login["data"]["name"] === $reddit_username){ | |
$modHash = $login["data"]["modhash"]; | |
return true; | |
} | |
} | |
return false; | |
} | |
$loggedIn = false; | |
if(!checkLoggedIn()){ //User isn't authed, so login | |
$response = getCURL("https://ssl.reddit.com/api/login", array( | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_POSTFIELDS => http_build_query(array( | |
"api_type" => "json", | |
"user" => $reddit_username, | |
"passwd" => $reddit_password, | |
"rem" => true)), | |
CURLOPT_HTTPHEADER => array( | |
"Accept: application/json"))); | |
if($response["body"]){ | |
if(count(json_decode($response["body"], true)["json"]["errors"]) === 0){ | |
$loggedIn = true; | |
} | |
} | |
print_r($response); | |
} else { | |
$loggedIn = true; | |
} | |
if(!$loggedIn){ //Not able to login (wrong password, etc.) | |
file_put_contents("php://stderr", "Unable to log in", FILE_APPEND); | |
die("Wasn't able to log in"); | |
} | |
$response = getCURL("http://www.reddit.com/r/" . $reddit_subreddit . "/about/edit.json", array( //Get settings from subreddit as they all need to be supplied when updating the sidebar | |
CURLOPT_COOKIEFILE => COOKIES)); | |
if($response["body"]){ | |
$current = json_decode($response["body"], true)["data"]; | |
} else { | |
file_put_contents("php://stderr", "Unable to get current SR info: " . print_r($response), FILE_APPEND); | |
die("Unable to get current SR info"); | |
} | |
$curDes = explode("\n\n", $current["description"]); //Split description (aka sidebar) by double line breaks (which render as a single line break in markdown) into an array | |
foreach($curDes as $line => $text){ | |
echo $curDes[$line], "<br>"; | |
if(substr($curDes[$line], 0, 22) === "###Current Server Time"){ //Find the start of the area to update | |
$linePosition = $line; | |
break; | |
} | |
} | |
if(!$linePosition){ //Start area wasn't found, so don't continue | |
file_put_contents("php://stderr", "Unable to find server time string: " . print_r($curDes), FILE_APPEND); | |
die("Unable to find server time string"); | |
} | |
$newDes = $curDes; | |
$time = new DateTime("now", new DateTimeZone("America/Los_Angeles")); //Get the current time in the wanted timezone | |
$newDes[$linePosition] = "###Current Server Time\n" . $time->format("m/d/Y - H:i") . " PST"; //Format the time and replace the old time with new time | |
$description = implode("\n\n", $newDes); //Rejoin sidebar array by double line breaks; | |
$description = html_entity_decode($description); //Issues on reddit side with sending encoded entities, but requiring the decoded entities in return | |
$new = array( //Move current settings along with updated settings | |
"api_type" => "json", | |
"comment_score_hide_mins" => $current["comment_score_hide_mins"], | |
"css_on_cname" => $current["domain_css"], //Inconsistencies with reddit send/receive var names | |
"description" => $description, //To update | |
"header-title" => $current["header-title"], | |
"lang" => $current["language"], //More inconsistencies | |
"link_type" => $current["content_options"], //and more | |
"name" => "", | |
"public_description" => $current["public_description"], | |
"show_cname_sidebar" => $current["domain_sidebar"], //and more | |
"spam_comments" => $current["spam_comments"], | |
"spam_links" => $current["spam_links"], | |
"spam_selfposts" => $current["spam_selfposts"], | |
"sr" => $current["subreddit_id"], //and more | |
"submit_link_label" => $current["submit_link_label"], | |
"submit_text" => $current["submit_text"], | |
"submit_text_label" => $current["submit_text_label"], | |
"title" => $current["title"], | |
"type" => $current["subreddit_type"], //and even more | |
"uh" => $modHash, | |
"wiki_edit_age" => $current["wiki_edit_age"], | |
"wiki_edit_karma" => $current["wiki_edit_karma"], | |
"wikimode" => $current["wikimode"]); | |
//Further inconsistencies. The following 7 if statements fix a reddit issue that means that if the key is set, the setting is true, regardless of the value | |
if($current["default_set"]){ | |
$new["allow_top"] = "on"; | |
} | |
if($current["collapse_deleted_comments"]){ | |
$new["collapse_deleted_comments"] = "on"; | |
} | |
if($current["over_18"]){ | |
$new["over_18"] = "on"; | |
} | |
if($current["show_media"]){ | |
$new["show_media"] = "on"; | |
} | |
if($current["public_traffic"]){ | |
$new["public_traffic"] = "on"; | |
} | |
if($current["collapse_deleted_comments"]){ | |
$new["collapse_deleted_comments"] = "on"; | |
} | |
if($current["exclude_banned_modqueue"]){ | |
$new["exclude_banned_modqueue"] = "on"; | |
} | |
$response = getCURL("http://www.reddit.com/api/site_admin", array( //Send old settings merged with new sidebar | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_POSTFIELDS => http_build_query($new), | |
CURLOPT_HTTPHEADER => array( | |
"Accept: application/json"), | |
CURLOPT_COOKIEFILE => COOKIES)); | |
if($response["body"]){ | |
if(count(json_decode($response["body"], true)["json"]["errors"]) === 0){ | |
die("Success!"); | |
} | |
} | |
file_put_contents("php://stderr", "Unable to update SR info: " . print_r($response), FILE_APPEND); | |
die("failure"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment