-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
202 lines (154 loc) · 4.23 KB
/
utils.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
def convertRules(mode, dic):
if "include" in mode:
for ruleName in mode["include"]:
dic[ruleName] = "include"
if "exclude" in mode:
for ruleName in mode["exclude"]:
dic[ruleName] = "exclude"
for ruleName, rules in mode.items():
if ruleName not in ("include", "exclude"):
if "include" in rules:
if isinstance(ruleName, tuple):
for item in ruleName:
dic[item] = rules
else:
dic[ruleName] = rules
continue
minAge = rules["age"] if "age" in rules else False
minRatio = rules["ratio"] if "ratio" in rules else False
minSeeds = rules["seeds"] if "seeds" in rules else False
minSize = rules["size"] if "size" in rules else False
if "fb_mode" in rules and rules["fb_mode"] != 0:
fbMode = rules["fb_mode"]
fbAge = rules["fb_age"] if "fb_age" in rules else False
fbRatio = rules["fb_ratio"] if "fb_ratio" in rules else False
fbSeeds = rules["fb_seeds"] if "fb_seeds" in rules else False
fbSize = rules["fb_size"] if "fb_size" in rules else False
else:
fbMode = fbAge = fbRatio = fbSeeds = fbSize = False
if "blacklist" in rules:
filter_ = "blacklist"
filterList = rules["blacklist"]
elif "whitelist" in rules:
filter_ = "whitelist"
filterList = rules["whitelist"]
else:
filter_ = filterList = False
if isinstance(ruleName, tuple):
for item in ruleName:
dic[item] = (
minAge,
minRatio,
minSeeds,
minSize,
fbMode,
fbAge,
fbRatio,
fbSeeds,
fbSize,
filter_,
filterList,
)
else:
dic[ruleName] = (
minAge,
minRatio,
minSeeds,
minSize,
fbMode,
fbAge,
fbRatio,
fbSeeds,
fbSize,
filter_,
filterList,
)
def sortTorrents(sortOrder, groupOrder, torrents):
def sortList(list_, key):
ordered[key] = [
torrentData
for i in list_
for torrentData in sorted(
ordered[key][i],
key=lambda x: (
-x[toIndex[sortOrder[0]]],
-x[toIndex[sortOrder[1]]],
-x[toIndex[sortOrder[2]]],
-x[toIndex[sortOrder[3]]],
),
)
]
toIndex = {"labels": 2, "trackers": 3, "age": 4, "ratio": 5, "seeds": 6, "size": 7}
while len(sortOrder) < 4:
sortOrder.append(sortOrder[-1])
if not groupOrder:
return sorted(
torrents,
key=lambda x: (
-x[toIndex[sortOrder[0]]],
-x[toIndex[sortOrder[1]]],
-x[toIndex[sortOrder[2]]],
-x[toIndex[sortOrder[3]]],
),
)
order, labelOrder, trackerOrder = [], [], []
labelsPairedWithTrackers = {}
for list_ in groupOrder:
if "labels" in list_:
order.append("labels")
for item in list_[1:]:
if isinstance(item, tuple) or isinstance(item, list):
labelsPairedWithTrackers[item[0]] = item[1:]
labelOrder.append(item[0])
else:
labelOrder.append(item)
elif "trackers" in list_:
order.append("trackers")
trackerOrder = list_[1:]
else:
order.append("unmatched")
ordered = {
"labels": {label: [] for label in labelOrder} if labelOrder else [],
"trackers": {tracker: [] for tracker in trackerOrder} if trackerOrder else [],
"unmatched": [],
}
labelsOrdered = ordered["labels"]
trackersOrdered = ordered["trackers"]
unmatched = ordered["unmatched"]
for torrentData in torrents:
label = torrentData[toIndex["labels"]]
trackers = str(torrentData[toIndex["trackers"]])
if label in labelsOrdered:
if label in labelsPairedWithTrackers:
if [tracker for tracker in labelsPairedWithTrackers[label] if tracker in trackers]:
labelsOrdered[label].append(torrentData)
continue
else:
labelsOrdered[label].append(torrentData)
continue
if trackersOrdered:
tracker = [tracker for tracker in trackerOrder if tracker in trackers]
if tracker:
trackersOrdered[tracker[0]].append(torrentData)
continue
unmatched.append(torrentData)
if labelsOrdered:
sortList(labelOrder, "labels")
if trackersOrdered:
sortList(trackerOrder, "trackers")
if unmatched:
unmatched.sort(
key=lambda x: (
-x[toIndex[sortOrder[0]]],
-x[toIndex[sortOrder[1]]],
-x[toIndex[sortOrder[2]]],
-x[toIndex[sortOrder[3]]],
)
)
orderedList = []
for group in order:
orderedList += ordered[group]
ordered[group].clear()
if unmatched:
orderedList += unmatched
return orderedList