Skip to content

Instantly share code, notes, and snippets.

@chrisfromredfin
Last active August 26, 2021 07:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisfromredfin/6d434d52cd9116d06c599fa8dfb0bbab to your computer and use it in GitHub Desktop.
Save chrisfromredfin/6d434d52cd9116d06c599fa8dfb0bbab to your computer and use it in GitHub Desktop.
<?php
define('START', 0);
define('END', 948);
$output_handle = fopen('./projects.csv', 'w');
fputcsv($output_handle, [
'category',
'development status',
'maintained',
'body',
'project type',
'machine name',
'has issue queue',
'components',
'has releases',
'homepage url',
'changelog url',
'demo url',
'documentation url',
'screenshots url',
'license',
'number of images',
'number supporting orgs',
'issue version options',
'security coverage',
'ecosystem(s)',
'next major version info',
'has been replaced',
'has issue summary template',
'stars',
'nid',
'title',
'url',
'total usage',
]);
for ($number = START; $number <= END; $number++) {
echo "Processing page $number\n";
$resource = file_get_contents("https://www.drupal.org/api-d7/node.json?type=project_module&page=$number", 'r');
$json = json_decode($resource);
$all_modules = $json->list;
// Iterate through all the modules.
foreach($all_modules as $module) {
$row = [];
// Vocabulary 3.
$v3 = [];
foreach($module->taxonomy_vocabulary_3 as $term) {
$v3[] = $term->id;
}
$row['vocab_3'] = '|' . implode('|', $v3) . '|';
// Vocabulary 46.
$row['vocab_46'] = ($module->taxonomy_vocabulary_46 ? $module->taxonomy_vocabulary_46->id : '');
// Vocabulary 44.
$row['vocab_44'] = ($module->taxonomy_vocabulary_44 ? $module->taxonomy_vocabulary_44->id : '');
$row['body']= substr($module->body->value, 0, 100);
$row['field_project_type'] = $module->field_project_type;
$row['field_project_machine_name'] = $module->field_project_machine_name;
$row['field_project_has_issue_queue'] = $module->field_project_has_issue_queue;
$row['field_project_components'] = '|' . implode('|', $module->field_project_components) . '|';
$row['field_project_has_releases'] = $module->field_project_has_releases;
$row['field_project_homepage'] = $module->field_project_homepage->url;
$row['field_project_changelog'] = $module->field_project_changelog->url;
$row['field_project_demo'] = $module->field_project_demo->url;
$row['field_project_documentation'] = $module->field_project_documentation->url;
$row['field_project_screenshots'] = $module->field_project_screenshots->url;
$row['field_project_license'] = $module->field_project_license->url;
$row['num_project_images'] = count($module->field_project_images);
$row['field_supporting_organizations'] = count($module->field_supporting_organizations);
$row['num_project_docs'] = count($module->field_project_docs);
$row['field_project_issue_version_opts'] = '|'. implode('|', $module->field_project_issue_version_opts) . '|';
$row['field_security_advisory_coverage'] = $module->field_security_advisory_coverage;
// Ecosystems.
$ecosystems = [];
foreach($module->field_project_ecosystem as $system) {
$ecosystems[] = $system->id;
}
$row['field_project_ecosystem'] = '|' . implode('|', $ecosystems) . '|';
$row['field_next_major_version_info'] = $module->field_next_major_version_info->value;
$row['replaced'] = (bool)(count($module->field_replaced_by) > 0);
$row['field_issue_summary_template'] = !empty($module->field_issue_summary_template->value);
$row['stars'] = is_array($module->flag_project_star_user) ? count($module->flag_project_star_user) : 0;
$row['nid'] = $module->nid;
$row['title'] = $module->title;
$row['url'] = $module->url;
$row['total_usage'] = 0;
if (property_exists($module, 'project_usage')) {
foreach($module->project_usage as $branch => $usage) {
$row['total_usage'] += (int)$usage;
}
}
fputcsv($output_handle, $row);
}
sleep(2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment