This repository was archived by the owner on Jan 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplexlistenandwol.py
139 lines (125 loc) · 4.72 KB
/
plexlistenandwol.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
#!/usr/bin/env python2
# https://github.com/Ali1/Plex-Listen-and-WOL-for-Raspbian/
# Ali: The script opens port 32400 acting as Plex for most of the day and starts Plex once a day just to keep the raspberry pi server activated on the Plex account
# sudo systemctl status plexlistenandwol.service to check status once running
import os
import datetime
import subprocess
import logging
from logging.handlers import RotatingFileHandler
from logging import handlers
import socket
import sys
import argparse
from subprocess import Popen, PIPE
from wol import * # ali
import time
import ConfigParser
class Object(object):
pass # for conf
config = ConfigParser.ConfigParser()
config.read('config.ini')
if not config.has_option('DEFAULT', 'RealPlexServerMac'):
exit('No config file, ensure you have renamed config.ini.new')
print(config.get('DEFAULT', 'RealPlexServerMac'));
log = logging.getLogger('')
log.setLevel(logging.DEBUG)
format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
ch = logging.StreamHandler(sys.stdout)
ch.setFormatter(format)
log.addHandler(ch)
fh = handlers.RotatingFileHandler('app.log', maxBytes=(1048576*5), backupCount=7)
fh.setFormatter(format)
log.addHandler(fh)
def main():
logging.info('Starting app');
# Check for root
if os.geteuid():
exit('[-] Please run as root')
# creating a socket object
s = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
# get local Host machine name
host = ''
port = 32400
logging.info("Attempting to bind port");
try:
s.bind((host, port))
except socket.error as e:
if e.errno == 98:
logging.warning("Port is already in use. Shutting Plex and trying in 45 secs")
os.system("sudo service plexmediaserver stop")
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
time.sleep(45)
logging.info("Now try again")
try:
s.bind((host, port))
except:
logging.error("Did not work, trying to kill the process")
os.system("pid=$(lsof -i:32400 -t); kill -TERM $pid || kill -KILL $pid")
time.sleep(45)
logging.info("Ok, last try")
s.bind((host, port))
else:
raise
s.settimeout(45);
s.listen(1) # up to 2 connections
logging.info("Binded successfully");
first_run=1 # to prevent the Plex shutdown process from occurring right away
while True:
now = datetime.datetime.now()
if now.hour==9 and now.minute==49 and first_run==0:
logging.info("shutting sockets down to prepare for plex");
try:
s.shutdown(socket.SHUT_RDWR)
except:
pass
s.close()
time.sleep(60)
logging.info("starting plex for 60 seconds");
os.system("sudo service plexmediaserver start")
time.sleep(60)
logging.info("stopping plex");
os.system("sudo service plexmediaserver stop")
time.sleep(60)
logging.info("re-opening sockets")
s = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
try:
s.bind((host, port))
except:
logging.error("Did not work, trying to kill processes hogging port 32400")
os.system("pid=$(lsof -i:32400 -t); kill -TERM $pid || kill -KILL $pid")
time.sleep(45)
logging.info("Ok, last try")
s.bind((host, port))
s.settimeout(45);
s.listen(1) # up to 2 connections
first_run=0
try:
clientSocket, addr = s.accept()
except socket.timeout:
pass
except:
raise
else:
logging.info("got a connection from " + str(addr))
clientSocket.close()
wake_on_lan(config.get('DEFAULT', 'RealPlexServerMac'))
time.sleep(1)
wake_on_lan(config.get('DEFAULT', 'RealPlexServerMac'))
time.sleep(1)
wake_on_lan(config.get('DEFAULT', 'RealPlexServerMac'))
time.sleep(1)
wake_on_lan(config.get('DEFAULT', 'RealPlexServerMac'))
time.sleep(1)
wake_on_lan(config.get('DEFAULT', 'RealPlexServerMac'))
time.sleep(1)
wake_on_lan(config.get('DEFAULT', 'RealPlexServerMac'))
time.sleep(1)
logging.info('sent 6 WOLs to ' + config.get('DEFAULT', 'RealPlexServerMac') + '. Now sleeping');
time.sleep(10)
logging.info("dying")
if __name__ == "__main__":
main()