JurajNyiri / pytapo

Python library for communication with Tapo Cameras
MIT License
277 stars 58 forks source link

Avoid duplicate download of recordings (happens when last recording is downloaded) #93

Open peterstamps opened 9 months ago

peterstamps commented 9 months ago

When downloading in a continues process, at the latest Tapo recording, the module session.py reports something like: "Received response with no or invalid session information. can't deliver" and then a retry is started and the file is downloaded. However I get two downloaded files with the same start time of the recording but with different end times (some seconds more).

I adapted the downloader.py as follows (skipping the retry and save the file. When I inspect the file the whole recording is there. The segment length was probably smaller then anticipated, but the whole file (recording) was delivered. This work around solves that issue. I do not miss any seconds of the original recording!

                if downloading:
                    # Peter commented this retry block as it was creating non-stop duplicates. 
                    # A shorter and a longer version, both had the same start time but different end times of recording)
                    # Handle case where camera randomly stopped responding
                    #if not downloadedFull and not retry:
                    #    currentAction = "Retrying"
                    #    yield {
                    #        "currentAction": currentAction,
                    #        "fileName": fileName,
                    #        "progress": 0,
                    #        "total": 0,
                    #    }
                    #    retry = True
                    #else:
                    if not downloadedFull:
                        detectedLength = convert.getLength()
                        if (
                            detectedLength >= segmentLength - 5
                        ):  # workaround for weird cases where the recording is a bit shorter than reported
                            downloadedFull = True
                            currentAction = "Converting [shorter]"
                            yield {
                                "currentAction": currentAction,
                                "fileName": fileName,
                                "progress": 0,
                                "total": 0,
                            }
                            convert.save(fileName, segmentLength, method)
                            yield {
                                "currentAction": "Ready Converting [shorter]",
                                "fileName": fileName,
                                "progress": 0,
                                "total": segmentLength,
                            }                                  
                        else:
                            currentAction = "Giving up"
                            yield {
                                "currentAction": currentAction,
                                "fileName": fileName,
                                "progress": 0,
                                "total": 0,
                            }
                        downloading = False