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

Create CLI tool #32

Open
turicas opened this issue Mar 12, 2014 · 0 comments
Open

Create CLI tool #32

turicas opened this issue Mar 12, 2014 · 0 comments

Comments

@turicas
Copy link
Contributor

turicas commented Mar 12, 2014

I'd be nice to interact with the API using the command line (without needing to use curl or wget directly).
I've started a little script that can evolve to something we can ship with the library:

#!/usr/bin/env python
# coding: utf-8

import argparse
import sys

from pypln.api import PyPLN, Document


API_BASE = 'http://demo.pypln.org/'

def print_document(document):
    print('Filename: {}\n  {} bytes\n  URL: {}\n  Properties: {}'
            .format(document.blob, document.size, document.url,
                    document.properties_url))

def main():
    args = argparse.ArgumentParser()
    args.add_argument('username')
    args.add_argument('password')
    args.add_argument('--list-corpora', action='store_true')
    args.add_argument('--list-documents', action='store_true')
    args.add_argument('--list-documents-from-corpus', type=unicode)
    argv = args.parse_args()

    username = argv.username
    password = argv.password
    credentials = (username, password)

    pypln = PyPLN(API_BASE, credentials)
    if argv.list_corpora:
        for corpus in pypln.corpora():
            print('Corpus name: {}, {} documents'
                    .format(corpus.name, len(corpus.documents)))
    elif argv.list_documents:
        for document in pypln.documents():
            print_document(document)
    elif argv.list_documents_from_corpus:
        corpus_name = argv.list_documents_from_corpus
        found_corpora = [corpus for corpus in pypln.corpora()
                         if corpus.name == corpus_name]
        if not len(found_corpora):
            sys.stderr.write('ERROR: corpus "{}" not found.\n'
                    .format(corpus_name))
            exit(2)
        else:
            corpus = found_corpora[0]
            print('Retrieving documents from corpus "{}" ({} found)...\n'
                    .format(corpus_name, len(corpus.documents)))
            for document_url in corpus.documents:
                document = Document.from_url(document_url, credentials)
                print_document(document)
    else:
        sys.stderr.write('ERROR: you should choose one option.\n')
        exit(1)


if __name__ == '__main__':
    main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant