plugin.video.kminus/urlfetch.py
def random_useragent(filename=None, *filenames):
    '''Returns a User-Agent string randomly from file.
    
    >>> ua = random_useragent('file1')
    >>> ua = random_useragent('file1', 'file2')
    >>> ua = random_useragent(['file1', 'file2'])
    >>> ua = random_useragent(['file1', 'file2'], 'file3')


    :param filename: path to the file from which a random useragent
                     is generated
    :type filename: string, optional
    '''
    import random
    from time import time
    
    filenames = list(filenames)

    if filename is None:
        filenames.extend([
            os.path.join(os.path.abspath(os.path.dirname(__file__)),
                         'urlfetch.useragents.list'),
            os.path.join(sys.prefix, 'share', 'urlfetch',
                         'urlfetch.useragents.list'),
        ])
    else:
        filenames.append(filename)
        
    filenames = set(_flatten(filenames))
        
    for filename in filenames:
        st = os.stat(filename)
        if stat.S_ISREG(st.st_mode) and os.access(filename, os.R_OK):
            break
    else:
        return 'urlfetch/%s' % __version__
        
    with open(filename, 'rb') as f:
        filesize = st.st_size
        r = random.Random(time())
        pos = 0
    
        # try getting a valid line for no more than 64 times
        for i in range(64):
            
            pos += r.randint(0, filesize)
            pos %= filesize
            f.seek(pos)
    
            # in case we are in middle of a line
            f.readline()
    
            line = f.readline()
            if not line:
                if f.tell() == filesize:
                    # end of file
                    f.seek(0)
                    line = f.readline()
    
            line = line.strip()
            if line and line[0] != '#':
                return line
            
    return 'urlfetch/%s' % __version__
Returns a User-Agent string randomly from file.
    
    >>> ua = random_useragent('file1')
    >>> ua = random_useragent('file1', 'file2')
    >>> ua = random_useragent(['file1', 'file2'])
    >>> ua = random_useragent(['file1', 'file2'], 'file3')


    :param filename: path to the file from which a random useragent
                     is generated
    :type filename: string, optional
    
Returns a User-Agent string randomly from file.
Returns a User - Agent string randomly from file .
def random_useragent(filename=None, *filenames):
    import random
    from time import time
    filenames = list(filenames)
    if filename is None:
        filenames.extend([
            os.path.join(os.path.abspath(os.path.dirname(__file__)),
                         'urlfetch.useragents.list'),
            os.path.join(sys.prefix, 'share', 'urlfetch',
                         'urlfetch.useragents.list'),
        ])
    else:
        filenames.append(filename)
    filenames = set(_flatten(filenames))
    for filename in filenames:
        st = os.stat(filename)
        if stat.S_ISREG(st.st_mode) and os.access(filename, os.R_OK):
            break
    else:
        return 'urlfetch/%s' % __version__
    with open(filename, 'rb') as f:
        filesize = st.st_size
        r = random.Random(time())
        pos = 0
        for i in range(64):
            pos += r.randint(0, filesize)
            pos %= filesize
            f.seek(pos)
            f.readline()
            line = f.readline()
            if not line:
                if f.tell() == filesize:
                    f.seek(0)
                    line = f.readline()
            line = line.strip()
            if line and line[0] != '#':
                return line
    return 'urlfetch/%s' % __version__
def random_useragent ( filename = None , * filenames ) : import random from time import time filenames = list ( filenames ) if filename is None : filenames . extend ( [ os . path . join ( os . path . abspath ( os . path . dirname ( __file__ ) ) , 'urlfetch.useragents.list' ) , os . path . join ( sys . prefix , 'share' , 'urlfetch' , 'urlfetch.useragents.list' ) , ] ) else : filenames . append ( filename ) filenames = set ( _flatten ( filenames ) ) for filename in filenames : st = os . stat ( filename ) if stat . S_ISREG ( st . st_mode ) and os . access ( filename , os . R_OK ) : break else : return 'urlfetch/%s' % __version__ with open ( filename , 'rb' ) as f : filesize = st . st_size r = random . Random ( time ( ) ) pos = 0 for i in range ( 64 ) : pos += r . randint ( 0 , filesize ) pos %= filesize f . seek ( pos ) f . readline ( ) line = f . readline ( ) if not line : if f . tell ( ) == filesize : f . seek ( 0 ) line = f . readline ( ) line = line . strip ( ) if line and line [ 0 ] != '#' : return line return 'urlfetch/%s' % __version__
Returns a User-Agent string randomly from file.
Returns a User - Agent string randomly from file .
'''Returns a User-Agent string randomly from file.
    
    >>> ua = random_useragent('file1')
    >>> ua = random_useragent('file1', 'file2')
    >>> ua = random_useragent(['file1', 'file2'])
    >>> ua = random_useragent(['file1', 'file2'], 'file3')


    :param filename: path to the file from which a random useragent
                     is generated
    :type filename: string, optional
    ''' # try getting a valid line for no more than 64 times # in case we are in middle of a line # end of file
filename
path to the file from which a random useragent
is generated
path to the file from which a random useragent is generated