# Path: yledl/ffprobe.py
# class Ffprobe:
#     def __init__(self, ffprobe_binary, ffmpeg_binary, x_forwarded_for):
#         self.ffprobe_binary = ffprobe_binary
#         self.ffmpeg_binary = ffmpeg_binary
#         self.x_forwarded_for = x_forwarded_for
# 
#     def show_programs_for_url(self, url):
#         args = [
#             self.ffprobe_binary,
#             '-loglevel', ffmpeg_loglevel(logger.getEffectiveLevel()),
#             '-headers', f'X-Forwarded-For: {self.x_forwarded_for}\r\n',
#             '-show_programs',
#             '-print_format', 'json=c=1',
#             '-probesize', '80000000',
#             '-i', url,
#         ]
#         try:
#             return json.loads(subprocess.check_output(args).decode('utf-8'))
#         except subprocess.CalledProcessError as ex:
#             raise ValueError(
#                 f'Stream probing failed with status {ex.returncode}')
# 
#     def duration_seconds_file(self, filename):
#         args = [
#             self.ffmpeg_binary,
#             '-stats',
#             '-loglevel', 'fatal',
#             '-i', f'file:{filename}',
#             '-f', 'null',
#             '-',
#         ]
# 
#         try:
#             decoding_result = (
#                 subprocess.check_output(args, stderr=subprocess.STDOUT)
#                 .decode('utf-8')
#                 .rsplit('\r', 1)[-1])
#         except subprocess.CalledProcessError as ex:
#             raise ValueError(
#                 f'Stream probing failed with status {ex.returncode}')
#         except UnicodeDecodeError:
#             raise ValueError('Unexpected encoding on stream probing response')
# 
#         m = re.search(r'time=(\d\d):(\d\d):(\d\d)\.(\d\d) ', decoding_result)
#         if not m:
#             raise ValueError('Failed to parse duration in the ffmpeg output')
# 
#         return (float(m.group(1)) * 60 * 60 + float(m.group(2)) * 60 +
#                 float(m.group(3)) + float(m.group(4)) / 100)
# 
# Path: yledl/utils.py
# def sane_filename(name, excludechars):
#     tr = {ord(c): ord('_') for c in excludechars}
#     x = re.sub(r'\s+', ' ', name, flags=re.UNICODE).strip(' .').translate(tr)
#     return x or 'ylevideo'


import attr
import ipaddress
import logging
import os.path
import random
import re
import subprocess
from .ffprobe import Ffprobe
from .utils import sane_filename

logger = logging.getLogger('yledl')


def convert_download_limits(arg):
    return arg or DownloadLimits()


def ffmpeg_default(arg):
    return arg or 'ffmpeg'


def ffprobe_default(arg):
    return arg or 'ffprobe'


def wget_default(arg):
    return arg or 'wget'


def random_elisa_ipv4():
    elisa_ipv4_range = list(ipaddress.ip_network('91.152.0.0/13').hosts())
    return str(random.choice(elisa_ipv4_range))


@attr.s

# Path: yledl/ffprobe.py
# class Ffprobe:
#     def __init__(self, ffprobe_binary, ffmpeg_binary, x_forwarded_for):
#         self.ffprobe_binary = ffprobe_binary
#         self.ffmpeg_binary = ffmpeg_binary
#         self.x_forwarded_for = x_forwarded_for
# 
#     def show_programs_for_url(self, url):
#         args = [
#             self.ffprobe_binary,
#             '-loglevel', ffmpeg_loglevel(logger.getEffectiveLevel()),
#             '-headers', f'X-Forwarded-For: {self.x_forwarded_for}\r\n',
#             '-show_programs',
#             '-print_format', 'json=c=1',
#             '-probesize', '80000000',
#             '-i', url,
#         ]
#         try:
#             return json.loads(subprocess.check_output(args).decode('utf-8'))
#         except subprocess.CalledProcessError as ex:
#             raise ValueError(
#                 f'Stream probing failed with status {ex.returncode}')
# 
#     def duration_seconds_file(self, filename):
#         args = [
#             self.ffmpeg_binary,
#             '-stats',
#             '-loglevel', 'fatal',
#             '-i', f'file:{filename}',
#             '-f', 'null',
#             '-',
#         ]
# 
#         try:
#             decoding_result = (
#                 subprocess.check_output(args, stderr=subprocess.STDOUT)
#                 .decode('utf-8')
#                 .rsplit('\r', 1)[-1])
#         except subprocess.CalledProcessError as ex:
#             raise ValueError(
#                 f'Stream probing failed with status {ex.returncode}')
#         except UnicodeDecodeError:
#             raise ValueError('Unexpected encoding on stream probing response')
# 
#         m = re.search(r'time=(\d\d):(\d\d):(\d\d)\.(\d\d) ', decoding_result)
#         if not m:
#             raise ValueError('Failed to parse duration in the ffmpeg output')
# 
#         return (float(m.group(1)) * 60 * 60 + float(m.group(2)) * 60 +
#                 float(m.group(3)) + float(m.group(4)) / 100)
# 
# Path: yledl/utils.py
# def sane_filename(name, excludechars):
#     tr = {ord(c): ord('_') for c in excludechars}
#     x = re.sub(r'\s+', ' ', name, flags=re.UNICODE).strip(' .').translate(tr)
#     return x or 'ylevideo'


# Path: yledl/io.py
import attr
import ipaddress
import logging
import os.path
import random
import re
import subprocess
from .ffprobe import Ffprobe
from .utils import sane_filename


logger = logging.getLogger('yledl')


def convert_download_limits(arg):
    return arg or DownloadLimits()


def ffmpeg_default(arg):
    return arg or 'ffmpeg'


def ffprobe_default(arg):
    return arg or 'ffprobe'


def wget_default(arg):
    return arg or 'wget'


def random_elisa_ipv4():
    elisa_ipv4_range = list(ipaddress.ip_network('91.152.0.0/13').hosts())
    return str(random.choice(elisa_ipv4_range))


@attr.s

class DownloadLimits: