-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrosetta.py
91 lines (82 loc) · 2.79 KB
/
rosetta.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @author: loricheung
import os
import pickle
from fontTools import ttLib
class Rosetta(object):
"""点评字体反爬破解"""
def __init__(self, num_font_file, char_font_file):
"""
:Paramters:
num_font_file: 点评上用来替换数字的字体文件
char_font_file: 点评上用来替换中文的字体文件
"""
MAPPING_FILE = "character"
self.num_font = ttLib.TTFont(num_font_file)
self.char_font = ttLib.TTFont(char_font_file)
self.table = pickle.load(open(os.path.join(os.path.curdir, MAPPING_FILE), "rb"))
def _is_normal_char(self, char):
if len(char) <= 1:
if self._is_emoji(char):
return True
if ord(char) < 0x9FEF:
return True
else:
char = char.encode("unicode_escape")
if hex(int(char.replace(b"\\u", b"").decode(), 16)) < "0xe000":
return True
else:
if any([ord(c) < 0x9FEF for c in char]):
return True
def _is_emoji(self, content):
if not content:
return False
elif u"\U0001F300" <= content and content <= u"\U0001F9EF":
return True
else:
return False
def _get_chinese_char(self, chr):
uni_chr = (
b"uni"
+ chr.replace(b" ", b"")
.replace(b"\\n", b"")
.replace(b"\\t", b"")
.replace(b"\\u", b"")
).decode()
try:
char_index = self.char_font.getGlyphID(uni_chr)
except Exception:
char_index = 0
try:
num_index = self.num_font.getGlyphID(uni_chr)
except Exception:
num_index = 2**10
index = num_index if (num_index & num_index < 12) else char_index
return self.table[index - 2]
def _convert(self, chr):
if chr.isascii():
return chr
if self._is_normal_char(chr):
return chr
try:
chr_b = (
chr.encode("unicode_escape", errors="ignore")
.replace(b" ", b"")
.replace(b"\\n", b"")
.replace(b"\\t", b"")
.replace(b"\\xa0", b", ")
)
return self._get_chinese_char(chr_b)
except Exception:
return chr
def convert(self, chr_list):
"""
:Parameters:
chr_list: 网页上抓取的所有文字(包含未加密的)列表
:Returns:
str: 解密后的文字
"""
chr_list = filter(lambda x: len(x.strip()) != 0, chr_list)
chr_list = list(map(self._convert, [c.strip() for c in chr_list]))
return "".join([str(c).strip() for c in chr_list])