# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
from __future__ import absolute_import
import sys
import json
import re

from svtplay_dl.service import Service, OpenGraphThumbMixin
from svtplay_dl.utils import get_http_data
from svtplay_dl.fetcher.http import download_http
from svtplay_dl.log import log

class Vimeo(Service, OpenGraphThumbMixin):
    supported_domains = ['vimeo.com']

    def get(self, options):
        match = re.search("data-config-url=\"(.*)\" data-fallback-url", self.get_urldata())
        if not match:
            log.error("Can't find data")
            sys.exit(4)
        player_url = match.group(1).replace("&amp;", "&")
        player_data = get_http_data(player_url)

        if player_data:
            jsondata = json.loads(player_data)
            avail_quality = jsondata["request"]["files"]["h264"]
            if options.quality:
                try:
                    selected = avail_quality[options.quality]
                except KeyError:
                    log.error("Can't find that quality. (Try one of: %s)",
                              ", ".join([str(elm) for elm in avail_quality]))
                    sys.exit(4)
            else:
                try:
                    selected = self.select_highest_quality(avail_quality)
                except KeyError:
                    log.error("Can't find any streams.")
                    sys.exit(4)
            url = selected['url']
            download_http(options, url)
        else:
            log.error("Can't find any streams.")
            sys.exit(2)

    def select_highest_quality(self, available):
        if 'hd' in available:
            return available['hd']
        elif 'sd' in available:
            return available['sd']
        else:
            raise KeyError()

@@ -294,29 +294,20 @@
  import 
-download_http
+HTTP
 %0Afrom sv
@@ -879,946 +879,210 @@
     
-if options.quality:%0A                try:%0A                    selected = avail_quality%5Boptions.quality%5D%0A                except KeyError:%0A                    log.error(%22Can't find that quality. (Try one of: %25s)%22,%0A                              %22, %22.join(%5Bstr(elm) for elm in avail_quality%5D))%0A                    sys.exit(4)%0A            else:%0A                try:%0A                    selected = self.select_highest_quality(avail_quality)%0A                except KeyError:%0A                    log.error(%22Can't find any streams.%22)%0A                    sys.exit(4)%0A            url = selected%5B'url'%5D%0A            download_http(options, url)%0A        else:%0A            log.error(%22Can't find any streams.%22)%0A            sys.exit(2)%0A%0A    def select_highest_quality(self, available):%0A        if 'hd' in available:%0A            return available%5B'hd'%5D%0A        elif 'sd' in available:%0A            return available%5B'sd'%5D%0A        else:%0A            raise KeyError()%0A
+for i in avail_quality.keys():%0A                yield HTTP(options, avail_quality%5Bi%5D%5B%22url%22%5D, avail_quality%5Bi%5D%5B%22height%22%5D)%0A        else:%0A            log.error(%22Can't find any streams.%22)%0A            sys.exit(2)

convert into new video fetcher