twitch-dl/twitchdl/output.py

74 lines
1.8 KiB
Python
Raw Normal View History

2018-01-25 10:09:20 +00:00
# -*- coding: utf-8 -*-
import sys
import re
2020-04-11 11:08:42 +00:00
from twitchdl import utils
2018-01-25 10:09:20 +00:00
START_CODES = {
2020-05-17 11:35:51 +00:00
'b': '\033[1m',
2020-05-17 12:32:37 +00:00
'dim': '\033[2m',
2019-08-13 10:29:42 +00:00
'i': '\033[3m',
'u': '\033[4m',
'red': '\033[91m',
'green': '\033[92m',
'yellow': '\033[93m',
'blue': '\033[94m',
'magenta': '\033[95m',
'cyan': '\033[96m',
2018-01-25 10:09:20 +00:00
}
END_CODE = '\033[0m'
START_PATTERN = "<(" + "|".join(START_CODES.keys()) + ")>"
END_PATTERN = "</(" + "|".join(START_CODES.keys()) + ")>"
USE_ANSI_COLOR = "--no-color" not in sys.argv
def start_code(match):
name = match.group(1)
return START_CODES[name]
def colorize(text):
text = re.sub(START_PATTERN, start_code, text)
text = re.sub(END_PATTERN, END_CODE, text)
return text
def strip_tags(text):
text = re.sub(START_PATTERN, '', text)
text = re.sub(END_PATTERN, '', text)
return text
def print_out(*args, **kwargs):
args = [colorize(a) if USE_ANSI_COLOR else strip_tags(a) for a in args]
print(*args, **kwargs)
def print_err(*args, **kwargs):
args = ["<red>{}</red>".format(a) for a in args]
args = [colorize(a) if USE_ANSI_COLOR else strip_tags(a) for a in args]
print(*args, file=sys.stderr, **kwargs)
2020-04-11 11:08:42 +00:00
def print_video(video):
published_at = video["publishedAt"].replace("T", " @ ").replace("Z", "")
length = utils.format_duration(video["lengthSeconds"])
channel = video["creator"]["channel"]["displayName"]
game = video["game"]["name"]
2020-04-11 11:08:42 +00:00
# Can't find URL in video object, strange
url = "https://twitch.tv/{}".format(video["id"])
2020-05-17 11:35:51 +00:00
print_out("\n<b>{}</b>".format(video["id"]))
2020-04-11 11:08:42 +00:00
print_out("<green>{}</green>".format(video["title"]))
2020-05-17 11:48:16 +00:00
print_out("<blue>{}</blue> playing <blue>{}</blue>".format(channel, game))
print_out("Published <blue>{}</blue> Length: <blue>{}</blue> ".format(published_at, length))
print_out("<i>{}</i>".format(url))