-
Notifications
You must be signed in to change notification settings - Fork 3
/
ed2kConvert.py
73 lines (66 loc) · 3.31 KB
/
ed2kConvert.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
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# ed2k Link Converter - the Convert part
# Auther Name: Wei-Jie Hsiao (a.k.a. RJ or RJ Hsiao, RJking, RJ-king)
# Date: 2010/12/17
# Version: 1.0
##########################################################################
# LICENSE #
##########################################################################
# This file is part of ed2k Link Converter. #
# #
# ed2k Link Converter is free software: you can redistribute it #
# and/or modify it under the terms of the GNU General Public License #
# as published by the Free Software Foundation, either #
# version 3 of the License, or (at your option) any later version. #
# #
# ed2k Link Converter is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with ed2k Link Converter. If not, see #
# <http://www.gnu.org/licenses/>. #
##########################################################################
### Program Start
import urllib
import re
## Defind constant
NOTAG = 0
TAG_HTML = 1
TAG_BBCODE = 2
ed2kQuoteSafe = ":,/|=!@#$%^&_-+'`~[](){}"
ed2kQuoteSafe_BBbbcode = ":,/|=!@#$%^&_-+'`~(){}"
## the ed2k file link format regex
ed2kFileLinkRegex = re.compile(\
'(ed2k://\|file\|([^\'"|]+?)\|\d+\|[a-fA-F0-9]{32}\|' + \
'(((p=[a-fA-F0-9]{32}(:[a-fA-F0-9]{32})*\|)?(h=\w{32}\|)?(s=http://[\w\.-_&%/]+\|)*)|' + \
'((p=[a-fA-F0-9]{32}(:[a-fA-F0-9]{32})*\|)?(s=http://[\w\.-_&%/]+\|)*(h=\w{32}\|)?)|' + \
'((h=\w{32}\|)?(p=[a-fA-F0-9]{32}(:[a-fA-F0-9]{32})*\|)?(s=http://[\w\.-_&%/]+\|)*)|' + \
'((h=\w{32}\|)?(s=http://[\w\.-_&%/]+\|)*(p=[a-fA-F0-9]{32}(:[a-fA-F0-9]{32})*\|)?)|' + \
'((s=http://[\w\.-_&%/]+\|)*(p=[a-fA-F0-9]{32}(:[a-fA-F0-9]{32})*\|)?(h=\w{32}\|)?)|' + \
'((s=http://[\w\.-_&%/]+\|)*(h=\w{32}\|)?(p=[a-fA-F0-9]{32}(:[a-fA-F0-9]{32})*\|)?))' + \
'/(\|sources,[\w\.-_]+:\d{1,5}\|/)?)')
ed2kLinkTemplate = ['{0}\n','<a href="{0}">{1}</a>\n','[url={0}]{1}[/url]\n']
def ConvertLink(inputLink, desType = NOTAG, isUTF8URL = True):
"""
ConvertLink(inputLink, desType = ed2kConvert.NOTAG, utf8url = True)
"""
if inputLink == '':
return ''
detectLink = ed2kFileLinkRegex.findall(inputLink)
if detectLink == []:
return None
returnStr = ''
for ed2kLink in detectLink:
if isUTF8URL is True:
srcLink = urllib.quote(ed2kLink[0].encode('utf_8'), ed2kQuoteSafe)
else:
srcLink = urllib.unquote(ed2kLink[0].encode('utf_8'))
if desType is TAG_BBCODE:
pass # In this case, we have to convert '[' & ']' to utf8 format. If not
srcLink = srcLink.replace('[','%5B').replace(']','%5D')
fileName = urllib.unquote(ed2kLink[1].encode('utf_8'))
returnStr += ed2kLinkTemplate[desType].format(srcLink, fileName).decode('utf_8')
return returnStr