-
Notifications
You must be signed in to change notification settings - Fork 0
/
thirdparty.py
executable file
·41 lines (38 loc) · 1.12 KB
/
thirdparty.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__all__ = [
'chardet',
'trydecodingbytes',
]
def chardet(bs):
try:
import chardet
return chardet.detect(bs)
except ImportError:
try:
import cchardet
return cchardet.detect(bs)
except ImportError:
raise
def trydecodingbytes(bs):
try:
import bs4
dammit = bs4.UnicodeDammit(bs)
unicode_markup, original_encoding, tried_encodings = dammit.unicode_markup, dammit.original_encoding, dammit.tried_encodings
except ImportError:
unicode_markup, original_encoding, tried_encodings = None, None, None
if unicode_markup:
return unicode_markup, original_encoding
try:
encoding = chardet(bs)['encoding']
except ImportError:
encoding = None
if not encoding and tried_encodings and tried_encodings[0] and tried_encodings[0][0]:
encoding = tried_encodings[0][0]
if encoding:
try:
s = bs.decode(encoding = encoding)
except Exception:
s = str(bs)
encoding = None
return s, encoding