Search code examples
pythondownloadzipsequentialshutil

How to make a python Script wait until download has finished


My Script downloads a zipfile, exctracts the relevant parts replaced files and folders etc. It used to work flawlessly, for some reason its has now decided to stop working and only partly downloads the zipfile, and of course, as the zipfile is incomplete I get an error, saying the downloaded file is not a zipfile. my script is as follows.

def downloadupdate():
    xbmcgui.Dialog().ok(
        "[B][COLOR white]Daily Updater[/B][/COLOR]",
        "Favourites and some software will now update",
        "Elements of your system will be cleaned",
        "Daily Update will take at most 2 minutes")
    #ONLY HAVE THE SUPER FAVOURITES FOLDER IN THE ZIPFILE!!
    url = 'http://x.com/x/x/Super Favourites.zip'
    destination = xbmc.translatePath('special://home/userdata/addon_data/iupdatefix/Super Favourites.zip')
    favzip = urllib.urlopen(url)
    xbmc.executebuiltin("Notification(Downloading new updates, PLEASE WAIT,()")
    with open(xbmc.translatePath('special://home/userdata/addon_data/iupdatefix/Super Favourites.zip'), "wb") as zipFile:
        zipFile.write(favzip.read())
    xbmc.executebuiltin("Notification(Download Complete, Please wait,()")
    time.sleep(5)
    xbmc.executebuiltin("Notification(Updating Click and Play channels, Please wait,()")
    updatezip = xbmc.translatePath('special://home/userdata/addon_data/iupdatefix/Super Favourites.zip')
    extractupdate = xbmc.translatePath('special://home/userdata/addon_data/plugin.program.super.favourites/')
    oldfav = xbmc.translatePath('special://home/userdata/addon_data/plugin.program.super.favourites/Super Favourites')
    yeszip = os.path.exists(updatezip)
    time.sleep(5)
    if yeszip:
        xbmc.executebuiltin("Notification(Removing previous, Please wait,()")
        shutil.rmtree(oldfav, ignore_errors=False)
        xbmc.executebuiltin("Notification(Updating, now,()")
        gh = open(updatezip, 'rb')
        zp = zipfile.ZipFile(gh)
        zp.extractall(extractupdate)
        gh.close()
        time.sleep(3)
        xbmc.executebuiltin("Notification(updated, Now Checking sd,()")
        # put this command at the end of whole script --->>>os.remove(updatezip)
    else:
        xbmc.executebuiltin("Notification(Update file Corrupt, Please try again,()")

Solution

  • The problem has been solved, It was not the code. It was the hosting service, I had to reset the File permissions on the server and after that all works perfectly again.