Skip to content

Instantly share code, notes, and snippets.

@ouoam
Created April 5, 2018 08:28
Show Gist options
  • Select an option

  • Save ouoam/95679ff71ad4a52a638963fdcc815706 to your computer and use it in GitHub Desktop.

Select an option

Save ouoam/95679ff71ad4a52a638963fdcc815706 to your computer and use it in GitHub Desktop.
Load Task List From Programming.in.th
<?php
date_default_timezone_set("Asia/Bangkok");
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=taskList.csv");
header("Pragma: no-cache");
header("Expires: 0");
$ids = "148320,161291,175854,175857,175860,156769";
$ids = explode(",", $ids);
$re = getData("https://programming.in.th/login.php?getUsername=***************&getPassword=****************&getRemember=");
$data = array();
$maxTask = 0;
foreach($ids as $key => $id) {
$data[$key] = getTask($id, $re["cookie"]);
$maxTask = max($maxTask, count($data[$key]));
}
$line = array();
for($i = 0; $i <= $maxTask; $i++) {
$line[$i] = "";
}
foreach($data as $key => $taskList) {
$i = 0;
$line[$i++] .= "\"" . $ids[$key] . "\",\"" . count($taskList) . "\",";
foreach($taskList as $key => $val) {
$line[$i++] .= "\"" . $key . "\",\"" . $val . "\",";
}
while($i <= $maxTask){
$line[$i++] .= "\"\",\"\",";
}
}
foreach($line as $key => $val) {
$line[$key] = substr($line[$key], 0, -1);
}
$line[0] .= ",\"" . date("c") . "\"";
//print_r($line);
echo implode("\n", $line);
function getTask($id, $cookie) {
$re = getData("https://programming.in.th/profile.php?id=" . $id, $cookie);
$get = $re["body"];
//-----------------Fix Bad HTML Source Format-------------------//
$get = str_replace("&nbsp;", "", $get);
$get = str_replace(" & ", "", $get);
$get = str_replace("&pid", "", $get);
$geta = explode("\n", $get);
$geta[124] .= "</span>";
$get = implode("\n", $geta);
//---------------------------------------------------------------//
$xml = simplexml_load_string($get);
$xml = object2array($xml);
$past = $xml["body"]["div"]["div"]["div"]["div"][0]["form"]["fieldset"][2]["div"];
$stat[0] = $past[0]["a"];
$stat[1] = $past[1]["a"];
array_pop($stat[1]);
$data = array();
//print pass task
foreach($stat[0] as $key => $val) {
//echo $val;
//echo "\tpass\n";
$data[$val] = "pass";
}
//print not pass task
foreach($stat[1] as $key => $val) {
$res = getData("https://programming.in.th/task/showStatus.txt.php?page=1&uid=" . $id . "&pid=" . $val ."&timeMode=0");
$get = $res["body"];
//-----------------Fix Bad HTML Source Format-------------------//
$geta = explode("</tr>", $get);
$end = count($geta) - 2;
for($i = 1; $i < $end; $i++){
$geta[$i] .= "</td>";
}
$get = implode("</tr>", $geta);
//---------------------------------------------------------------//
$xml = simplexml_load_string($get);
$xml = object2array($xml);
$detail = $xml["tr"]["1"]["td"][5];
if(!is_string($detail)){
$detail = $detail["a"];
}
//echo $val . "\t" . $detail . "\n";
$data[$val] = $detail;
}
ksort($data);
return $data;
}
function object2array($object) { return @json_decode(@json_encode($object),1); }
function getData($url, $cookieIN="") {
$ch = curl_init( $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
if($cookieIN != "") {
$headers = array();
$headers[] = $cookieIN;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$response = curl_exec($ch);
// Then, after your curl_exec call:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$cookie = "Cookie: ";
if($cookieIN == "") {
$headers = explode("\n", $header);
foreach($headers as $header){
$ex = explode(": ", $header);
if($ex[0] == "Set-Cookie") {
$ex2 = explode("; ", $ex[1]);
$cookie .= $ex2[0]."; ";
}
}
$cookie = substr($cookie, 0, -2);
}
return [
"cookie" => $cookie,
"body" => $body
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment