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

[Feature request] parser-friendly stdout #24

Closed
innocenzi opened this issue Jul 6, 2021 · 11 comments
Closed

[Feature request] parser-friendly stdout #24

innocenzi opened this issue Jul 6, 2021 · 11 comments

Comments

@innocenzi
Copy link

Hey,

First - amazing work. I'm very impressed by the efficiency of animdl.
I have a request, which I would have PR'd myself if only I knew Python, but I believe it's reasonable enough to still ask.

I'm going to make a wrapper around animdl for my own usage, and I'd love to avoid having to parse the CLI output. I was thinking that, given a flag, something like --json, all the output could be just JSON.

Ideally, commands like animdl test, animdl grab "one piece" or animdl grab https://9anime.to/watch/one-piece.ov8 would only output JSON, without color or formatting (though that last command is pretty close).

To be clear, I can totally parse everything even now, I just think it'd be very nice to have a reliable way to do so.

@innocenzi
Copy link
Author

innocenzi commented Jul 6, 2021

Actually, ainsi seem to make the program crash when calling from symfony/process:

Traceback (most recent call last):
  File "D:\Programmes\Python\3-8-0\Scripts\animdl-script.py", line 11, in <module>
    load_entry_point('animdl==1.3.1', 'console_scripts', 'animdl')()
  File "d:\programmes\python\3-8-0\lib\site-packages\pkg_resources\__init__.py", line 489, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "d:\programmes\python\3-8-0\lib\site-packages\pkg_resources\__init__.py", line 2852, in load_entry_point
    return ep.load()
  File "d:\programmes\python\3-8-0\lib\site-packages\pkg_resources\__init__.py", line 2443, in load
    return self.resolve()
  File "d:\programmes\python\3-8-0\lib\site-packages\pkg_resources\__init__.py", line 2449, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "d:\programmes\python\3-8-0\lib\site-packages\animdl\__main__.py", line 11, in <module>
    print("\x1b[35m{}\x1b[39m".format(package_banner))
  File "d:\programmes\python\3-8-0\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 5-51: character maps to <undefined>

From what I gather, this line is responsible for crashing, and I think it's because of the color formatting?

I'm not sure how to find a workaround for that. Should I raise the issue in https://github.com/justfoolingaround/animdl-install?

EDIT - Found how to work around that, using python -X utf8 fixes the issue. I don't know how fixable it is on your end though.

@justfoolingaround
Copy link
Owner

I was thinking that, given a flag, something like --json, all the output could be just JSON.

Added by default. All the values shown in the grab stdout will now be in JSON format.. just separated by newlines on each episodes (You can easily guess why that is done).

To make things easier, I've added a --quiet option alongside all the commands and with this, banner will be silenced as well with all the "announcements" done by animdl.

This has been pushed to the unstable version (the non-packaged repo) atm and will be pushed to the packaged repo soon.

From what I gather, this line is responsible for crashing, and I think it's because of the color formatting?

I'm not sure how to find a workaround for that. Should I raise the issue in https://github.com/justfoolingaround/animdl-install?

EDIT - Found how to work around that, using python -X utf8 fixes the issue. I don't know how fixable it is on your end though.

Ah, that's great to know!

@innocenzi
Copy link
Author

That's great, that was fast!

Could I abuse and ask if it's possible to do the same for animdl grab "one piece" --quiet? Currently this one still outputs colors and non-json, as well as waiting for user input :/

I tested animdl grab https://9anime.to/watch/one-piece.ov8 --quiet though and this is perfect. 🙏

@justfoolingaround
Copy link
Owner

justfoolingaround commented Jul 7, 2021

So, I've added an auto flag with an index argument which will be in use if the flag is passed.

In case of One Piece, the auto would've just selected the dub version which isn't ideal for most use-cases; hence, using a index would make selecting easy from queries (1; or second item would be the sub one).

Keep in mind that the index is a programmer's index so 1 will be equivalent to the second item. Not sure if I even have to mention this xd.

@justfoolingaround
Copy link
Owner

Also, for the record, these all features are added to all the major commands so yeah, you can avoid the colored stuff and the query selector in those commands (stream/download) as well.

@innocenzi
Copy link
Author

Alright, that's good enough for me. Thanks a lot! I'll probably publish my wrapper and the app I'll make in a few weeks. animdl is very useful. :)

@innocenzi
Copy link
Author

Hey,

It's me again! I've discovered while working on it that the --auto option is not that convenient, since I want the user to be able to look for an anime.

I was going to just parse the output (and not use --auto), but without TTY the CLI returns the code 1. It means that I have to modify my abstraction to not throw, and while I could do that, I think it'd be better if the CLI could just understand --quiet and return a result like the following:

➜ python .\animdl.py grab "one piece" --quiet
{"name": "One Piece", "url": "https://9anime.to/watch/one-piece.ov8"}
{"name": "One Piece (Dub)", "url": "https://9anime.to/watch/one-piece-dub.34r"}
{"name": "One Piece Recap", "url": "https://9anime.to/watch/one-piece-recap.j8j8"}

This is more convenient because, from a query, I can actually get a list of matching animes and their URL, so the user can chose the one they want (same problematic you solved with index, but the user would have no way to input that index so we can't use it there).

Alternatively I could make the search with an external API (eg. AniList) but the names are not the exact sames so it's not reliable.

TL;DR: I'd like the user to be able to search for an anime, select one, and after use the corresponding link (ie. https://9anime.to/watch/one-piece.ov8) to get the episode list (python .\animdl.py grab https://9anime.to/watch/one-piece.ov8 --quiet works wonderfully though).

@innocenzi innocenzi reopened this Jul 7, 2021
@justfoolingaround
Copy link
Owner

justfoolingaround commented Jul 8, 2021

I think I'll add a search command for that when I get time. Giving json queries on quiet mode wouldn't be ideal. And of course, this search command will have its own --json/-j flag.

@innocenzi
Copy link
Author

Ah, that seems perfect. 👍

@justfoolingaround
Copy link
Owner

Added. I kinda separated the provider argument; you can get the output you desire by using

animdl.py search "one piece" --json --quiet

Don't worry about 2 calls making your wrapper or normal usage inefficient. The search done by other commands is ignored if the url matches hence, there won't ever be any unnecessary calls.

@innocenzi
Copy link
Author

You're so fast, this is exactly what I needed. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants