Skip to content

Commit

Permalink
[feat] only list mmc json input when using -N, support _find post, li…
Browse files Browse the repository at this point in the history
…mit/skip
  • Loading branch information
fangq committed Feb 11, 2025
1 parent 6c76b35 commit 9a9a650
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/mmc_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3286,28 +3286,47 @@ void mcx_parsecmd(int argc, char* argv[], mcconfig* cfg) {
break;

case 'N':
if (i == argc - 1 || argv[i + 1][0] == '-') {
if (i == argc - 1 || argv[i + 1][0] == '-' || argv[i + 1][0] == '{' || strchr(argv[i + 1], '=')) {
int j, doclen;
char* jbuf = NULL;
runcommand("curl -s -X GET \"https://neurojson.io:7777/mmc/_all_docs\"", "", &jbuf);
cJSON* root = cJSON_Parse(jbuf), *docs = cJSON_GetObjectItem(root, "rows"), *subitem, *tmp;

if (i < argc - 1 && argv[i + 1][0] == '{') {
char cmd[MAX_PATH_LENGTH] = "curl -s -X POST -H 'Content-Type: application/json' -d ";
sprintf(cmd + strlen(cmd), "'%s' \"https://neurojson.io:7777/mmc/_find\"", argv[i + 1]);
runcommand(cmd, "", &jbuf);
} else if (i < argc - 1 && strchr(argv[i + 1], '=')) {
char cmd[MAX_PATH_LENGTH] = "curl -s -X GET 'https://neurojson.io:7777/mmc/_all_docs?";
sprintf(cmd + strlen(cmd), "%s'", argv[i + 1]);
runcommand(cmd, "", &jbuf);
} else {
runcommand("curl -s -X POST -H 'Content-Type: application/json' -d '{\"selector\": {\"Session\": {\"$gt\": null}},\"fields\": [\"_id\"],\"limit\":50}' \"https://neurojson.io:7777/mmc/_find\"", "", &jbuf);
}

cJSON* root = cJSON_Parse(jbuf), *docs = cJSON_GetObjectItem(root, "docs"), *subitem, *tmp;

if (!docs) {
MMC_ERROR(-1, jbuf);
docs = cJSON_GetObjectItem(root, "rows");

if (!docs || !root) {
MMC_ERROR(-1, jbuf);
}
}

doclen = cJSON_GetArraySize(docs);
subitem = docs->child;
printf("Downloading %d simulations from NeuroJSON.io (https://neurojson.org/db/mmc)\n", doclen - 1);

for (j = 0; j < doclen; j++) {
if (strchr(FIND_JSON_KEY("id", "id", subitem, "", valuestring), '_') == NULL) {
printf("\t%s\n", FIND_JSON_KEY("id", "id", subitem, "", valuestring));
char* docid = (cJSON_GetObjectItem(subitem, "_id") ? FIND_JSON_KEY("_id", "id", subitem, "", valuestring) : FIND_JSON_KEY("id", "id", subitem, "", valuestring));

if (docid && docid[0] != '_' && docid[0] != '.') {
printf("\t%s\n", docid);
}

subitem = subitem->next;
}

cJSON_Delete(root);
free(jbuf);
exit(0);
} else {
Expand Down

0 comments on commit 9a9a650

Please sign in to comment.