-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.py
108 lines (89 loc) · 3.42 KB
/
services.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
import json
import subprocess
import datetime
import time
import random
import requests
from bs4 import BeautifulSoup as bs
#External Plugins
import plugins.nyaa.nyaascraper as nyaa
import plugins.animetosho.animetoshoapi as tosho
active = {} #keeping track of active user's services
keepalivehistory = {} #keeping track of active online users
def scrape(payload):
global active,keepalivehistory
uid = payload["uid"]
if "addr" not in active[uid].keys():
try:
port = active[uid]["port"]
except KeyError:
return ["Waiting","Null","Null"]
time.sleep(3)
resp = None
while resp==None:
try:
resp = requests.get(f"http://localhost:{port}")
except:
pass
if uid not in active.keys():
print("UID Not Found Cancelling scrape") #avoid zombie mode if the main process is terminated before this thread
return None
soup = bs(resp.text, 'html.parser')
li = soup.find_all("a",href=True)
li = [i["href"] for i in li]
print(li)
active[uid]["addr"] = li
active[uid]["scraped"] = True
return ["ready",li,port]
else:
return ["ready",active[uid]["addr"],active[uid]["port"]]
def ready_subs(payload):
global active
extract_path = tosho.get_subs(int(payload["nyaaid"]))
if extract_path == -1:
active[payload["uid"]]["subtitles"] = "Not Available"
else:
active[payload["uid"]]["subtitles"]=extract_path
print(active[payload["uid"]]["subtitles"])
def keepalivecheck():
global keepalivehistory
time.sleep(15)
if keepalivehistory:
print(keepalivehistory)
torem = []
for i in keepalivehistory.keys():
last_ping = keepalivehistory[i]
if ((datetime.datetime.now() - last_ping).seconds) > 15:
print(f"UID : {i} is stale , killing process")
print(active)
if i in active.keys():
active[i]["process"].kill()
del(active[i])
torem.append(i)
else:
print("UID Not in active sessions")
if torem:
for i in torem:
del(keepalivehistory[i])
def getport():
while True:
port = random.randint(8081,9999)
activeports = [i["port"] if i != None else None for i in active.values()]
print(activeports)
if port not in activeports:
return port
def player(uid,magnet,configs,webplayer_args):
global active
print(active)
port = getport()
print("Port :",port)
if active[uid] is None:
print("Not Running")
else:
active[uid]["process"].kill()
#print("Command :",f"{configs['node_path']} {configs['webtorrent_path']} \"{magnet}\" {webplayer_args} -p {port}")
process = subprocess.Popen(f"{configs['node_path']} {configs['webtorrent_path']} \"{magnet}\" {webplayer_args} -p {port}",shell=configs["shell"]
,stdout=subprocess.PIPE,stderr=subprocess.DEVNULL,bufsize=0)
active[uid] = {"port":port,"magnet":magnet,"process":process,"scraped":"Not Started","subtitles":"Not Started"}
print(active[uid])
#stdout=subprocess.PIPE