Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
# Conflicts:
#	bazarr/get_providers.py
#	libs/subliminal_patch/providers/titrari.py
  • Loading branch information
morpheus65535 committed Feb 1, 2020
2 parents a4a44b1 + dd4d24f commit f81f7ed
Show file tree
Hide file tree
Showing 26 changed files with 1,308 additions and 361 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ If you need something that is not already part of Bazarr, feel free to create a
* Argenteam
* Assrt
* BetaSeries
* BSPlayer
* GreekSubtitles
* Hosszupuska
* LegendasDivx
* LegendasTV
* Napiprojekt
* Napisy24
Expand Down
115 changes: 108 additions & 7 deletions bazarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import sys
import platform
import re
import signal

from bazarr.get_args import args

Expand Down Expand Up @@ -39,15 +40,97 @@ def check_python_version():
dir_name = os.path.dirname(__file__)


def start_bazarr():
class ProcessRegistry:

def register(self, process):
pass

def unregister(self, process):
pass


class DaemonStatus(ProcessRegistry):

def __init__(self):
self.__should_stop = False
self.__processes = set()

def register(self, process):
self.__processes.add(process)

def unregister(self, process):
self.__processes.remove(process)

'''
Waits all the provided processes for the specified amount of time in seconds.
'''
@staticmethod
def __wait_for_processes(processes, timeout):
reference_ts = time.time()
elapsed = 0
remaining_processes = list(processes)
while elapsed < timeout and len(remaining_processes) > 0:
remaining_time = timeout - elapsed
for ep in list(remaining_processes):
if ep.poll() is not None:
remaining_processes.remove(ep)
else:
if remaining_time > 0:
if PY3:
try:
ep.wait(remaining_time)
remaining_processes.remove(ep)
except sp.TimeoutExpired:
pass
else:
'''
In python 2 there is no such thing as some mechanism to wait with a timeout.
'''
time.sleep(1)
elapsed = time.time() - reference_ts
remaining_time = timeout - elapsed
return remaining_processes

'''
Sends to every single of the specified processes the given signal and (if live_processes is not None) append to it processes which are still alive.
'''
@staticmethod
def __send_signal(processes, signal_no, live_processes=None):
for ep in processes:
if ep.poll() is None:
if live_processes is not None:
live_processes.append(ep)
try:
ep.send_signal(signal_no)
except Exception as e:
print('Failed sending signal %s to process %s because of an unexpected error: %s' % (signal_no, ep.pid, e))
return live_processes

'''
Flags this instance as should stop and terminates as smoothly as possible children processes.
'''
def stop(self):
self.__should_stop = True
live_processes = DaemonStatus.__send_signal(self.__processes, signal.SIGINT, list())
live_processes = DaemonStatus.__wait_for_processes(live_processes, 120)
DaemonStatus.__send_signal(live_processes, signal.SIGTERM)

def should_stop(self):
return self.__should_stop


def start_bazarr(process_registry=ProcessRegistry()):
script = [sys.executable, "-u", os.path.normcase(os.path.join(dir_name, 'bazarr', 'main.py'))] + sys.argv[1:]

ep = sp.Popen(script, stdout=sp.PIPE, stderr=sp.STDOUT, stdin=sp.PIPE)
process_registry.register(ep)
print("Bazarr starting...")
try:
while True:
line = ep.stdout.readline()
if line == '' or not line:
# Process ended so let's unregister it
process_registry.unregister(ep)
break
if PY3:
sys.stdout.buffer.write(line)
Expand All @@ -73,7 +156,7 @@ def start_bazarr():
pass


def daemon():
def daemon(bazarr_runner = lambda: start_bazarr()):
if os.path.exists(stopfile):
try:
os.remove(stopfile)
Expand All @@ -89,12 +172,30 @@ def daemon():
except:
print('Unable to delete restart file.')
else:
start_bazarr()
bazarr_runner()


bazarr_runner = lambda: start_bazarr()

should_stop = lambda: False

if PY3:
daemonStatus = DaemonStatus()

def shutdown():
# indicates that everything should stop
daemonStatus.stop()
# emulate a Ctrl C command on itself (bypasses the signal thing but, then, emulates the "Ctrl+C break")
os.kill(os.getpid(), signal.SIGINT)

signal.signal(signal.SIGTERM, lambda signal_no, frame: shutdown())

should_stop = lambda: daemonStatus.should_stop()
bazarr_runner = lambda: start_bazarr(daemonStatus)

start_bazarr()
bazarr_runner()

# Keep the script running forever.
while True:
daemon()
# Keep the script running forever until stop is requested through term or keyboard interrupt
while not should_stop():
daemon(bazarr_runner)
time.sleep(1)
4 changes: 4 additions & 0 deletions bazarr/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@
'password': '',
'random_agents': 'True'
},
'legendasdivx': {
'username': '',
'password': ''
},
'legendastv': {
'username': '',
'password': ''
Expand Down
5 changes: 4 additions & 1 deletion bazarr/get_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
}
}

PROVIDERS_FORCED_OFF = ["addic7ed", "tvsubtitles", "legendastv", "napiprojekt", "shooter", "hosszupuska",
PROVIDERS_FORCED_OFF = ["addic7ed", "tvsubtitles", "legendasdivx", "legendastv", "napiprojekt", "shooter", "hosszupuska",
"supersubtitles", "titlovi", "argenteam", "assrt", "subscene"]

throttle_count = {}
Expand Down Expand Up @@ -114,6 +114,9 @@ def get_providers_auth():
'password': settings.subscene.password,
'only_foreign': False, # fixme
},
'legendasdivx': {'username': settings.legendasdivx.username,
'password': settings.legendasdivx.password,
},
'legendastv': {'username': settings.legendastv.username,
'password': settings.legendastv.password,
},
Expand Down
Loading

0 comments on commit f81f7ed

Please sign in to comment.