Skip to content

Instantly share code, notes, and snippets.

@bootell
Created January 15, 2019 09:15
Show Gist options
  • Save bootell/18fada56f7262ec881c5c46829e40e78 to your computer and use it in GitHub Desktop.
Save bootell/18fada56f7262ec881c5c46829e40e78 to your computer and use it in GitHub Desktop.
<?php
error_reporting(0);
function get_raw_url($url)
{
$parse = parse_url($url);
return 'https://raw.githubusercontent.com' . $parse['path'] . '/master/readme.md';
}
function getData($object)
{
$url = get_raw_url($object['url']);
$result = file_get_contents($url);
if (!$result) {
$result = file_get_contents(str_replace('readme', 'README', $url));
}
$rows = explode("\n", $result);
$category = null;
$category_except = ['contents'];
$data = [];
foreach ($rows as $row) {
if (strpos($row, '##') !== false) {
$str = substr($row, 3);
if (!in_array(strtolower($str), $category_except)) {
$category = $str;
}
continue;
}
if ($category && strpos($row, '- ') !== false) {
preg_match('/.*- \[(.*)\]\((.*)\)( - )?(.*)/', $row, $matches);
$item = [];
$item['category'] = $category;
list(, $item['name'], $item['url'], ,$item['description']) = $matches;
$data[] = $item;
}
}
return $data;
}
$data[0] = [
'category' => 'awesome',
'name' => 'awesome',
'url' => 'https://github.com/sindresorhus/awesome',
'description' => null,
'content' => [],
];
$data[0]['content'] = getData($data[0]);
foreach ($data[0]['content'] as &$datum) {
echo $count ++ . PHP_EOL;
$datum['content'] = getData($datum);
}
file_put_contents('/tmp/github.json', json_encode($data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment