-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.py
101 lines (83 loc) · 3.78 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import json
import os
import re
import subprocess
from tqdm import tqdm
from ext import *
from ext import unext_v2 as Unext_v2
from ext import dmm_tv as Dmm_tv
from ext import brainshark as Brainshark
from ext import fod as FOD
def get_parser(url):
"""
Function that is called first time to check if it's a valid supported link
:return: A class of one of supported website
"""
valid_abema = r'^["\']?http(?:|s)://(?:abema\.tv)/(?:channels|video)/(?:\w*)(?:/|-\w*/)((?P<slot>slots/)|)(?P<video_id>.*[^-_])["\']?$'
valid_gyao = r'(?isx)^["\']?http(?:|s)://gyao.yahoo.co.jp/(?:player|p|title[\w])/(?P<p1>[\w]*.*)["\']?$'
valid_aniplus = r'^["\']?http(?:|s)://(?:www\.|)aniplus-asia\.com/episode/(?P<video_id>[\w]*.*)["\']?$'
valid_unext = r'^["\']?http(?:|s)://video\.unext\.jp/(?:play|title|freeword).*(?:SID(?P<sid>[0-9]+)|ED(?P<ed>[0-9]+))["\']?$'
valid_dmm_tv = r'^["\']?http(?:s)?://tv\.dmm\.com/vod(?:/playback)?/\?.*season=(?P<season>[^&?]+)(?:&.*content=(?P<content>[^&?]+)|)["\']?$'
valid_brainshark = r'^["\']?https?://www\.brainshark\.com/brainshark/brainshark\.services\.player/api/v1\.0/Presentation\?([^&]*&)*pi=(?P<pi>[^&]+)(&|$)'
valid_fod = r'^["\']?http(?:|s)://fod\.fujitv\.co\.jp/title/(?P<title_id>[0-9a-z]+)/?(?P<episode_id>[0-9a-z]+/?)?["\']?$'
if re.match(valid_abema, url):
return AbemaTV, "abema"
elif re.match(valid_gyao, url):
return GYAO, "gyao"
elif re.match(valid_aniplus, url):
return Aniplus, "aniplus"
elif re.match(valid_unext, url):
return Unext_v2, "unext"
elif re.match(valid_dmm_tv, url):
return Dmm_tv, "dmm_tv"
elif re.match(valid_brainshark, url):
return Brainshark, "brainshark"
elif re.match(valid_fod, url):
return FOD, "fod"
return None, None
def merge_video(path, output):
"""
Merge every video chunk to a single file output
"""
with open(output, 'wb') as out:
with tqdm(total=len(path), desc="Merging", ascii=True, unit="file") as pbar:
for i in path:
out.write(open(i, 'rb').read())
os.remove(i)
pbar.update()
def mux_video(old_file, muxfile):
"""
Mux .ts or .mp4 or anything to a .mkv
It will try to use ffmpeg first, if it's not in the PATH, then it will try to use mkvmerge
If it's doesn't exist too, it just gonna skip.
"""
# MkvMerge/FFMPEG check
use_ffmpeg = False
use_mkvmerge = False
check_ffmpeg = subprocess.run(['ffmpeg', '-version'], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
check_mkvmerge = subprocess.run(['mkvmerge', '-V'], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if check_mkvmerge.returncode == 0:
use_mkvmerge = True
if check_ffmpeg.returncode == 0:
use_ffmpeg = True
else:
return "Error"
fn_, _ = os.path.splitext(old_file)
if use_mkvmerge:
subprocess.run(['mkvmerge', '-o', '{f}.{e}'.format(f=fn_, e=muxfile), old_file], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if use_ffmpeg:
subprocess.run(['ffmpeg', '-i', old_file, '-c', 'copy', '{f}.{e}'.format(f=fn_, e=muxfile)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return '{f}.{e}'.format(f=fn_,e=muxfile)
def get_yuu_folder():
if os.name == "nt":
yuu_folder = os.path.join(os.getenv('LOCALAPPDATA'), 'yuu_data')
else:
yuu_folder = os.path.join(os.getenv('HOME'), '.yuu_data')
if not os.path.isdir(yuu_folder):
os.mkdir(yuu_folder)
return yuu_folder
def _prepare_yuu_data():
yuu_folder = get_yuu_folder()
if not os.path.isfile(os.path.join(yuu_folder, 'yuu_download.json')):
with open(os.path.join(yuu_folder, 'yuu_download.json'), 'w') as f:
json.dump({}, f)