Repository navigation
Expand file tree
/
Copy pathpackagetools.py
More file actions
155 lines (117 loc) · 4.75 KB
/
Copy pathpackagetools.py
File metadata and controls
155 lines (117 loc) · 4.75 KB
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
#!/usr/bin/env python
# JDownloader/src/jd/controlling/LinkGrabberPackager.java
import re
from urlparse import urlparse
def matchFirst(string, *args):
""" matches against list of regexp and returns first match"""
for patternlist in args:
for pattern in patternlist:
r = pattern.search(string)
if r is not None:
name = r.group(1)
return name
return string
def parseNames(files):
""" Generates packages names from name, data lists
:param files: list of (name, data)
:return: packagenames mapt to data lists (eg. urls)
"""
packs = {}
endings = "\\.(3gp|7zip|7z|abr|ac3|aiff|aifc|aif|ai|au|avi|bin|bz2|cbr|cbz|ccf|cue|cvd|chm|dta|deb|divx|djvu|dlc|dmg|doc|docx|dot|eps|exe|ff|flv|f4v|gsd|gif|gz|iwd|iso|ipsw|java|jar|jpg|jpeg|jdeatme|load|mws|mw|m4v|m4a|mkv|mp2|mp3|mp4|mov|movie|mpeg|mpe|mpg|msi|msu|msp|nfo|npk|oga|ogg|ogv|otrkey|pkg|png|pdf|pptx|ppt|pps|ppz|pot|psd|qt|rmvb|rm|rar|ram|ra|rev|rnd|r\\d+|rpm|run|rsdf|rtf|sh(!?tml)|srt|snd|sfv|swf|tar|tif|tiff|ts|txt|viv|vivo|vob|wav|wmv|xla|xls|xpi|zeno|zip|z\\d+|_[_a-z]{2}|\\d+$)"
rarPats = [re.compile("(.*)(\\.|_|-)pa?r?t?\\.?[0-9]+.(rar|exe)$", re.I),
re.compile("(.*)(\\.|_|-)part\\.?[0]*[1].(rar|exe)$", re.I),
re.compile("(.*)\\.rar$", re.I),
re.compile("(.*)\\.r\\d+$", re.I),
re.compile("(.*)(\\.|_|-)\\d+$", re.I)]
zipPats = [re.compile("(.*)\\.zip$", re.I),
re.compile("(.*)\\.z\\d+$", re.I),
re.compile("(?is).*\\.7z\\.[\\d]+$", re.I),
re.compile("(.*)\\.a.$", re.I)]
ffsjPats = [re.compile("(.*)\\._((_[a-z])|([a-z]{2}))(\\.|$)"),
re.compile("(.*)(\\.|_|-)[\\d]+(" + endings + "$)", re.I)]
iszPats = [re.compile("(.*)\\.isz$", re.I),
re.compile("(.*)\\.i\\d{2}$", re.I)]
pat1 = re.compile("(\\.?CD\\d+)", re.I)
pat2 = re.compile("(\\.?part\\d+)", re.I)
pat3 = re.compile("(.+)[\\.\\-_]+$")
pat4 = re.compile("(.+)\\.\\d+\\.xtm$")
for file, url in files:
patternMatch = False
if file is None:
continue
# remove trailing /
name = file.rstrip('/')
# extract last path part .. if there is a path
split = name.rsplit("/", 1)
if len(split) > 1:
name = split.pop(1)
#check if a already existing package may be ok for this file
# found = False
# for pack in packs:
# if pack in file:
# packs[pack].append(url)
# found = True
# break
#
# if found: continue
# unrar pattern, 7zip/zip and hjmerge pattern, isz pattern, FFSJ pattern
before = name
name = matchFirst(name, rarPats, zipPats, iszPats, ffsjPats)
if before != name:
patternMatch = True
# xtremsplit pattern
r = pat4.search(name)
if r is not None:
name = r.group(1)
# remove part and cd pattern
r = pat1.search(name)
if r is not None:
name = name.replace(r.group(0), "")
patternMatch = True
r = pat2.search(name)
if r is not None:
name = name.replace(r.group(0), "")
patternMatch = True
# additional checks if extension pattern matched
if patternMatch:
# remove extension
index = name.rfind(".")
if index <= 0:
index = name.rfind("_")
if index > 0:
length = len(name) - index
if length <= 4:
name = name[:-length]
# remove endings like . _ -
r = pat3.search(name)
if r is not None:
name = r.group(1)
# replace . and _ with space
name = name.replace(".", " ")
name = name.replace("_", " ")
name = name.strip()
else:
name = ""
# fallback: package by hoster
if not name:
name = urlparse(file).hostname
if name: name = name.replace("www.", "")
# fallback : default name
if not name:
name = "unknown"
# build mapping
if name in packs:
packs[name].append(url)
else:
packs[name] = [url]
return packs
if __name__ == "__main__":
from os.path import join
from pprint import pprint
f = open(join("..", "..", "testlinks2.txt"), "rb")
urls = [(x.strip(), x.strip()) for x in f.readlines() if x.strip()]
f.close()
print "Having %d urls." % len(urls)
packs = parseNames(urls)
pprint(packs)
print "Got %d urls." % sum([len(x) for x in packs.itervalues()])