

def rushdifficulty(keyword):
    pp = 50
    try:
        apikey = globs.config['semrush']
    except:
        return 'In the Config tab, put semrush under name and the api key under value to proceed.'
    funcol = (globs.row1.index('rushdifficulty') + 1)
    lastq = len(globs.sheet.col_values(funcol))
    mycol = globs.letter[funcol]
    if ('keyword' in globs.row1):
        kwcol = globs.letter[(globs.row1.index('keyword') + 1)]
        chunks = [('%s%s:%s%s' % (kwcol, chunk, kwcol, ((chunk + pp) - 1))) for chunk in range(lastq, globs.numrows, pp)]
    else:
        return 'You must have a column named keyword.'
    if (not chunks):
        return 'Please use difficulty function instead.'
    lastchunk = chunks[(- 1)].split(':')
    lastchunk[1] = ('%s%s' % (kwcol, globs.numrows))
    chunks[(- 1)] = ('%s:%s' % (lastchunk[0], lastchunk[1]))
    for chunk in chunks:
        out(('Chunk: %s' % chunk))
        CellList1 = globs.sheet.range(chunk)
        try:
            CellList2 = globs.sheet.range(chunk.replace(kwcol, mycol))
        except:
            CellList2 = globs.sheet.range(chunk.replace(kwcol, mycol))
        kwlist = []
        for (cindex, acell) in enumerate(CellList1):
            asciival = acell.value.encode('ascii', errors='ignore')
            kwlist.append(asciival)
        kwstring = ';'.join(kwlist)
        endpoint = 'http://api.semrush.com/'
        params = {
            'type': 'phrase_kdi',
            'export_columns': 'Ph,Kd',
            'database': 'us',
            'key': apikey,
            'phrase': kwstring,
        }
        out('Hitting SEMRush API...')
        try:
            respobj = requests.get(endpoint, params=params, timeout=5)
        except:
            Stop()
        resplist = respobj.text.splitlines()
        scoredict = {
            
        }
        apair = []
        for scorepair in resplist:
            apair = scorepair.split(';')
            if (len(apair) > 1):
                scoredict[str(apair[0])] = str(apair[1])
        for (cindex, acell) in enumerate(CellList1):
            acell = acell.value.lower()
            if (acell in scoredict):
                score = scoredict[acell]
                if (not score):
                    score = '-1'
                CellList2[cindex].value = score
            else:
                CellList2[cindex].value = '-1'
        globs.sheet.update_cells(CellList2)
        time.sleep(5)
    globs.STOP = True

dataset/ETHPy150Open miklevin/pipulate/common.py rushdifficulty/original