-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·78 lines (59 loc) · 2.16 KB
/
main.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
#!/usr/bin/env python3
import logging
log = logging.getLogger(__name__)
import sqlalchemy as sqla
from configobj import ConfigObj
from distutils.util import strtobool
from UsenetAgent.ConfigLoader import ConfigLoader
from UsenetAgent.Database import Database
from UsenetAgent.SabnzbdHandler import SabnzbdHandler
from UsenetAgent.EmailHandler import EmailHandler
from UsenetAgent.HitnewsAgent import HitnewsAgent
from UsenetAgent.UsenetFarmAgent import UsernetFarmAgent
from UsenetAgent.HostConfig import HostConfig
def getUsenetAgent(cfg, hostname):
if hostname == "Hitnews":
email = EmailHandler(cfg)
return HitnewsAgent(email)
elif hostname == "UsenetFarm":
email = EmailHandler(cfg)
return UsernetFarmAgent(email)
else:
raise Exception("Unknown Host, no Agent available")
def getHost(cfg, hostname):
host = cfg['hosts'][hostname]
try:
host['ssl'] = strtobool(host['ssl'])
except:
pass
return HostConfig(**host)
def main():
cfg = ConfigLoader.load()
db = Database(sqla.create_engine(f"sqlite:///{cfg['agent']['database']}"))
sab = SabnzbdHandler(cfg)
for acc in db.findValidConnections():
host = getHost(cfg, acc.host)
acc.valid = acc.checkLogin(host)
db.session.add(acc)
db.session.commit()
accountsInUse = set()
for serverName in cfg['servers']:
try:
hostname = cfg['servers'][serverName]['host']
host = getHost(cfg, hostname)
availableAccounts = [acc for acc in db.findValidConnections(hostname)
if acc not in accountsInUse]
if len(availableAccounts) > 0:
account = availableAccounts[0]
else:
agent = getUsenetAgent(cfg, hostname)
account = agent.getTrial()
account.host = hostname
db.session.add(account)
db.session.commit()
accountsInUse.add(account)
sab.addServer(serverName, account, host)
except Exception:
log.exception(f'Account generation for {serverName} failed!')
if __name__ == '__main__':
main()