forked from SilentDemonSD/WZML-X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wserver.py
782 lines (699 loc) · 20 KB
/
wserver.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
from logging import getLogger, FileHandler, StreamHandler, INFO, basicConfig
from time import sleep
from qbittorrentapi import NotFound404Error, Client as qbClient
from aria2p import API as ariaAPI, Client as ariaClient
from flask import Flask, request
from web.nodes import make_tree
app = Flask(__name__)
aria2 = ariaAPI(ariaClient(host="http://localhost", port=6800, secret=""))
basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[FileHandler('log.txt'), StreamHandler()],
level=INFO)
LOGGER = getLogger(__name__)
page = """
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Torrent File Selector</title>
<link rel="icon" href="https://graph.org/file/1a6ad157f55bc42b548df.png" type="image/jpg">
<script
src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha256-4+XzXVhsDmqanXGHaHvgh1gMQKX40OUvDEBTu8JcmNs="
crossorigin="anonymous"
></script>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Ubuntu:ital,wght@0,300;0,400;0,500;0,700;1,300;1,400;1,500;1,700&display=swap"
rel="stylesheet"
/>
<link
rel="stylesheet"
href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css"
integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p"
crossorigin="anonymous"
/>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Ubuntu", sans-serif;
list-style: none;
text-decoration: none;
outline: none !important;
color: white;
}
body{
background-color: #0D1117;
}
header{
margin: 3vh 1vw;
padding: 0.5rem 1rem 0.5rem 1rem;
display: flex;
align-items: center;
justify-content: space-between;
border-bottom: #161B22;
border-radius: 30px;
background-color: #161B22;
border: 2px solid rgba(255, 255, 255, 0.11);
}
header:hover, section:hover{
box-shadow: 0px 0px 15px black;
}
.brand{
display: flex;
align-items: center;
}
img{
width: 2.5rem;
height: 2.5rem;
border: 2px solid black;
border-radius: 50%;
}
.name{
margin-left: 1vw;
font-size: 1.5rem;
}
.intro{
text-align: center;
margin-bottom: 2vh;
margin-top: 1vh;
}
.social a{
font-size: 1.5rem;
padding-left: 1vw;
}
.social a:hover, .brand:hover{
filter: invert(0.3);
}
section{
margin: 0vh 1vw;
margin-bottom: 10vh;
padding: 1vh 3vw;
display: flex;
flex-direction: column;
border: 2px solid rgba(255, 255, 255, 0.11);
border-radius: 20px;
background-color: #161B22 ;
}
li:nth-child(1){
padding: 1rem 1rem 0.5rem 1rem;
}
li:nth-child(n+1){
padding-left: 1rem;
}
li label{
padding-left: 0.5rem;
}
li{
padding-bottom: 0.5rem;
}
span{
margin-right: 0.5rem;
cursor: pointer;
user-select: none;
transition: transform 200ms ease-out;
}
span.active{
transform: rotate(90deg);
-ms-transform: rotate(90deg); /* for IE */
-webkit-transform: rotate(90deg);/* for browsers supporting webkit (such as chrome, firefox, safari etc.). */
display: inline-block;
}
ul{
margin: 1vh 1vw 1vh 1vw;
padding: 0 0 0.5rem 0;
border: 2px solid black;
border-radius: 20px;
background-color: #1c2129;
overflow: hidden;
}
input[type="checkbox"]{
cursor: pointer;
user-select: none;
}
input[type="submit"] {
border-radius: 20px;
margin: 2vh auto 1vh auto;
width: 50%;
display: block;
height: 5.5vh;
border: 2px solid rgba(255, 255, 255, 0.11);
background-color: #0D1117;
font-size: 16px;
font-weight: 500;
}
input[type="submit"]:hover, input[type="submit"]:focus{
background-color: rgba(255, 255, 255, 0.068);
cursor: pointer;
}
@media (max-width: 768px){
input[type="submit"]{
width: 100%;
}
}
#treeview .parent {
position: relative;
}
#treeview .parent > ul {
display: none;
}
#sticks {
margin: 0vh 1vw;
margin-bottom: 1vh;
padding: 1vh 3vw;
display: flex;
flex-direction: column;
border: 2px solid rgba(255, 255, 255, 0.11);
border-radius: 20px;
background-color: #161b22;
align-items: center;
}
#sticks.stick {
position: sticky;
top: 0;
z-index: 10000;
}
</style>
<script>
function s_validate() {
if ($("input[name^='filenode_']:checked").length == 0) {
alert("Select one file at least!");
return false;
}
}
</script>
</head>
<body>
<!--© Designed and coded by @bipuldey19-Telegram-->
<header>
<div class="brand">
<img
src="https://graph.org/file/1a6ad157f55bc42b548df.png"
alt="logo"
/>
<a href="https://t.me/krn2701">
<h2 class="name">Qbittorrent Selection</h2>
</a>
</div>
<div class="social">
<a href="https://github.com/weebzone/WZML"><i class="fab fa-github"></i></a>
<a href="https://t.me/krn2701"><i class="fab fa-telegram"></i></a>
</div>
</header>
<div id="sticks">
<h4>Selected files: <b id="checked_files">0</b> of <b id="total_files">0</b></h4>
<h4>Selected files size: <b id="checked_size">0</b> of <b id="total_size">0</b></h4>
</div>
<section>
<form action="{form_url}" onsubmit="return s_validate()" method="POST">
{My_content}
<input type="submit" name="Select these files ;)">
</form>
</section>
<script>
$(document).ready(function () {
docready();
var tags = $("li").filter(function () {
return $(this).find("ul").length !== 0;
});
tags.each(function () {
$(this).addClass("parent");
});
$("body").find("ul:first-child").attr("id", "treeview");
$(".parent").prepend("<span>▶</span>");
$("span").click(function (e) {
e.stopPropagation();
e.stopImmediatePropagation();
$(this).parent( ".parent" ).find(">ul").toggle("slow");
if ($(this).hasClass("active")) $(this).removeClass("active");
else $(this).addClass("active");
});
});
if(document.getElementsByTagName("ul").length >= 10){
var labels = document.querySelectorAll("label");
//Shorting the file/folder names
labels.forEach(function (label) {
if (label.innerText.toString().split(" ").length >= 9) {
let FirstPart = label.innerText
.toString()
.split(" ")
.slice(0, 5)
.join(" ");
let SecondPart = label.innerText
.toString()
.split(" ")
.splice(-5)
.join(" ");
label.innerText = `${FirstPart}... ${SecondPart}`;
}
if (label.innerText.toString().split(".").length >= 9) {
let first = label.innerText
.toString()
.split(".")
.slice(0, 5)
.join(" ");
let second = label.innerText
.toString()
.split(".")
.splice(-5)
.join(".");
label.innerText = `${first}... ${second}`;
}
});
}
</script>
<script>
$('input[type="checkbox"]').change(function(e) {
var checked = $(this).prop("checked"),
container = $(this).parent(),
siblings = container.siblings();
/*
$(this).attr('value', function(index, attr){
return attr == 'yes' ? 'noo' : 'yes';
});
*/
container.find('input[type="checkbox"]').prop({
indeterminate: false,
checked: checked
});
function checkSiblings(el) {
var parent = el.parent().parent(),
all = true;
el.siblings().each(function() {
let returnValue = all = ($(this).children('input[type="checkbox"]').prop("checked") === checked);
return returnValue;
});
if (all && checked) {
parent.children('input[type="checkbox"]').prop({
indeterminate: false,
checked: checked
});
checkSiblings(parent);
} else if (all && !checked) {
parent.children('input[type="checkbox"]').prop("checked", checked);
parent.children('input[type="checkbox"]').prop("indeterminate", (parent.find('input[type="checkbox"]:checked').length > 0));
checkSiblings(parent);
} else {
el.parents("li").children('input[type="checkbox"]').prop({
indeterminate: true,
checked: false
});
}
}
checkSiblings(container);
});
</script>
<script>
function docready () {
$("label[for^='filenode_']").css("cursor", "pointer");
$("label[for^='filenode_']").click(function () {
$(this).prev().click();
});
checked_size();
checkingfiles();
var total_files = $("label[for^='filenode_']").length;
$("#total_files").text(total_files);
var total_size = 0;
var ffilenode = $("label[for^='filenode_']");
ffilenode.each(function () {
var size = parseFloat($(this).data("size"));
total_size += size;
$(this).append(" - " + humanFileSize(size));
});
$("#total_size").text(humanFileSize(total_size));
};
function checked_size() {
var checked_size = 0;
var checkedboxes = $("input[name^='filenode_']:checked");
checkedboxes.each(function () {
var size = parseFloat($(this).data("size"));
checked_size += size;
});
$("#checked_size").text(humanFileSize(checked_size));
}
function checkingfiles() {
var checked_files = $("input[name^='filenode_']:checked").length;
$("#checked_files").text(checked_files);
}
$("input[name^='foldernode_']").change(function () {
checkingfiles();
checked_size();
});
$("input[name^='filenode_']").change(function () {
checkingfiles();
checked_size();
});
function humanFileSize(size) {
var i = -1;
var byteUnits = [' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB'];
do {
size = size / 1024;
i++;
} while (size > 1024);
return Math.max(size, 0).toFixed(1) + byteUnits[i];
}
function sticking() {
var window_top = $(window).scrollTop();
var div_top = $('.brand').offset().top;
if (window_top > div_top) {
$('#sticks').addClass('stick');
} else {
$('#sticks').removeClass('stick');
}
}
$(function () {
$(window).scroll(sticking);
sticking();
});
</script>
</body>
</html>
"""
code_page = """
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Torrent Code Checker</title>
<link rel="icon" href="https://graph.org/file/1a6ad157f55bc42b548df.png" type="image/jpg">
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Ubuntu:ital,wght@0,300;0,400;0,500;0,700;1,300;1,400;1,500;1,700&display=swap"
rel="stylesheet"
/>
<link
rel="stylesheet"
href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css"
integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p"
crossorigin="anonymous"
/>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Ubuntu", sans-serif;
list-style: none;
text-decoration: none;
color: white;
}
body{
background-color: #0D1117;
}
header{
margin: 3vh 1vw;
padding: 0.5rem 1rem 0.5rem 1rem;
display: flex;
align-items: center;
justify-content: space-between;
border-bottom: #161B22;
border-radius: 30px;
background-color: #161B22;
border: 2px solid rgba(255, 255, 255, 0.11);
}
header:hover, section:hover{
box-shadow: 0px 0px 15px black;
}
.brand{
display: flex;
align-items: center;
}
img{
width: 2.5rem;
height: 2.5rem;
border: 2px solid black;
border-radius: 50%;
}
.name{
color: white;
margin-left: 1vw;
font-size: 1.5rem;
}
.intro{
text-align: center;
margin-bottom: 2vh;
margin-top: 1vh;
}
.social a{
font-size: 1.5rem;
color: white;
padding-left: 1vw;
}
.social a:hover, .brand:hover{
filter: invert(0.3);
}
section{
margin: 0vh 1vw;
margin-bottom: 10vh;
padding: 1vh 3vw;
display: flex;
flex-direction: column;
border: 2px solid rgba(255, 255, 255, 0.11);
border-radius: 20px;
background-color: #161B22 ;
color: white;
}
section form{
display: flex;
margin-left: auto;
margin-right: auto;
flex-direction: column;
}
section div{
background-color: #0D1117;
border-radius: 20px;
max-width: fit-content;
padding: 0.7rem;
margin-top: 2vh;
}
section label{
font-size: larger;
font-weight: 500;
margin: 0 0 0.5vh 1.5vw;
display: block;
}
section input[type="text"]{
border-radius: 20px;
outline: none;
width: 50vw;
height: 4vh;
padding: 1rem;
margin: 0.5vh;
border: 2px solid rgba(255, 255, 255, 0.11);
background-color: #3e475531;
box-shadow: inset 0px 0px 10px black;
}
section input[type="text"]:focus{
border-color: rgba(255, 255, 255, 0.404);
}
section button{
border-radius: 20px;
margin-top: 1vh;
width: 100%;
height: 5.5vh;
border: 2px solid rgba(255, 255, 255, 0.11);
background-color: #0D1117;
color: white;
font-size: 16px;
font-weight: 500;
cursor: pointer;
transition: background-color 200ms ease;
}
section button:hover, section button:focus{
background-color: rgba(255, 255, 255, 0.068);
}
section span{
display: block;
font-size: x-small;
margin: 1vh;
font-weight: 100;
font-style: italic;
margin-left: 23%;
margin-right: auto;
margin-bottom: 2vh;
}
@media (max-width: 768px) {
section form{
flex-direction: column;
width: 90vw;
}
section div{
max-width: 100%;
margin-bottom: 1vh;
}
section label{
margin-left: 3vw;
margin-top: 1vh;
}
section input[type="text"]{
width: calc(100% - 0.3rem);
}
section button{
width: 100%;
height: 5vh;
display: block;
margin-left: auto;
margin-right: auto;
}
section span{
margin-left: 5%;
}
}
</style>
</head>
<body>
<!--© Designed and coded by @bipuldey19-Telegram-->
<header>
<div class="brand">
<img
src="https://graph.org/file/1a6ad157f55bc42b548df.png"
alt="logo"
/>
<a href="https://t.me/krn2701">
<h2 class="name">Qbittorrent Selection</h2>
</a>
</div>
<div class="social">
<a href="https://github.com/weebzone/WZML"><i class="fab fa-github"></i></a>
<a href="https://t.me/krn2701"><i class="fab fa-telegram"></i></a>
</div>
</header>
<section>
<form action="{form_url}">
<div>
<label for="pin_code">Pin Code :</label>
<input
type="text"
name="pin_code"
placeholder="Enter the code that you have got from Telegram to access the Torrent"
/>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<span
>* Dont mess around. Your download will get messed up.</
>
</section>
</body>
</html>
"""
def re_verfiy(paused, resumed, client, hash_id):
paused = paused.strip()
resumed = resumed.strip()
if paused:
paused = paused.split("|")
if resumed:
resumed = resumed.split("|")
k = 0
while True:
res = client.torrents_files(torrent_hash=hash_id)
verify = True
for i in res:
if str(i.id) in paused and i.priority != 0:
verify = False
break
if str(i.id) in resumed and i.priority == 0:
verify = False
break
if verify:
break
LOGGER.info("Reverification Failed! Correcting stuff...")
client.auth_log_out()
sleep(1)
client = qbClient(host="localhost", port="8090")
try:
client.torrents_file_priority(torrent_hash=hash_id, file_ids=paused, priority=0)
except NotFound404Error:
raise NotFound404Error
except Exception as e:
LOGGER.error(f"{e} Errored in reverification paused!")
try:
client.torrents_file_priority(torrent_hash=hash_id, file_ids=resumed, priority=1)
except NotFound404Error:
raise NotFound404Error
except Exception as e:
LOGGER.error(f"{e} Errored in reverification resumed!")
k += 1
if k > 5:
return False
LOGGER.info(f"Verified! Hash: {hash_id}")
return True
@app.route('/app/files/<string:id_>', methods=['GET'])
def list_torrent_contents(id_):
if "pin_code" not in request.args.keys():
return code_page.replace("{form_url}", f"/app/files/{id_}")
pincode = ""
for nbr in id_:
if nbr.isdigit():
pincode += str(nbr)
if len(pincode) == 4:
break
if request.args["pin_code"] != pincode:
return "<h1>Incorrect pin code</h1>"
if len(id_) > 20:
client = qbClient(host="localhost", port="8090")
res = client.torrents_files(torrent_hash=id_)
cont = make_tree(res)
client.auth_log_out()
else:
res = aria2.client.get_files(id_)
cont = make_tree(res, True)
return page.replace("{My_content}", cont[0]).replace("{form_url}", f"/app/files/{id_}?pin_code={pincode}")
@app.route('/app/files/<string:id_>', methods=['POST'])
def set_priority(id_):
data = dict(request.form)
resume = ""
if len(id_) > 20:
pause = ""
for i, value in data.items():
if "filenode" in i:
node_no = i.split("_")[-1]
if value == "on":
resume += f"{node_no}|"
else:
pause += f"{node_no}|"
pause = pause.strip("|")
resume = resume.strip("|")
client = qbClient(host="localhost", port="8090")
try:
client.torrents_file_priority(torrent_hash=id_, file_ids=pause, priority=0)
except NotFound404Error:
raise NotFound404Error
except Exception as e:
LOGGER.error(f"{e} Errored in paused")
try:
client.torrents_file_priority(torrent_hash=id_, file_ids=resume, priority=1)
except NotFound404Error:
raise NotFound404Error
except Exception as e:
LOGGER.error(f"{e} Errored in resumed")
sleep(1)
if not re_verfiy(pause, resume, client, id_):
LOGGER.error(f"Verification Failed! Hash: {id_}")
client.auth_log_out()
else:
for i, value in data.items():
if "filenode" in i and value == "on":
node_no = i.split("_")[-1]
resume += f'{node_no},'
resume = resume.strip(",")
res = aria2.client.change_option(id_, {'select-file': resume})
if res == "OK":
LOGGER.info(f"Verified! Gid: {id_}")
else:
LOGGER.info(f"Verification Failed! Report! Gid: {id_}")
return list_torrent_contents(id_)
@app.route('/')
def homepage():
return "<h1>See mirror-with-weeb <a href='https://github.com/weebzone/WZML'>@GitHub</a> By <a href='https://github.com/weebzone'>Code With Weeb</a></h1>"
@app.errorhandler(Exception)
def page_not_found(e):
return f"<h1>404: Torrent not found! Mostly wrong input. <br><br>Error: {e}</h2>", 404
if __name__ == "__main__":
app.run()