2020-08-27 21:48:55 +00:00
|
|
|
#!/usr/sbin/python
|
2019-01-07 07:48:10 +00:00
|
|
|
|
2020-08-27 21:48:55 +00:00
|
|
|
import os, sys, json, struct, threading, subprocess
|
2019-01-07 07:48:10 +00:00
|
|
|
|
2020-08-27 21:48:55 +00:00
|
|
|
|
|
|
|
DLPATH = os.path.expanduser("~") + "/Downloads"
|
|
|
|
MAJOR_VERSION = sys.version_info[0]
|
|
|
|
|
|
|
|
|
|
|
|
def threaded(fn):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
threading.Thread(target=fn, args=args, kwargs=kwargs).start()
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
# Python 2.x and 3.x read version
|
2019-01-07 07:48:10 +00:00
|
|
|
# Read a message from stdin and decode it.
|
|
|
|
def getMessage():
|
2020-08-27 21:48:55 +00:00
|
|
|
message = None
|
|
|
|
rawLength = None
|
|
|
|
|
|
|
|
if MAJOR_VERSION == 2: # py2
|
|
|
|
rawLength = sys.stdin.read(4)
|
|
|
|
else: # py3
|
|
|
|
rawLength = sys.stdin.buffer.read(4)
|
|
|
|
|
2019-01-07 07:48:10 +00:00
|
|
|
if len(rawLength) == 0:
|
|
|
|
sys.exit(0)
|
2020-08-27 21:48:55 +00:00
|
|
|
|
2019-01-07 07:48:10 +00:00
|
|
|
messageLength = struct.unpack('@I', rawLength)[0]
|
2020-08-27 21:48:55 +00:00
|
|
|
if MAJOR_VERSION == 2: # py2
|
|
|
|
message = sys.stdin.read(messageLength)
|
|
|
|
else: # py3
|
|
|
|
message = sys.stdin.buffer.read(messageLength).decode("utf-8")
|
|
|
|
|
2019-01-07 07:48:10 +00:00
|
|
|
return json.loads(message)
|
|
|
|
|
2020-08-27 21:48:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
@threaded
|
|
|
|
def downloadVideo(command):
|
|
|
|
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
|
|
|
|
|
|
|
|
|
2019-01-07 07:48:10 +00:00
|
|
|
while True:
|
|
|
|
receivedMessage = getMessage()
|
|
|
|
if receivedMessage:
|
2020-08-27 21:48:55 +00:00
|
|
|
outputFile = DLPATH + "/%\(title\)s.%\(ext\)s "
|
|
|
|
command = "youtube-dl --no-playlist -o " + outputFile + receivedMessage;
|
|
|
|
downloadVideo(command)
|