From efdd29a212cd685a9d4c3e617e094f2c1f6ddbaf Mon Sep 17 00:00:00 2001 From: beaston02 Date: Mon, 29 May 2017 21:26:18 -0700 Subject: [PATCH] Update CAM4Recorder.py --- CAM4Recorder.py | 65 ++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/CAM4Recorder.py b/CAM4Recorder.py index b77cbd4..7181167 100644 --- a/CAM4Recorder.py +++ b/CAM4Recorder.py @@ -30,37 +30,40 @@ i = 1 def startRecording(model): - req = urllib.request.Request('https://www.cam4.com/' + model) - req.add_header('UserAgent', UserAgent) - resp = urllib.request.urlopen(req) - resp = resp.read().decode().splitlines() - videoPlayUrl = "" - videoAppUrl = "" - for line in resp: - if "videoPlayUrl" in line: - for part in line.split("&"): - if "videoPlayUrl" in part and videoPlayUrl == "": - videoPlayUrl = part[13:] - elif "videoAppUrl" in part and videoAppUrl == "": - videoAppUrl = part.split("//")[1] - session = Livestreamer() - session.set_option('http-headers', "referer=https://www.cam4.com/{}".format(model)) - streams = session.streams("hlsvariant://https://{}/amlst:{}_aac/playlist.m3u8?referer=www.cam4.com×tamp={}\" best" - .format(videoAppUrl, videoPlayUrl, str(int(time.time() * 1000)))) - stream = streams["best"] - fd = stream.open() - ts = time.time() - st = datetime.datetime.fromtimestamp(ts).strftime("%Y.%m.%d_%H.%M.%S") - if not os.path.exists("{path}/{model}".format(path=save_directory, model=model)): - os.makedirs("{path}/{model}".format(path=save_directory, model=model)) - with open("{path}/{model}/{st}_{model}.mp4".format(path=save_directory, model=model, - st=st), 'wb') as f: - recording.append(model.lower()) - while True: - try: - data = fd.read(1024) - f.write(data) - except: + try: + req = urllib.request.Request('https://www.cam4.com/' + model) + req.add_header('UserAgent', UserAgent) + resp = urllib.request.urlopen(req) + resp = resp.read().decode().splitlines() + videoPlayUrl = "" + videoAppUrl = "" + for line in resp: + if "videoPlayUrl" in line: + for part in line.split("&"): + if "videoPlayUrl" in part and videoPlayUrl == "": + videoPlayUrl = part[13:] + elif "videoAppUrl" in part and videoAppUrl == "": + videoAppUrl = part.split("//")[1] + session = Livestreamer() + session.set_option('http-headers', "referer=https://www.cam4.com/{}".format(model)) + streams = session.streams("hlsvariant://https://{}/amlst:{}_aac/playlist.m3u8?referer=www.cam4.com×tamp={}\" best" + .format(videoAppUrl, videoPlayUrl, str(int(time.time() * 1000)))) + stream = streams["best"] + fd = stream.open() + ts = time.time() + st = datetime.datetime.fromtimestamp(ts).strftime("%Y.%m.%d_%H.%M.%S") + if not os.path.exists("{path}/{model}".format(path=save_directory, model=model)): + os.makedirs("{path}/{model}".format(path=save_directory, model=model)) + with open("{path}/{model}/{st}_{model}.mp4".format(path=save_directory, model=model, + st=st), 'wb') as f: + recording.append(model.lower()) + while True: + try: + data = fd.read(1024) + f.write(data) + except: + pass + if model in recording: recording.remove(model)