Created
February 14, 2015 17:48
-
-
Save astorm/e9c810008310613cdc2a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/php | |
<?php | |
namespace Pulsestorm\Stackexchange\Magento\Noupvotes; | |
/** | |
* A simple shell script for returning a list of questions | |
* with an answer or have answers with no upvotes. Suffers | |
* from "thrown together for my own use" syndrome, but it | |
* does the job. | |
* | |
* @author Alan Storm http://alanstorm.com | |
*/ | |
function curlGetApiContents($url) | |
{ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL,$url); | |
curl_setopt($ch, CURLOPT_FAILONERROR,1); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 15); | |
curl_setopt($ch, CURLOPT_ENCODING , "gzip"); //http://api.stackoverflow.com/1.1/usage/gzip | |
$response = curl_exec($ch); | |
return $response; | |
} | |
function out($string) | |
{ | |
echo $string, "\n"; | |
} | |
function taskDownloadAllUnanswered() | |
{ | |
$results = array(); | |
$has_more = '1'; | |
$page = 1; | |
while($has_more) | |
{ | |
$json = curlGetApiContents("https://api.stackexchange.com/2.1/questions/unanswered?page=$page&pagesize=100&order=desc&sort=activity&site=magento"); | |
$json = json_decode(trim($json)); | |
$results[] = $json; | |
out("Downloaded page $page of unanswered questions"); | |
$page++; | |
$has_more = false; | |
if(isset($json->has_more)) | |
{ | |
$has_more = $json->has_more; | |
} | |
else | |
{ | |
out("JSON missing has_more — this shouldn't happen (network?)."); | |
} | |
} | |
foreach($results as $key=>$json) | |
{ | |
file_put_contents(getBaseStorageFolder() . "/magento_unanswered_$key", json_encode($json)); | |
} | |
} | |
function getBaseStorageFolder() | |
{ | |
return '/Users/alanstorm/Documents/stack-exchange'; | |
} | |
function taskReadItemsFromDisk() | |
{ | |
$files = glob(getBaseStorageFolder() . '/magento_unanswered_*'); | |
$items = array(); | |
foreach($files as $file) | |
{ | |
$data = file_get_contents($file); | |
$data = json_decode($data); | |
$items = array_merge($items, $data->items); | |
} | |
return $items; | |
} | |
function taskGetAllQuestionIds() | |
{ | |
$items = taskReadItemsFromDisk(); | |
$ids = array(); | |
foreach($items as $item) | |
{ | |
$ids[] = $item->question_id; | |
} | |
return($ids); | |
} | |
function chunkArray($to_chunk, $size=100) | |
{ | |
$chunked = array(); | |
$count = 0; | |
while($array = array_slice($to_chunk,($count*$size),$size)) | |
{ | |
$chunked[] = $array; | |
$count++; | |
} | |
return $chunked; | |
} | |
function taskDownloadAllUnansweredWithAnswers() | |
{ | |
$question_ids = taskGetAllQuestionIds(); | |
$question_ids = chunkArray($question_ids); | |
$results = array(); | |
$set = 1; | |
foreach($question_ids as $ids) | |
{ | |
$param = implode(';',$ids); | |
$page = 1; | |
$has_more=true; | |
while($has_more) | |
{ | |
$url = 'http://api.stackexchange.com/2.1/questions/' . | |
$param . '/answers?order=desc&sort=activity&site=magento&page='.$page; | |
$json = curlGetApiContents($url); | |
$results[] = $json; | |
out("Downloaded page $page of unanswered questions with answers from set $set."); | |
out($url); | |
$has_more = false; | |
$json = json_decode(trim($json)); | |
if(isset($json->has_more)) | |
{ | |
$has_more = $json->has_more; | |
} | |
else | |
{ | |
out("JSON missing has_more — this shouldn't happen (network?)."); | |
exit(__FUNCTION__); | |
} | |
$page++; | |
} | |
$set++; | |
} | |
foreach($results as $key=>$json) | |
{ | |
file_put_contents(getBaseStorageFolder() . "/magento_withanswer_$key", $json); | |
} | |
} | |
function taskOutputLinks($how_many=false) | |
{ | |
$files = glob(getBaseStorageFolder() . '/magento_withanswer_*'); | |
$items = array(); | |
foreach($files as $file) | |
{ | |
$data = json_decode(file_get_contents($file)); | |
if(!isset($data->items)) | |
{ | |
continue; | |
} | |
$items = array_merge($items, $data->items); | |
} | |
$report = array(); | |
// $report[] = '<style type="text/css"> | |
// body | |
// { | |
// font-size:24px; | |
// } | |
// ul li | |
// { | |
// margin-bottom:.6em; | |
// } | |
// </style>'; | |
$already_done = array(); | |
foreach($items as $answer) | |
{ | |
if(!in_array($answer->question_id,$already_done)) | |
{ | |
$url = 'http://magento.stackexchange.com/questions/' . $answer->question_id; | |
$url = '<li><a href="' . | |
$url . '">' . $url . | |
'</a></li>'; | |
$report[] = $url; | |
} | |
$already_done[] = $answer->question_id; | |
} | |
$count = $how_many ? $how_many : count($report); | |
foreach(array_rand($report,$count) as $item) | |
{ | |
echo $report[$item],"\n"; | |
} | |
} | |
function main($argv) | |
{ | |
if(!is_dir(getBaseStorageFolder())) | |
{ | |
exit("Could not find " . getBaseStorageFolder() . "\n" . 'Create this folder ' . | |
'or modify getBaseStorageFolder();' . "\n\n"); | |
} | |
taskDownloadAllUnanswered(); | |
taskDownloadAllUnansweredWithAnswers(); | |
taskOutputLinks(10); | |
} | |
if(isset($argv)) | |
{ | |
main($argv); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment