Skip to content

Instantly share code, notes, and snippets.

@bearlikelion
Created October 15, 2013 02:06
Show Gist options
  • Save bearlikelion/6985491 to your computer and use it in GitHub Desktop.
Save bearlikelion/6985491 to your computer and use it in GitHub Desktop.
<?php
#!/usr/bin/php
include('ganon.php');
include("config.php");
include("Snoopy.class.php");
include("Googl.class.php");
require("TimeDiff.class.php");
# Constants
define('MAX_MATCHES', 6);
//Get shortened URL
function shortUrl($url)
{
//There has been at least 2 occasions in a 6 week period where a shortened URL returned false
//We'll give it 5 attempts to shorten the URL, if it fails we'll notice. The hashed URL files
//should have a size of 19 bytes, they'll be 0 if the URL failed to shorten.
$attempts = 0;
$short = "";
while(strlen($short) < 1 && $attempts < 5)
{
//Create it since it doesn't exist
$googl = new Googl('AIzaSyB08OAFSZ5MDgyx3YYt9iBhPkOj0a6HrHM');
$short = $googl->shorten($url);
//Increment attempts
$attempts++;
}
//Return
return $short;
}
//Parses 2 digit country code from country name
function parseFlag($country)
{
//Get country codes
include("countrycodes.array.php");
//Search array
//$code = strtolower(array_search($country, $countrycodes));
//Return
return str_replace('"', '', $country);
}
//Parse a json event to a string
function parseEvent($match)
{
if($match == null) return "* ~~x~~\n\n";
else
{
if ($match->match_live == true) $event = "* [**LIVE - " . trim($match->coverage_title) . "**](" . shortUrl($match->match_url). ") ";
else $event = "* [" . $match->match_countdown . " - " . trim($match->coverage_title) . "](" . shortUrl($match->match_url) . ") ";
$event .= " \n[](/" . parseFlag($match->team_1_country) . " \"" . $match->team_1_country . "\") " . $match->team_1_name . "" ;
$event .= " vs [](/" . parseFlag($match->team_2_country) . " \"" . $match->team_2_country . "\") " . $match->team_2_name . "\n\n";
}
return $event;
}
//Get the events info parsed
function getEvents()
{
$events = '';
$data = file_get_contents('http://api.dev.freakms.net/?action=match_upcoming&game=9');
$data = json_decode($data);
//Test json output
// print '<pre>';
// print_r($data);
// print '</pre>';
// exit;
$iteration = 0;
if (!empty($data))
{
foreach ($data as $match)
{
if ($iteration < MAX_MATCHES) {
$events .= parseEvent($match);
$iteration++;
}
}
if ($iteration < MAX_MATCHES)
{
if (isset($data->upcoming) && !empty($data->upcoming))
{
foreach ($data->upcoming as $upcoming)
{
if ($iteration < MAX_MATCHES)
{
$events .= parseEvent($upcoming);
$iteration++;
}
}
}
}
}
else
{
$events .= "* Match Ticker API timed out \n\n";
$iteration++;
}
if ($iteration < MAX_MATCHES)
{
while($iteration < MAX_MATCHES)
{
$events .= "* ~~x~~\n\n";
$iteration++;
}
}
return $events;
}
function getStatus()
{
$data = file_get_contents('http://www.dota2.com/overview/externaldata/');
$data = json_decode($data);
if (isset($data->rgPlayersTotal) && $data->rgPlayersTotal > 0) return '';
else return '> > **The Dota 2 Network is Down**';
}
//Ok lets do some really messy HTTP stuff
$snoopy = new Snoopy;
//Set some initialisation
$snoopy->agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0"; //User agent of the latest firefox version
//Login to reddit
$post['user'] = $r_user;
$post['passwd'] = $r_pass;
$post['api_type'] = "json";
$snoopy->submit("http://www.reddit.com/api/login/".$r_user, $post);
//Submit login
$login = json_decode($snoopy->results);
//Check if login was successful
if(count($login->json->errors) < 1)
{
//Check Dota 2 Network Status
$status = getStatus();
//Generate events list
$events = getEvents();
//Set cookies
$snoopy->cookies["reddit_session"] = $login->json->data->cookie;
//Get wiki info
$snoopy->fetch("http://www.reddit.com/r/".$r_subr."/about/edit/.json");
$admin = json_decode($snoopy->results);
//print_r($admin);
//print_r($login);
//Create description
$json = json_decode(file_get_contents("http://www.reddit.com/r/".$r_subr."/wiki/sidebar.json"));
$description = $json->data->content_md;
$description = str_replace("&gt;", ">", $description);
$description = str_replace('%%STATUS%%', $status, $description);
$description = str_replace("%%EVENTS%%", $events, $description);
//Set post values
$post = array();
$post['sr'] = $r_tid;
$post['title'] = $admin->data->title;
$post['thing_id'] = '';
$post['public_description'] = $admin->data->public_description;
$post['public_description_conflict_old'] = '';
$post['description'] = $description;
$post['description_conflict_old'] = '';
$post['prev_public_description_id'] = $admin->data->prev_public_description_id;
$post['prev_description_id'] = $admin->data->prev_description_id;
$post['lang'] = $admin->data->language;
$post['type'] = $admin->data->subreddit_type;
$post['link_type'] = $admin->data->content_options;
$post['wikimode'] = $admin->data->wikimode;
$post['wiki_edit_karma'] = $admin->data->wiki_edit_karma;
$post['wiki_edit_age'] = $admin->data->wiki_edit_age;
$post['allow_top'] = 'on';
$post['header-title'] = '';
$post['id'] = '#sr-form';
$post['r'] = 'dota2';
$post['uh'] = $login->json->data->modhash;
$post['renderstyle'] = 'html';
$post['comment_score_hide_mins'] = $admin->data->comment_score_hide_mins;
$post['public_traffic'] = 'on';
$post['spam_comments'] = 'low';
$post['spam_links'] = 'high';
$post['spam_selfposts'] = 'high';
// print '<pre>';
// print_r($post);
// print '</pre>';
// exit;
//Now submit all of this
$snoopy->submit("http://www.reddit.com/api/site_admin", $post);
echo "\n\n" . date("[Y/M/d - H:i]: <br>") . $description;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment