Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to print raw json #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Simple command line tool for fetching user anime data from MyAnimeList.
-p, --plantowatch Fetch a user's plan to watch anime
-w, --watching Fetch a user's currently watching anime
-s, --sfw Fetch only SFW anime
-j, --json Format results as JSON
-?, --help Give this help list
--usage Give a short usage message
-V, --version Print program version
Expand Down
14 changes: 12 additions & 2 deletions src/mya.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define OPT_PLANTOWATCH 'p'
#define OPT_ALL 'a'
#define OPT_SFW 's'
#define OPT_JSON 'j'

/* constants for ANSI color codes */
#define ANSI_CODE_CYAN "\033" "[0;36m"
Expand All @@ -41,13 +42,15 @@ static struct argp_option options[] = {
{ "plantowatch", OPT_PLANTOWATCH, 0, 0, "Fetch a user's plan to watch anime" },
{ "all", OPT_ALL, 0, 0, "Fetch all anime for a user" },
{ "sfw", OPT_SFW, 0, 0, "Fetch only SFW anime" },
{"json", OPT_JSON, 0, 0, "Format results as JSON" },
{ 0 },
};

/* struct to keep track of selected options and arguments */
struct arguments {
enum { WATCHING_MODE, COMPLETED_MODE, HOLD_MODE, DROPPED_MODE, PLAN_MODE, ALL_MODE } mode;
int nsfw;
int json;
char *args[1];
};

Expand Down Expand Up @@ -126,6 +129,7 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state) {
case OPT_PLANTOWATCH: arguments->mode = PLAN_MODE; break;
case OPT_ALL: arguments->mode = ALL_MODE; break;
case OPT_SFW: arguments->nsfw = 0; break;
case OPT_JSON: arguments->json = 1; break;

/* parse arguments */
case ARGP_KEY_ARG:
Expand Down Expand Up @@ -463,6 +467,7 @@ int main (int argc, char *argv[]) {
struct arguments arguments;
arguments.mode = WATCHING_MODE;
arguments.nsfw = 1;
arguments.json = 0;
argp_parse(&argp, argc, argv, 0, 0, &arguments);

/* setup uri to fetch based on arguments */
Expand Down Expand Up @@ -497,8 +502,13 @@ int main (int argc, char *argv[]) {
get_new_uri(uri, uri_size, json);

/* print out the anime list */
print_anime_list(anime_list, page_num, endpoint, arguments.mode);

if (arguments.json == 0) {
print_anime_list(anime_list, page_num, endpoint, arguments.mode);
/* print out the anime json */
} else {
const char *serialized_json = json_object_to_json_string(anime_list);
printf("%s\n", serialized_json);
}
/* cleanup json */
json_object_put(json);

Expand Down
Loading