def retrievePiratePlayXML(apiBaseUrl, url, name, fileInfo, downloadAll, setQuality, bestQuality, checkDuration, verbose): xmlCode = "" trys = 0 if verbose: printInfo1("Using %s as source for getting XML" % apiBaseUrl) if verbose: printInfo2("Parsing the response from pirateplay.se API...") parseUrl = "%s/%s%s" % (apiBaseUrl, getStreamsXML, url) printInfo2("\nGetting streams for %s ..." % parseUrl) printScores() while True: trys += 1 if trys > maxTrys: onError(10, "Tried connecting %s times. Giving up..." % (trys - 1)) break if verbose: printInfo1("%s%s try" % (trys, numbering(trys, verbose))) try: piratePlayXML = urllib2.urlopen(parseUrl) except urllib2.HTTPError, e: onError(35, "HTTPError\n %s\n Trying again...\n" % str(e.code)) sleep(waitTime) except urllib2.URLError, e: onError(36, "URLError\n %s\n Trying again...\n" % str(e.reason)) sleep(waitTime)
def checkFileSize(line, verbose): printScores() expectedFileSize = int(line['subSize']) downloadedFileSize = os.path.getsize("%s.srt" % line['name'].rstrip()) printInfo1("Expected file size: %d B" % (expectedFileSize)) printInfo1("Downloaded file size: %d B" % (downloadedFileSize)) if expectedFileSize == 0: FileSizesMatch = True printWarning("Expected file size was 0\nSkipping checking...") else: if downloadedFileSize + 2 > expectedFileSize and downloadedFileSize - 2 < expectedFileSize: FileSizesMatch = True if verbose: printInfo1("File sizes match") else: FileSizesMatch = False if verbose: printWarning("File sizes does not match") return FileSizesMatch
def checkDurations(line, verbose): printScores() expectedDuration = round(float(line['duration']), 2) downloadedDuration = float(getInfo(line, '--Inform="General;%Duration%"', verbose)) / 1000 printInfo1("Expected duration: %d s (%s)" % (expectedDuration, str(datetime.timedelta(seconds=expectedDuration)))) printInfo1("Downloaded duration: %d s (%s)" % (downloadedDuration, str(datetime.timedelta(seconds=downloadedDuration)))) if expectedDuration == 0: durationsMatch = True printWarning("Expected duration was 0\nSkipping checking...") else: if downloadedDuration + 2 > expectedDuration and downloadedDuration - 2 < expectedDuration: durationsMatch = True if verbose: printInfo1("Durations match") else: durationsMatch = False if verbose: printWarning("Durations does not match") return durationsMatch
def finish(downloads, keepOld, reDownload, checkDuration, listOnly, convertTo, bashOutFile, verbose): shouldBeDeleted = [] if not listOnly: if downloads: infoDownloaded = getVideos(downloads, keepOld, reDownload, checkDuration, verbose) if convertTo: if verbose: printInfo2("Converting downloads...") convertDownloads(downloads, convertTo, verbose) else: infoDownloaded = "" onError(17, "Could not find any streams to download") else: infoDownloaded = "" printInfo1("\nListing only") printScores() if bashOutFile: if continueWithProcess(bashOutFile, bashSuffix, True, False, "Will redownload\n", "Keeping old file. No download\n", verbose): bashFile = open("%s.%s" % (bashOutFile, bashSuffix), "w") bashFile.write("#!/bin/bash\n\n") if downloads: printInfo1("These files would have been downloaded:") for line in downloads: # print line printInfo1("\nVideo name: %s.%s" % (line['name'].rstrip(), line['suffix'])) printInfo1("Video quality: %s" % line['quality']) printInfo1("Video address: %s" % line['address']) if line['subs']: printInfo1("Subtitles name: %s.srt" % line['name'].rstrip()) printInfo1("Subtitles address: %s" % line['subs']) else: printInfo1("No subtitles found") print "Duration: %s s" % line['duration'] if bashOutFile: if line['address'].startswith("http"): cmd = ffmpegDownloadCommand(line, verbose) bashFile.write("%s\n\n" % cmd) elif line['address'].startswith("rtmp"): cmd = rtmpdumpDownloadCommand(line, verbose) bashFile.write("%s\n\n" % cmd) if line['subs']: cmd = wgetDownloadCommand(line, verbose) bashFile.write("%s\n\n" % cmd) if bashOutFile: bashFile.close() st = os.stat("%s.sh" % bashOutFile) os.chmod("%s.sh" % bashOutFile, st.st_mode | stat.S_IEXEC) else: printWarning("Could not find anything that would have been downloaded") for line in infoDownloaded: printInfo1("\nVideo: %s" % line['videoName']) printScores() if line['expectedDuration'] != "0.000": printInfo1("Expected duration: %s" % (str(datetime.timedelta(seconds=round(float(line['expectedDuration']), 2))))) if verbose: printInfo1("Duration: %s ms" % line['duration']) printInfo1("Duration: %s" % line['durationFormatted']) if line['videoDlComment'] == dlCommentError: printError(line['videoDlComment']) shouldBeDeleted.append(line['videoName']) else: printInfo2(line['videoDlComment']) if verbose: printInfo1("Expected duration: %s s" % line['expectedDuration']) printInfo1("Actual duration: %s s" % line['duration']) if not compareDurations(float(line['expectedDuration']), float(line['duration']) / 1000, verbose): printError("Durations does not match") shouldBeDeleted.append(line['videoName']) if verbose: printInfo1("File size: %s b" % line['fileSize']) printInfo1("File size: %s" % line['fileSizeMeasure']) if verbose: printInfo1("Overall bit rate: %s bps" % line['overallBitRate']) printInfo1("Overall bit rate: %s" % line['overallBitRateMeasure']) print if verbose: printInfo1("Video format: %s" % line['videoFormat']) printInfo1("Video codec ID: %s" % line['videoCodecId']) printInfo1("Video bit rate: %s bps" % line['videoBitRate']) printInfo1("Video bit rate: %s" % line['videoBitRateMeasure']) printInfo1("Width: %s px" % line['width']) printInfo1("Height: %s px" % line['height']) printInfo1("Frame rate: %s fps" % line['frameRate']) if verbose: printInfo1("Frame count: %s" % line['frameCount']) print printInfo1("Audio format: %s" % line['audioFormat']) printInfo1("Audio codec ID: %s" % line['audioCodecId']) printInfo1("Audio bit rate: %s bps" % line['audioBitRate']) printInfo1("Audio bit rate: %s" % line['audioBitRateMeasure']) if line['subLines'] != 'na': printInfo1("\nSubtitles: %s" % line['subName']) printScores() if line['subDlComment'] == dlCommentError: printError(line['subDlComment']) shouldBeDeleted.append(line['subName']) else: printInfo2(line['subDlComment']) if line['expectedSubSize'] != "0": printInfo1("Expected file size: %s B" % line['expectedSubSize']) printInfo1("File size: %s B" % line['subSize']) printInfo1("Number of lines: %s" % line['subLines']) else: printWarning("\nNo subtitles downloaded") if shouldBeDeleted: print printError("\nThese files should be deleted and re downloaded") printScores() for line in shouldBeDeleted: printWarning(line) print
def getVideos(downloads, keepOld, reDownload, checkDuration, verbose): oldReDownload = reDownload printInfo2("\nStarting downloads") for line in downloads: trys = 0 videoCmd, subCmd = getDownloadCommands(line, verbose) while True: trys += 1 if trys > maxTrys: onError(29, "Tried to download video %s times\nSkipping..." % (trys - 1)) videoComment = dlCommentError break print printInfo2("Downloading stream to %s.%s ..." % (line['name'].rstrip(), line['suffix'])) expectedDuration = round(float(line['duration']), 2) printInfo1("Expected duration: %s" % str(datetime.timedelta(seconds=expectedDuration))) printInfo1("%s%s try" % (trys, numbering(trys, verbose))) printScores() if continueWithProcess(line['name'].rstrip(), line['suffix'], keepOld, reDownload, "Will redownload\n", "Keeping old file. No download\n", verbose): if verbose: printInfo2("Executing download command...") print videoCmd exitCode = runProcess(videoCmd, verbose) if exitCode != 0: printScores() onError(30, "Failed. Process exited on %s" % exitCode) printInfo2("Trying again...") reDownload = True else: if checkDuration and float(line['duration']) > 0: durationOK = checkDurations(line, verbose) else: if verbose: printWarning("Not checking duration") durationOK = True if os.path.isfile("%s.%s" % (line['name'].rstrip(), line['suffix'])) and durationOK: printScores() printInfo1("Finished downloading video") setPerms("%s.%s" % (line['name'].rstrip(), line['suffix']), verbose) reDownload = oldReDownload videoComment = dlCommentSuccess break else: printScores() if not durationOK: onError(46, "Durations does not match") printInfo2("Trying again") else: onError(31, "Failed. Video file does not exist") printInfo2("Trying again...") reDownload = True else: videoComment = dlCommentExist break if subCmd: trys = 0 oldReDownload = reDownload while True: trys += 1 if trys > maxTrys: onError(32, "Tried to download subtitles %s times\nSkipping..." % (trys - 1)) subComment = dlCommentError break print printInfo2("Downloading subtitles %s.srt ..." % line['name'].rstrip()) if verbose: printInfo1("from %s" % line['subs']) printInfo1("%s%s try" % (trys, numbering(trys, verbose))) printScores() if continueWithProcess(line['name'].rstrip(), "srt", keepOld, reDownload, "Will redownload\n", "Keeping old file. No download\n", verbose): if verbose: printInfo2("Downloading file...") print line['subs'] print videoCmd result = downloadFile(line['subs'], "%s.%s" % (line['name'].rstrip(), "srt"), verbose) if not result: printScores() onError(33, "Failed to download subtitles") printInfo2("Trying again...") reDownload = True else: fileSizeOK = checkFileSize(line, verbose) if os.path.isfile("%s.srt" % line['name'].rstrip()) and fileSizeOK: printScores() printInfo1("Finished downloading subtitles") setPerms("%s.srt" % line['name'].rstrip(), verbose) reDownload = oldReDownload subComment = dlCommentSuccess break else: printScores() if not fileSizeOK: onError(47, "Failed. File sizes does not match") printInfo2("Trying again") else: onError(34, "Failed. Subtitle file does not exist") printInfo2("Trying again") reDownload = True else: subComment = dlCommentExist break else: subComment = dlCommentNoSub printInfo2("\nGetting file info...") fileSize = getInfo(line, '--Inform="General;%FileSize%"', verbose) fileSizeMeasure = getInfo(line, '--Inform="General;%FileSize/String%"', verbose) duration = getInfo(line, '--Inform="General;%Duration%"', verbose) durationFormatted = getInfo(line, '--Inform="General;%Duration/String3%"', verbose) overallBitRate = getInfo(line, '--Inform="General;%OverallBitRate%"', verbose) overallBitRateMeasure = getInfo(line, '--Inform="General;%OverallBitRate/String%"', verbose) videoFormat = getInfo(line, '--Inform="Video;%Format%"', verbose) videoCodecId = getInfo(line, '--Inform="Video;%CodecID%"', verbose) videoBitRate = getInfo(line, '--Inform="Video;%BitRate%"', verbose) videoBitRateMeasure = getInfo(line, '--Inform="Video;%BitRate/String%"', verbose) width = getInfo(line, '--Inform="Video;%Width%"', verbose) height = getInfo(line, '--Inform="Video;%Height%"', verbose) frameRate = getInfo(line, '--Inform="Video;%FrameRate%"', verbose) frameCount = getInfo(line, '--Inform="Video;%FrameCount%"', verbose) audioFormat = getInfo(line, '--Inform="Audio;%Format%"', verbose) audioCodecId = getInfo(line, '--Inform="Audio;%CodecID%"', verbose) audioBitRate = getInfo(line, '--Inform="Audio;%BitRate%"', verbose) audioBitRateMeasure = getInfo(line, '--Inform="Audio;%BitRate/String%"', verbose) if line['subs']: subSize = os.path.getsize("%s.srt" % line['name'].rstrip()) with open("%s.srt" % line['name'].rstrip()) as myfile: subLines = sum(1 for line in myfile) # number of lines in file myfile.close() # close file else: subSize = "na" subLines = "na" infoDownloaded.append({'videoName': "%s.%s" % (line['name'].rstrip(), line['suffix']), 'expectedDuration': line['duration'], 'videoDlComment': videoComment, 'fileSize': fileSize, 'fileSizeMeasure': fileSizeMeasure, 'duration': duration, 'durationFormatted': durationFormatted, 'overallBitRate': overallBitRate, 'overallBitRateMeasure': overallBitRateMeasure, 'videoFormat': videoFormat, 'videoCodecId': videoCodecId, 'videoBitRate': videoBitRate, 'videoBitRateMeasure': videoBitRateMeasure, 'width': width, 'height': height, 'frameRate': frameRate, 'frameCount': frameCount, 'audioFormat': audioFormat, 'audioCodecId': audioCodecId, 'audioBitRate': audioBitRate, 'audioBitRateMeasure': audioBitRateMeasure, 'subName': "%s.srt" % line['name'].rstrip(), 'expectedSubSize': line['subSize'], 'subDlComment': subComment, 'subSize': subSize, 'subLines': subLines}) printScores() print return infoDownloaded
def getDuration(stream, checkDuration, verbose): duration = "0.000" gotAnswer = False gotXML = False noFFmpeg = False trys = 0 ffprobe = getffprobePath(verbose) if not ffprobe: if verbose: printWarning("Disabling checking of duration") checkDuration = False if checkDuration: if verbose: printScores() printInfo2("Probing for duration of stream...") if ffprobe == ffprobePath: if verbose: printInfo2("Using ffprope to determin duration...") cmd = "%s -loglevel error -show_format -show_streams %s -print_format xml" % (ffprobe, stream) if verbose: printInfo1("Command: %s\n" % cmd) args = shlex.split(cmd) while True: trys += 1 if trys > maxTrys: onError(38, "Giving up after %s trys" % (trys - 1)) printWarning("Setting duration to %s" % duration) gotAnswer = True gotXML = True break while True: try: process = Popen(args, stdout=PIPE, stderr=PIPE) except OSError as e: onError(39, "%s\nYou are probably missing ffmpeg" % e) noFFmpeg = True break else: if verbose: printInfo1("Got an answer") output, error = process.communicate() gotAnswer = True break if not noFFmpeg: try: xmlRoot = ET.fromstring(output) except: onError(43, "Did not receive a valid XML") printInfo2("Trying again...") else: if verbose: printInfo1("Downloaded a valid XML") print output for xmlChild in xmlRoot: if 'duration' in xmlChild.attrib: duration = xmlChild.attrib['duration'] if verbose: printInfo1("Found duration in XML: %s" % duration) gotXML = True if not duration and verbose: printWarning("Could not find duration in XML") else: onError(40, "Can not detect duration") printWarning("Setting duration to %s" % duration) gotAnswer = True gotXML = True if gotAnswer and gotXML: break if verbose: printScores() else: printInfo2("Using avprobe to determine duration...") cmd = "%s -loglevel error -show_format -show_streams %s -of json" % (ffprobe, stream) if verbose: printInfo1("Command: %s\n" % cmd) args = shlex.split(cmd) while True: trys += 1 if trys > maxTrys: onError(38, "Giving up after % trys" % (trys - 1)) gotAnswer = True gotXML = True break while True: try: process = Popen(args, stdout=PIPE, stderr=PIPE) except OSError as e: onError(39, "%s\nYou are probably missing ffmpeg" % e) noFFmpeg = True break else: if verbose: printInfo1("Got an answer") output, error = process.communicate() gotAnswer = True break if gotAnswer: break if gotAnswer: jsonString = json.loads(output) if json.dumps(jsonString['format']['duration']): duration = (json.dumps(jsonString['format']['duration']).strip('"')) if verbose: print "Duration: %s" % duration else: printWarning("Duration check disabled") printWarning("Setting duration to %s" % duration) printInfo1("Duration: %s s (%s)" % (duration.rstrip("0").rstrip("."), str(datetime.timedelta(seconds=round(float(duration), 2)))) ) return duration
def usage(exitCode): printInfo1("\nUsage:") printScores() printInfo1("%s -u <url>" % sys.argv[0]) printInfo1(" Parse <url> for streamed video")