sportsref/nfl/finders/GPF.py
def inputs_options_defaults():
    """Handles scraping options for play finder form.

    :returns: {'name1': {'value': val, 'options': [opt1, ...] }, ... }

    """
    # set time variables
    if os.path.isfile(GPF_CONSTANTS_FILENAME):
        modtime = int(os.path.getmtime(GPF_CONSTANTS_FILENAME))
        curtime = int(time.time())
    # if file found and it's been <= a week
    if (os.path.isfile(GPF_CONSTANTS_FILENAME)
            and curtime - modtime <= 7 * 24 * 60 * 60):

        # just read the dict from the cached file
        with open(GPF_CONSTANTS_FILENAME, 'r') as const_f:
            def_dict = json.load(const_f)

    # otherwise, we must regenerate the dict and rewrite it
    else:

        print('Regenerating GPFConstants file')

        html = utils.get_html(GPF_URL)
        doc = pq(html)

        def_dict = {}
        # start with input elements
        for inp in doc('form#play_finder input[name]'):
            name = inp.attrib['name']
            # add blank dict if not present
            if name not in def_dict:
                def_dict[name] = {
                    'value': set(),
                    'options': set(),
                    'type': inp.type
                }

            val = inp.attrib.get('value', '')
            # handle checkboxes and radio buttons
            if inp.type in ('checkbox', 'radio'):
                # deal with default value
                if 'checked' in inp.attrib:
                    def_dict[name]['value'].add(val)
                # add to options
                def_dict[name]['options'].add(val)
            # handle other types of inputs (only other type is hidden?)
            else:
                def_dict[name]['value'].add(val)

        # for dropdowns (select elements)
        for sel in doc.items('form#play_finder select[name]'):
            name = sel.attr['name']
            # add blank dict if not present
            if name not in def_dict:
                def_dict[name] = {
                    'value': set(),
                    'options': set(),
                    'type': 'select'
                }

            # deal with default value
            defaultOpt = sel('option[selected]')
            if len(defaultOpt):
                defaultOpt = defaultOpt[0]
                def_dict[name]['value'].add(defaultOpt.attrib.get('value', ''))
            else:
                def_dict[name]['value'].add(
                    sel('option')[0].attrib.get('value', '')
                )

            # deal with options
            def_dict[name]['options'] = {
                opt.attrib['value'] for opt in sel('option')
                if opt.attrib.get('value')
            }

        # ignore QB kneels by default
        def_dict['include_kneels']['value'] = ['0']

        def_dict.pop('request', None)
        def_dict.pop('use_favorites', None)

        with open(GPF_CONSTANTS_FILENAME, 'w+') as f:
            for k in def_dict:
                try:
                    def_dict[k]['value'] = sorted(
                        list(def_dict[k]['value']), key=int
                    )
                    def_dict[k]['options'] = sorted(
                        list(def_dict[k]['options']), key=int
                    )
                except:
                    def_dict[k]['value'] = sorted(list(def_dict[k]['value']))
                    def_dict[k]['options'] = sorted(
                        list(def_dict[k]['options'])
                    )
            json.dump(def_dict, f)

    return def_dict
def inputs_options_defaults():
    """Handles scraping options for play finder form.

    :returns: {'name1': {'value': val, 'options': [opt1, ...] }, ... }

    """
    # set time variables
    if os.path.isfile(GPF_CONSTANTS_FILENAME):
        modtime = int(os.path.getmtime(GPF_CONSTANTS_FILENAME))
        curtime = int(time.time())
    # if file found and it's been <= a week
    if (os.path.isfile(GPF_CONSTANTS_FILENAME)
            and curtime - modtime <= 7 * 24 * 60 * 60):

        # just read the dict from the cached file
        with open(GPF_CONSTANTS_FILENAME, 'r') as const_f:
            def_dict = json.load(const_f)

    # otherwise, we must regenerate the dict and rewrite it
    else:

        print('Regenerating GPFConstants file')

        html = utils.get_html(GPF_URL)
        doc = pq(html)

        def_dict = {}
        # start with input elements
        for inp in doc('form#play_finder input[name]'):
            name = inp.attrib['name']
            # add blank dict if not present
            if name not in def_dict:
                def_dict[name] = {
                    'value': set(),
                    'options': set(),
                    'type': inp.type
                }

            val = inp.attrib.get('value', '')
            # handle checkboxes and radio buttons
            if inp.type in ('checkbox', 'radio'):
                # deal with default value
                if 'checked' in inp.attrib:
                    def_dict[name]['value'].add(val)
                # add to options
                def_dict[name]['options'].add(val)
            # handle other types of inputs (only other type is hidden?)
            else:
                def_dict[name]['value'].add(val)

        # for dropdowns (select elements)
        for sel in doc.items('form#play_finder select[name]'):
            name = sel.attr['name']
            # add blank dict if not present
            if name not in def_dict:
                def_dict[name] = {
                    'value': set(),
                    'options': set(),
                    'type': 'select'
                }

            # deal with default value
            defaultOpt = sel('option[selected]')
            if len(defaultOpt):
                defaultOpt = defaultOpt[0]
                def_dict[name]['value'].add(defaultOpt.attrib.get('value', ''))
            else:
                def_dict[name]['value'].add(
                    sel('option')[0].attrib.get('value', '')
                )

            # deal with options
            def_dict[name]['options'] = {
                opt.attrib['value'] for opt in sel('option')
                if opt.attrib.get('value')
            }

        # ignore QB kneels by default
        def_dict['include_kneels']['value'] = ['0']

        def_dict.pop('request', None)
        def_dict.pop('use_favorites', None)

        with open(GPF_CONSTANTS_FILENAME, 'w+') as f:
            for k in def_dict:
                try:
                    def_dict[k]['value'] = sorted(
                        list(def_dict[k]['value']), key=int
                    )
                    def_dict[k]['options'] = sorted(
                        list(def_dict[k]['options']), key=int
                    )
                except:
                    def_dict[k]['value'] = sorted(list(def_dict[k]['value']))
                    def_dict[k]['options'] = sorted(
                        list(def_dict[k]['options'])
                    )
            json.dump(def_dict, f)

    return def_dict
def inputs_options_defaults ( ) : # set time variables if os . path . isfile ( GPF_CONSTANTS_FILENAME ) : modtime = int ( os . path . getmtime ( GPF_CONSTANTS_FILENAME ) ) curtime = int ( time . time ( ) ) # if file found and it's been <= a week if ( os . path . isfile ( GPF_CONSTANTS_FILENAME ) and curtime - modtime <= 7 * 24 * 60 * 60 ) : # just read the dict from the cached file with open ( GPF_CONSTANTS_FILENAME , 'r' ) as const_f : def_dict = json . load ( const_f ) # otherwise, we must regenerate the dict and rewrite it else : print ( 'Regenerating GPFConstants file' ) html = utils . get_html ( GPF_URL ) doc = pq ( html ) def_dict = { } # start with input elements for inp in doc ( 'form#play_finder input[name]' ) : name = inp . attrib [ 'name' ] # add blank dict if not present if name not in def_dict : def_dict [ name ] = { 'value' : set ( ) , 'options' : set ( ) , 'type' : inp . type } val = inp . attrib . get ( 'value' , '' ) # handle checkboxes and radio buttons if inp . type in ( 'checkbox' , 'radio' ) : # deal with default value if 'checked' in inp . attrib : def_dict [ name ] [ 'value' ] . add ( val ) # add to options def_dict [ name ] [ 'options' ] . add ( val ) # handle other types of inputs (only other type is hidden?) else : def_dict [ name ] [ 'value' ] . add ( val ) # for dropdowns (select elements) for sel in doc . items ( 'form#play_finder select[name]' ) : name = sel . attr [ 'name' ] # add blank dict if not present if name not in def_dict : def_dict [ name ] = { 'value' : set ( ) , 'options' : set ( ) , 'type' : 'select' } # deal with default value defaultOpt = sel ( 'option[selected]' ) if len ( defaultOpt ) : defaultOpt = defaultOpt [ 0 ] def_dict [ name ] [ 'value' ] . add ( defaultOpt . attrib . get ( 'value' , '' ) ) else : def_dict [ name ] [ 'value' ] . add ( sel ( 'option' ) [ 0 ] . attrib . get ( 'value' , '' ) ) # deal with options def_dict [ name ] [ 'options' ] = { opt . attrib [ 'value' ] for opt in sel ( 'option' ) if opt . attrib . get ( 'value' ) } # ignore QB kneels by default def_dict [ 'include_kneels' ] [ 'value' ] = [ '0' ] def_dict . pop ( 'request' , None ) def_dict . pop ( 'use_favorites' , None ) with open ( GPF_CONSTANTS_FILENAME , 'w+' ) as f : for k in def_dict : try : def_dict [ k ] [ 'value' ] = sorted ( list ( def_dict [ k ] [ 'value' ] ) , key = int ) def_dict [ k ] [ 'options' ] = sorted ( list ( def_dict [ k ] [ 'options' ] ) , key = int ) except : def_dict [ k ] [ 'value' ] = sorted ( list ( def_dict [ k ] [ 'value' ] ) ) def_dict [ k ] [ 'options' ] = sorted ( list ( def_dict [ k ] [ 'options' ] ) ) json . dump ( def_dict , f ) return def_dict
Handles scraping options for play finder form.

    :returns: {'name1': {'value': val, 'options': [opt1, ...] }, ... }
Handles scraping options for play finder form .