-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsponsrdump.py
executable file
·749 lines (544 loc) · 23 KB
/
sponsrdump.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#! /usr/bin/env python3
import json
import logging
import os
import re
import shlex
import shutil
from collections import defaultdict
from contextlib import contextmanager
from enum import Enum
from os import listdir
from pathlib import Path
from pprint import pformat
from subprocess import Popen, PIPE
from textwrap import wrap
from typing import List, Callable, Union, Dict, NamedTuple, Tuple, TypeVar, Type
from urllib.parse import parse_qs, urlparse
from uuid import uuid4
import requests
from bs4 import BeautifulSoup
from html2text import html2text
from lxml import etree
from requests import HTTPError
from requests.cookies import cookiejar_from_dict
LOGGER = logging.getLogger(__name__)
PATH_BASE = Path(__file__).parent.absolute()
RE_FILENAME_INVALID = re.compile(r'[:?"/<>\\|*]')
class FileType(Enum):
TEXT = 0
VIDEO = 1
AUDIO = 2
IMAGE = 3
_CLEANUP = True
TypeTextConverter = TypeVar('TypeTextConverter', bound='TextConverter')
class TextConverter:
alias: str = ''
register: Dict[str, Type[TypeTextConverter]] = {}
def __init_subclass__(cls):
super().__init_subclass__()
cls.register[cls.alias] = cls
def _convert(self, value: str) -> str:
raise NotImplementedError
def dump(self, value: str, *, dest: Path) -> Path:
target = dest.with_suffix(f'.{self.alias}')
with open(target, 'w') as f:
f.write(self._convert(value))
return target
@classmethod
def spawn(cls, alias: str) -> 'TypeTextConverter':
return cls.register[alias]()
class HtmlConverter(TextConverter):
alias = 'html'
def _convert(self, value: str) -> str:
return value
class MarkdownConverter(TextConverter):
alias = 'md'
def _convert(self, value: str) -> str:
return html2text(value)
class VideoPreference(NamedTuple):
frame: str = 'best'
sound: str = 'best'
@contextmanager
def chdir(where: Path):
current_dir = Path.cwd()
try:
os.chdir(where)
yield
finally:
os.chdir(current_dir)
class SponsrDumperError(Exception):
"""Base exception."""
class SponsrDumper:
_url_base: str = 'https://sponsr.ru'
_fname_conf: str = 'sponsrdump.json'
_fname_auth: str = 'sponsrdump_auth.txt'
_headers: dict = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'ru,en;q=0.9',
'Cache-Control': 'no-cache',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'cross-site',
'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "YaBrowser";v="23"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Linux"',
}
def __init__(self, url: str):
self.url = url
self.project_id: str = ''
self._collected: List[dict] = []
self._dumped: Dict[str, str] = {}
session = requests.Session()
session.headers = self._headers
self._session = session
self._auth_read()
@classmethod
def text_to_video(cls, src: Path) -> Path:
font = 'tahoma.ttf'
line_width = 80
sec_per_line = 3
sec_plus = 5
line_space = 20
font_size = 25
fname_stem = src.stem
path_bg = PATH_BASE / 'bg.png'
path_tmp_bg = src.with_suffix('.mp4').with_stem(f'{fname_stem}_bg')
path_tmp_bg.unlink(missing_ok=True)
path_tmp_text = src.with_suffix('.txt').with_stem(f'{fname_stem}_txt')
path_tmp_text.unlink(missing_ok=True)
path_target = src.with_suffix('.mp4').with_stem(f'{fname_stem} [txt]')
path_target.unlink(missing_ok=True)
LOGGER.info(f' Generating text video: {path_target} ...')
with open(src) as f:
text = f.read()
text = text.strip().strip('_ ').strip().replace('\u200e', '').replace('\u200f', '')
lines = []
for line in text.splitlines():
lines.extend(wrap(line, width=line_width))
vid_len = (len(lines) * sec_per_line) + sec_plus
with open(path_tmp_text, 'w') as f:
f.write('\r\n'.join(lines))
cls.call(f'ffmpeg -loop 1 -t {vid_len} -i "{path_bg}" "{path_tmp_bg}"', cwd=src.parent)
try:
cls.call(
(
f'ffmpeg -i "{path_tmp_bg}" -filter_complex "'
'[0]split[txt][orig];'
'[txt]drawtext='
f'fontfile={font}:'
f'fontsize={font_size}:'
'fontcolor=white:'
f'x=(w-text_w)/2+{line_space}:'
f'y=h-{line_space}*t:'
f'textfile=\'{path_tmp_text}\':'
'bordercolor=black:'
f'line_spacing={line_space}:'
'borderw=3[txt];'
'[orig]crop=iw:50:0:0[orig];'
'[txt][orig]overlay" '
f'-c:v libx264 -y -preset ultrafast -t {vid_len} "{path_target}"'
),
cwd=src.parent,
)
finally:
path_tmp_bg.unlink(missing_ok=True)
path_tmp_text.unlink(missing_ok=True)
return path_target
@classmethod
def call(cls, cmd: str, *, cwd: Path, capture_out: bool = True):
prc = Popen(cmd, cwd=cwd, shell=True, stdout=PIPE if capture_out else None, stderr=PIPE)
out, err = [item.decode() if item else '' for item in prc.communicate()]
if prc.returncode:
raise SponsrDumperError(f'Command error:\n{cmd}\n\n{out}\n\n{err}\n----------')
@classmethod
def _concat_chunks(cls, *, src: Path, suffix: str) -> Path:
with chdir(src):
src_files = sorted([f'{fname}' for fname in listdir(src) if f'_{suffix}.' in fname])
target = f'{uuid4()}.mp4'
src_files_str = '" "'.join(src_files)
cls.call(f'cat "{src_files_str}" > "{target}"', cwd=src)
for src_file in src_files:
(src / src_file).unlink()
return src / target
@classmethod
def _get_soup(cls, html: str) -> BeautifulSoup:
return BeautifulSoup(html, 'lxml')
@classmethod
def _mpd_parse(cls, fpath: Path):
with open(fpath) as f:
xml = f.read()
xml = re.sub("xmlns(:[^=]*)?='[^']+'", '', xml)
root = etree.fromstring(xml, etree.XMLParser(
no_network=True,
huge_tree=True,
remove_blank_text=True,
resolve_entities=False,
))
video = defaultdict(list)
trash = defaultdict(list)
audio = defaultdict(list)
for aset in root.findall('.//AdaptationSet'):
mime = aset.attrib['mimeType']
if mime.startswith('video'):
bucket = video
elif mime.startswith('audio'):
bucket = audio
else:
bucket = trash
for repres in aset.findall('Representation'):
if audio_rate := repres.attrib.get('audioSamplingRate'):
ident = audio_rate
else:
ident = f"{repres.attrib['width']}x{repres.attrib['height']}"
for url_element in repres[0]:
url = url_element.attrib.get('sourceURL') or url_element.attrib.get('media')
range = url_element.attrib.get('range') or url_element.attrib.get('mediaRange')
if url and url not in bucket[ident]:
bucket[ident].append((url, range))
def sort_idents(container):
return dict(sorted(container.items(), key=lambda items: int(items[0].split('x', 1)[0])))
video = sort_idents(video)
audio = sort_idents(audio)
LOGGER.info(f" Found media formats: video - {', '.join(video)}; audio - {', '.join(audio)}.")
return video, audio
def _download_file(
self,
url: str,
*,
dest: Path,
stream: bool = True,
prefer_video: VideoPreference,
range: str = ''
):
if not url.startswith('http'):
url = f'{self._url_base}{url}'
headers = {}
if range:
headers.update({
'Accept': '*/*',
'Accept-Encoding': 'identity',
'Connection': 'keep-alive',
'Range': f'bytes={range}',
'Referer': 'https://kinescope.io/',
})
is_mpd = url.endswith('.mpd')
dest_tmp = None
if is_mpd:
headers['Referer'] = (
'https://kinescope.io/203245765?enableIframeApi'
'&playerId=player&size%5Bwidth%5D=100%25&size%5Bheight%5D=100%25&preload=none'
)
dest_tmp = dest.with_suffix('.tmp')
with self._session.get(url, stream=stream, headers=headers) as response:
if response.status_code == 403:
LOGGER.error('Access denied.')
response.raise_for_status()
with open(dest_tmp or dest, 'wb') as f:
for chunk in response.iter_content(chunk_size=1024):
f.write(chunk)
if is_mpd:
try:
# download mpd chunks
self._mpd_process(mpd=dest_tmp, dest=dest, prefer_video=prefer_video)
finally:
_CLEANUP and dest_tmp.unlink(missing_ok=True)
def _mpd_process(self, *, mpd: Path, dest: Path, prefer_video: VideoPreference):
dest_tmp = (mpd.parent / 'tmp').absolute()
dest_tmp.mkdir(parents=True, exist_ok=True)
def download_all(urls: List[Tuple[str, str]], *, suffix: str):
for idx, (url, range) in enumerate(urls, 1):
file_dest = dest_tmp / f'{idx:>05}_{suffix}{dest.suffix}'
self._download_file(url, dest=file_dest, prefer_video=prefer_video, range=range)
try:
video, audio = self._mpd_parse(mpd)
download_all(
video.get(prefer_video.frame) or video[list(video.keys())[-1]],
suffix='vid'
)
LOGGER.info(' Joining video chunks ...')
f_video = self._concat_chunks(src=dest_tmp, suffix='vid')
download_all(
audio.get(prefer_video.sound) or audio[list(audio.keys())[-1]],
suffix='aud'
)
LOGGER.info(' Joining audio chunks ...')
f_audio = self._concat_chunks(src=dest_tmp, suffix='aud')
# join video + audio
LOGGER.info(' Compiling final video ...')
self.call(
f'ffmpeg -i "{f_video}" -i "{f_audio}" -c copy {shlex.quote(str(dest))}',
cwd=dest_tmp,
)
finally:
_CLEANUP and shutil.rmtree(dest_tmp)
def _get_response(self, url: str, *, xhr: bool = False) -> requests.Response:
if not url.startswith('http'):
url = f'{self._url_base}{url}'
headers = {}
if xhr:
headers.update({
'X-Requested-With': 'XMLHttpRequest',
'e': 'true',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Sec-Fetch-Site': 'same-origin',
})
response = self._session.get(url, headers=headers)
response.raise_for_status()
return response
def _normalize_files(self, post: dict):
audio = []
video = []
text = []
images = []
post['__files'] = {
'audio': audio,
'video': video,
'text': text,
'images': images,
}
for file_info in post.get('files') or []:
assert file_info['file_category'] == 'podcast', f'Unsupported file category found: {file_info}'
if not file_info['file_duration']:
LOGGER.debug(f'Probably missing {file_info["file_link"]}. Skipped.')
continue
file_info['file_type'] = FileType.AUDIO
audio.append(file_info)
post_title = post['post_title'].rstrip('.')
post_text = post.get('post_text', post.get('post_small_text', '')).strip()
text.append({
'file_id': f"{post['post_id']}",
'file_title': f'{post_title}.html',
'file_path': '',
'file_type': FileType.TEXT,
'__content': post_text,
})
soup = self._get_soup(post_text)
for image in soup.find_all('img'):
if (src := image['src']) and (image_name := Path(urlparse(src).path).name):
images.append({
'file_id': image_name,
'file_title': image_name,
'file_path': src,
'file_type': FileType.IMAGE,
})
for iframe in soup.find_all('iframe'):
if 'video' in (src := iframe['src']) and (file_id := parse_qs(urlparse(src).query).get('video_id')):
# workaround bogus links like /post/video/?video_id=xxx?poster_id=yyy
file_id = file_id[0].partition('?')[0]
video.append({
'file_id': file_id,
'file_title': f'{post_title}.mp4',
'file_path': f'https://kinescope.io/{file_id}/master.mpd',
'file_type': FileType.VIDEO,
})
def _collect_posts(self, *, project_id: str, func_filter: Callable = None) -> List[dict]:
posts_all = []
rows_seen = 0
rows_total = 1
func_filter = func_filter or (lambda post_info: post_info)
while rows_seen < rows_total:
data = self._get_response(f'/project/{project_id}/more-posts/?offset={rows_seen}').json()['response']
posts_current = data['rows']
rows_seen += len(posts_current)
posts_current = [post for post in posts_current if func_filter(post)]
for post in posts_current:
self._normalize_files(post)
posts_all.extend(posts_current)
rows_total = data['rows_count']
LOGGER.debug(f'Searched {rows_seen}/{rows_total} ...')
return posts_all
def _get_project_id(self) -> str:
soup = self._get_soup(self._get_response(self.url).text)
project_id = soup.find_all(id='project_id')[0]['value']
return project_id
def _auth_read(self):
path = Path(self._fname_auth)
if not path.exists():
raise SponsrDumperError(f'File {path} is not found in the current directory.')
try:
with open(path) as f:
data = f.read().rstrip(';')
self._session.cookies = cookiejar_from_dict(
dict(
line.strip().split('=', 1)
for line in data.split(';')
)
)
except ValueError:
raise SponsrDumperError(f'File {path} contents is not valid.')
def _auth_write(self):
with open(Path(self._fname_auth), 'w') as f:
return f.write(
';'.join([
'%s=%s' % (key, val)
for key, val in self._session.cookies.get_dict().items()
])
)
def _conf_load(self):
fname = Path(self._fname_conf)
if not fname.exists():
self._conf_save()
else:
LOGGER.info(f'Configuration is loaded from {fname} ...')
with open(fname) as f:
data = json.load(f)
self._dumped = data.get('dumped', {})
def _conf_save(self):
fname = Path(self._fname_conf)
with open(fname, 'w') as f:
json.dump(
{
'dumped': self._dumped,
},
f,
ensure_ascii=False,
indent=2,
)
@contextmanager
def _configuration(self):
self._conf_load()
try:
yield
finally:
self._conf_save()
def search(self, *, func_filter: Callable = None) -> int:
LOGGER.info(f'Searching data for {self.url} ...')
project_id = self._get_project_id()
self.project_id = project_id
LOGGER.debug(f'Project ID: {project_id}')
collected = self._collect_posts(project_id=project_id, func_filter=func_filter)
self._collected = collected
found = len(collected)
LOGGER.info(f'Found articles: {found}')
return found
def dump(
self,
dest: Union[str, Path],
*,
func_filename: Callable = None,
reverse: bool = True,
audio: bool = True,
video: bool = True,
images: bool = True,
text: Union[bool, str] = True,
text_to_video: bool = True,
prefer_video: VideoPreference = VideoPreference(),
):
LOGGER.info(f'Start dump using preference: {prefer_video} ...')
func_filename = func_filename or (
lambda post_inf, file_inf: RE_FILENAME_INVALID.sub(
'',
f"{post_inf['__idx']:>03}. "
f"{file_inf['__idx']:>03}. "
f"{post_inf['post_title'].rstrip('.')}"
f"{Path(file_inf['file_title']).suffix}"
)
)
dest = Path(dest).absolute()
dest.mkdir(parents=True, exist_ok=True)
collected = self._collected
if reverse:
collected = list(reversed(collected))
realms = []
audio and realms.append('audio')
video and realms.append('video')
images and realms.append('images')
text and realms.append('text')
with self._configuration():
post_idx = 0
posts_total = len(collected)
for idx, post_info in enumerate(collected, 1):
# 'post_id' 'level_id' 'post_date' 'post_title' 'post_text' 'post_url' 'tags'
file_idx = 0
post_idx += 1
post_info['__idx'] = post_idx
msg_prefix = f'[{idx}/{posts_total} {round(100 * idx / posts_total, 1)}%] '
for realm in realms:
for file_info in post_info['__files'][realm]:
# 'files': 'file_id' 'file_path' 'file_title' 'file_link' 'file_duration' 'file_order'
file_id = file_info['file_id']
file_title = file_info['file_title']
msg_postfix = f'File {file_id} [{file_title}]:'
file_id_conf = f'f_{file_id}'
file_idx += 1
file_info['__idx'] = file_idx
if file_id_conf in self._dumped:
LOGGER.warning(f'{msg_prefix} Skipped {msg_postfix}')
continue
LOGGER.info(f'{msg_prefix} Downloading {msg_postfix} ...')
file_type = file_info['file_type']
filename = func_filename(post_info, file_info)
dest_filename = dest / filename
if filepath := file_info['file_path']:
try:
self._download_file(
filepath,
dest=dest_filename,
stream=file_type is not FileType.IMAGE,
prefer_video=prefer_video
)
except HTTPError:
LOGGER.debug(f'{pformat(file_info, indent=2)}')
raise
if file_type is FileType.TEXT and text:
converter_alias_md = MarkdownConverter.alias
converter_alias = converter_alias_md if isinstance(text, bool) else text
if text_to_video:
conversion_required = converter_alias != converter_alias_md
text_to_video_src_filename = TextConverter.spawn(
converter_alias_md
).dump(file_info['__content'], dest=dest_filename)
self.text_to_video(text_to_video_src_filename)
if conversion_required:
text_to_video_src_filename.unlink(missing_ok=True)
if converter_alias != converter_alias_md:
dest_filename = TextConverter.spawn(
converter_alias
).dump(file_info['__content'], dest=dest_filename)
filename = dest_filename.name
self._dumped[file_id_conf] = filename
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
'project_url', help='URL проекта на sponsr.ru')
parser.add_argument(
'--debug', help='Вывести отладочную информацию', action='store_true')
parser.add_argument(
'--title', help='Фильтр заголовка для отбора статей', default='')
parser.add_argument(
'--to', help='Путь назначения для файлов', default='dump/')
parser.add_argument(
'--prefer-video', help='Предпочтительное разрешение видео', default='best')
parser.add_argument(
'--text-fmt', help=(
f'Формат для текстовых данных. Варианты: {", ".join(sorted(TextConverter.register.keys()))}'),
default='txt')
parser.add_argument(
'--no-audio', help='Не следует скачивать аудио', action='store_true')
parser.add_argument(
'--no-video', help='Не следует скачивать видео', action='store_true')
parser.add_argument(
'--no-text', help='Не следует скачивать текст', action='store_true')
parser.add_argument(
'--no-images', help='Не следует скачивать изображения', action='store_true')
parser.add_argument(
'--text-to-video', help='Следует ли создать видео с текстом статьи', action='store_true')
args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO, format='%(levelname)-8s: %(message)s')
dumper = SponsrDumper(args.project_url)
filter_func = None
if title := args.title.strip():
filter_func = lambda post_info: title in post_info['post_title']
dumper.search(func_filter=filter_func)
dumper.dump(
args.to,
prefer_video=VideoPreference(frame=args.prefer_video),
audio=not args.no_audio,
video=not args.no_video,
images=not args.no_images,
text=False if args.no_text else args.text_fmt.lower(),
text_to_video=args.text_to_video
)