-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
131 lines (104 loc) · 3.64 KB
/
main.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import base64
import os
import re
import time
from datetime import datetime
from io import StringIO
from typing import List, Dict
import ass
import requests
from dotenv import load_dotenv
from requests import Response
from Baidu_Text_transAPI import BaiduTextTransAPI
load_dotenv()
USER_NAME = os.getenv("USER_NAME")
BASE_URI = os.getenv("BASE_URI")
API_TOKEN = os.getenv("API_TOKEN")
JELLYFIN_TARGET_LANG = os.getenv("JELLYFIN_TARGET_LANG")
SCAN_INTERVAL = int(os.getenv("SCAN_INTERVAL"))
btt_api = BaiduTextTransAPI(
os.getenv('BAIDU_APP_ID'),
os.getenv('BAIDU_APP_KEY'),
'auto',
os.getenv('BAIDU_TARGET_LANG'),
)
def jellyfin(path, method='get', **kwargs) -> 'Response':
return getattr(requests, method)(
f'{BASE_URI}/{path}',
headers={'X-MediaBrowser-Token': API_TOKEN},
**kwargs
)
users = jellyfin('Users').json() # type: List[dict]
users_name_mapping = {user.get('Name'): user for user in users} # type: Dict[str, dict]
user_id = users_name_mapping.get(USER_NAME).get('Id')
def scan(FOLDER_ID=None, callback=None):
resp = jellyfin(f'Users/{user_id}/Items', params=dict(
Fields='Path', ParentId=FOLDER_ID
)).json() # type: dict
items = resp.get('Items')
for item in items:
item: dict
if item.get('IsFolder'):
print(datetime.now(), '- scanning:', item.get('Name'))
scan(item.get('Id'), callback)
else:
item = jellyfin(f'Users/{user_id}/Items/{item.get("Id")}').json() # type: dict
if item.get('HasSubtitles'):
if callback:
callback(item)
def translate_ass(content: str, progress_title='Translating...'):
doc = ass.parse_string(content)
for style in doc.styles:
style.fontname = 'FZZhengHei-M-GBK'
tag_strs = []
texts = []
for event in doc.events:
text = event.text
tags = re.findall(r'\{.+?\}', text)
tag_str = ''.join(tags)
for tag in tags:
text = text.replace(tag, '')
text = text.replace(r'\N', '\n')
text = re.sub(r'\s+', ' ', text)
tag_strs.append(tag_str)
texts.append(text)
trans = btt_api.translate_s(
texts, callback_progress=lambda now, end: print(
f'\r[{int(now / end * 100)}%] {progress_title}',
end='' if now < end else '\n'
))
for index, event in enumerate(doc.events):
event.text = tag_strs[index] + trans[index]
with StringIO() as io:
doc.dump_file(io)
io.seek(0)
result = io.read()
return result
def translate_subtitle(item: dict):
item_id = item.get('Id')
item_name = item.get('Name')
streams = item.get('MediaStreams') # type: List[dict]
# Ass format subtitle support only current.
streams = list(filter(
lambda stream: (stream.get('Type') == 'Subtitle' and stream.get('Codec') == 'ass'), streams
))
langs = set(map(lambda stream: stream.get('Language'), streams))
if JELLYFIN_TARGET_LANG in langs:
return
for stream in streams:
index = stream.get('Index')
resp = jellyfin(f'Videos/{item_id}/{item_id}/Subtitles/{index}/Stream.ass')
content = resp.content.decode('utf-8')
content = translate_ass(content, progress_title=item_name)
jellyfin(f'Videos/{item_id}/Subtitles', method='post', json=dict(
data=base64.b64encode(content.encode('utf-8')).decode(),
format='ass',
isForced=False,
language=JELLYFIN_TARGET_LANG,
))
try:
while True:
scan(callback=translate_subtitle)
time.sleep(SCAN_INTERVAL)
except KeyboardInterrupt:
pass