davidchambers / tutor

JavaScript interface for the Gatherer card database
https://gatherer.wizards.com/
Do What The F*ck You Want To Public License
149 stars 18 forks source link

CLI should reflect the full Javascript API #62

Closed ire-and-curses closed 11 years ago

ire-and-curses commented 11 years ago

The command line interface to the API is a convenient way to access common functions (individual card retrieval, set retrieval). For non-Javascript developers, it also provides a way to cleanly access the API. It would be great if the full API (formats, types, sets) was supported. I personally would make use of at least the sets function if it were available to me. Also, if I add searching by type to my project (I may do), then the types function would be very useful.

robdennis commented 11 years ago

I was about to take a look at this myself since I too could use the {{sets}} function, but I see David created a branch for it already, presumably to take it on: https://github.com/davidchambers/tutor/tree/cli-feature-parity

davidchambers commented 11 years ago

I just turned this issue into a pull request. Feel free to pull from this branch and give the new functionality a spin. I'd appreciate a code review before I pull this change into master. At the very least, I'll take another look myself one day soon.

robdennis commented 11 years ago

One of the cli tests failed one time, then passed:

 $ tutor types prints types:
 Error: timeout of 10000ms exceeded
  at Object.<anonymous> (/Users/rad3/development/tutor/node_modules/mocha/lib/runnable.js:167:14)
  at Timer.list.ontimeout (timers.js:101:19)

not really about this change, but I think this help text for "set" is out of date: set [options] output one page of cards from the named set

since it's actually the entire set now

other than that, looks good to me.

robdennis commented 11 years ago

as an example of how easy this makes things for external consumers, I will now download all of gatherer to an 8.1 MB file using python 2.7 (maybe this is a decent README example?):

import os
import itertools
import logging
import json
import subprocess

logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger()

TUTOR_DIR = os.path.realpath(os.path.dirname(__file__))
TUTOR_PATH = os.path.join(TUTOR_DIR, 'tutor')

def _query_tutor(command_parameters):
    """

    :param command_parameters:
    :return: the json-parsed output of the tutor command
    """

    command = [TUTOR_PATH, '--format', 'json'] + command_parameters

    try:
        return json.loads(subprocess.check_output(command).strip())
    except (subprocess.CalledProcessError, ValueError) as e:
        raise subprocess.CalledProcessError(u'problem with command',
                                            u' '.join(command))

def _query_tutor_for_cards_by_set(set_name):
    """
    Depends on: installation of the tutor CLI, that path in settings,
    the path to node in your $PATH environment variable

    :param set_name: the set's name, which will be passed to
        tutor's command line
    :return: a list of cards represented as dictionaries
    """

    log.debug('searching for set name: %r', set_name)
    return _query_tutor(['set', set_name])

def _query_tutor_for_all_sets():
    log.debug('searching for sets')
    return _query_tutor(['sets'])

def write_all_cards_from_gatherer_to_file():
    all_sets = _query_tutor_for_all_sets()
    cards = {
        card['name']: card
        for card in itertools.chain(*[_query_tutor_for_cards_by_set(set_name)
                                      for set_name in all_sets])
    }

    json.dump(cards, open('cards.json', 'wb'))

if __name__ == '__main__':

    write_all_cards_from_gatherer_to_file()
davidchambers commented 11 years ago

Thanks for taking a look at this pull request, @rdennis463. :)

not really about this change, but I think this help text for "set" is out of date: set [options] output one page of cards from the named set

since it's actually the entire set now

Good catch. I'll update this description.

The Python code above is very cool. I think the wiki (which I've just enabled) is the best place for it. Go ahead and create a page for it. It might be a good idea to link to it from the CLI section of the README, too.