-
Notifications
You must be signed in to change notification settings - Fork 62
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 command line tool with basic options for parsing torrents #10
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env python | ||
import argparse | ||
import PTN | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('torrent_name', help='Torrent name to parse') | ||
parser.add_argument('-j', '--json', dest="json", action='store_true', default=False, help='Output in json format') | ||
parser.add_argument('-y', '--yaml', dest="yaml", action='store_true', default=False, help='Output to YAML') | ||
parser.add_argument('-f', '--file', dest="filename", default=False, help='Output to file as well as command line') | ||
args = parser.parse_args() | ||
|
||
info = PTN.parse(args.torrent_name) | ||
if args.yaml: | ||
try: | ||
from ruamel import yaml | ||
output = str(yaml.dump(info, default_flow_style=False)) | ||
except ImportError: | ||
print "yaml module required for yaml output!\nInstall with 'pip install ruamel.yaml'" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. print statement is not compatible with python3 use print() function |
||
exit(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use sys.exit |
||
elif args.json: | ||
try: | ||
try: | ||
import simplejson as json | ||
except ImportError: | ||
import json | ||
output = json.dumps(info, indent=4, sort_keys=True) | ||
except ImportError: | ||
print "json module required for json output!\nInstall with 'pip install simplejson' or update python to 2.6+" | ||
exit(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use sys.exit |
||
else: | ||
output = str(info) | ||
if args.filename: | ||
with open(args.filename, 'w') as f: | ||
f.write(output) | ||
print output |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First line for script should be the shebang line.
I.e. #!/usr/bin/python
Then the script can be executable.