def downloadInternal(url,file,maxbitrate=0):
    global key
    global iv
    global USEDec
    dumpfile = None
    #dumpfile=open('c:\\temp\\myfile.mp4',"wb")
    variants = []
    variant = None
    for line in gen_m3u(url):
        if line.startswith('#EXT'):
            tag, attribs = parse_m3u_tag(line)
            if tag == '#EXT-X-STREAM-INF':
                variant = attribs
        elif variant:
            variants.append((line, variant))
            variant = None
    if len(variants) == 1:
        url = urlparse.urljoin(url, variants[0][0])
    elif len(variants) >= 2:
        print "More than one variant of the stream was provided."

        choice=-1
        lastbitrate=0
        print 'maxbitrate',maxbitrate
        for i, (vurl, vattrs) in enumerate(variants):
            print i, vurl,
            for attr in vattrs:
                key, value = attr.split('=')
                key = key.strip()
                value = value.strip().strip('"')
                if key == 'BANDWIDTH':
                    print 'bitrate %.2f kbps'%(int(value)/1024.0)
                    if int(value)<=int(maxbitrate) and int(value)>lastbitrate:
                        choice=i
                        lastbitrate=int(value)
                elif key == 'PROGRAM-ID':
                    print 'program %s'+value,
                elif key == 'CODECS':
                    print 'codec %s'%value,
                elif key == 'RESOLUTION':
                    print 'resolution %s'%value,
                else:
                    raise ValueError("unknown STREAM-INF attribute %s"%key)
            print
        if choice==-1: choice=0
        #choice = int(raw_input("Selection? "))
        print 'choose %d'%choice
        url = urlparse.urljoin(url, variants[choice][0])

    #queue = Queue.Queue(1024) # 1024 blocks of 4K each ~ 4MB buffer
    control = ['go']
    #thread = threading.Thread(target=player_pipe, args=(queue, control,file))
    #thread.start()
    last_seq = -1
    targetduration = 5
    changed = 0
    
    try:
        while 1==1:#thread.isAlive():
            medialist = list(handle_basic_m3u(url))
            playedSomething=False
            if None in medialist:
                # choose to start playback at the start, since this is a VOD stream
                pass
            else:
                # choose to start playback three files from the end, since this is a live stream
                medialist = medialist[-3:]
            #print medialist
            
            for media in medialist:
                if media is None:
                    #queue.put(None, block=True)
                    return
                seq, enc, duration, targetduration, media_url = media
                if seq > last_seq:
                    #print 'downloading.............',url
                    for chunk in download_chunks(urlparse.urljoin(url, media_url),enc=enc):
                        #print '1. chunk available %d'%len(chunk)
                        if enc: 
                             if not USEDec==3:
                                chunk = enc.decrypt(chunk)
                             else:
                                chunkb=array.array('B',chunk)
                                chunk = enc.decrypt(chunkb)
                                chunk="".join(map(chr, chunk))
                        #if enc: chunk = enc.decrypt(chunk,key,'CBC')
                        #print '2. chunk done %d'%len(chunk)
                        if dumpfile: dumpfile.write(chunk)
                        #queue.put(chunk, block=True)
                        send_back(chunk,file)
                        #print '3. chunk available %d'%len(chunk)
                    last_seq = seq
                    changed = 1
                    playedSomething=True
            
            '''if changed == 1:
                # initial minimum reload delay
                time.sleep(duration)
            elif changed == 0:
                # first attempt
                time.sleep(targetduration*0.5)
            elif changed == -1:
                # second attempt
                time.sleep(targetduration*1.5)
            else:
                # third attempt and beyond
                time.sleep(targetduration*3.0)
            
            changed -= 1
            '''
            if not playedSomething:
                xbmc.sleep(2000)
    except:
        control[0] = 'stop'
        raise
dataset/ETHPy150Open mrknow/filmkodi/plugin.video.specto/resources/lib/libraries/f4mproxy/hlsDownloader.py/downloadInternal/operator `%` -> `+`