Help with a download script
#1
I made a script to download a external video file, its working just fine, but i keep stuck in the screen till the download is done and i wanted to know how can i play this file while downloading it. Or if there is any addon that could do that for me it would work too, thanks in advance

python:
def Download(url, ref=""):
    Path = xbmc.translatePath( xbmcaddon.Addon().getAddonInfo('path') )
    file = os.path.join( Path, "video.mp4")
    req = Request(url)
    if ref:
        req.add_header('referer', ref)
    prog=0
    progress = xbmcgui.DialogProgressBG()
    progress.create('Loading...')
    sizechunk = 16 * 1024 
    totalsize = 0
    resp = urlopen(req)
    length = re.compile('ength\: ?(\d+)').findall(str(resp.headers))
    with open(file, 'wb') as f:
        while True:
            progtotal = int( 100*totalsize/(int(length[0])) )
            progress.update(progtotal, "")
            prog+=1
            chunk = resp.read(sizechunk)
            if not chunk:
                break
            f.write(chunk)
            totalsize+=sizechunk
    progress.close()
Reply
#2
Thread moved to addon development
|Banned add-ons (wiki)|Forum rules (wiki)|VPN policy (wiki)|First time user (wiki)|FAQs (wiki) Troubleshooting (wiki)|Add-ons (wiki)|Free content (wiki)|Debug Log (wiki)|

Kodi Blog Posts
Reply
#3
(2021-05-15, 20:27)DarrenHill Wrote: Thread moved to addon development

Sorry for opening in the wrong forum
I guess I made it worked, what would be the best way to stop the looping if i wanted to stop downloading, my only thought for now is making a variable and saved it in the addon config and change it when i want it to stop
Reply
#4
Probably not the best solution, but this what came to my mind, using getSetting to check if u want to stop the download, also it needs to implement resume option
Posting here cuz it may help someone that are looking for something like that, thanks

python:

AddonID = 'plugin.video.addoname'
Addon = xbmcaddon.Addon(AddonID)

def StartDownload(url="", ref=""):
    if Addon.getSetting("cDownload") == "True":
        d = xbmcgui.Dialog().yesno("", "Stop Downloading and start this one?")
        if d:
            Addon.setSetting("cDownload", "False")
            xbmc.sleep(6000)
            Addon.setSetting("cDownload", "True")
            Download(url, ref)
    else:
        Addon.setSetting("cDownload", "True")
        Download(url, ref)
    
def StopDownload():
    d = xbmcgui.Dialog().yesno("", "Stop Downloading?")
    if d:
        Addon.setSetting("cDownload", "False")

python:

def Download(url="", ref=""):
    if not url: return
    Path = xbmc.translatePath( xbmcaddon.Addon().getAddonInfo('path') )
    file = os.path.join( Path, "video.mp4")
    req = Request(url)
    if ref:
        req.add_header('referer', ref)
    req.add_header('Content-Type', 'video/mp4')
    sizechunk = 10 * 1024 * 1024
    totalsize = 0
    resp = urlopen(req)
    length = re.compile('ength\: ?(\d+)').findall(str(resp.headers))
    prog=0
    progress = xbmcgui.DialogProgressBG()
    progress.create('Loading... '+length[0])
    with open(file, 'wb') as f:
        while True and Addon.getSetting("cDownload") == "True":
            progtotal = int( 100*totalsize/(int(length[0])) )
            progress.update(progtotal, "")
            prog+=1
            chunk = resp.read(sizechunk)
            if not chunk:
                Addon.setSetting("cDownload", "False")
                break
            f.write(chunk)
            totalsize+=sizechunk
    progress.close()
Reply
#5
You don't need to use addon.setSetting / addon.getSetting, just use self to jump between functions.

self.cDownload = False

self.cDownload = True
Reply
#6
(2021-05-17, 08:58)M89SE Wrote: You don't need to use addon.setSetting / addon.getSetting, just use self to jump between functions.

self.cDownload = False

self.cDownload = True

Ill try that, thanks for replying
Reply
#7
(2021-05-17, 08:58)M89SE Wrote: You don't need to use addon.setSetting / addon.getSetting, just use self to jump between functions.

self.cDownload = False

self.cDownload = True
Im not sure how to use that, i guess i need to make a class, never tried that
Reply
#8
(2021-05-17, 20:47)Rick987 Wrote:
(2021-05-17, 08:58)M89SE Wrote: You don't need to use addon.setSetting / addon.getSetting, just use self to jump between functions.

self.cDownload = False

self.cDownload = True
Im not sure how to use that, i guess i need to make a class, never tried that
I guess it should be something like this, you also need to add the request func to the class.

python:
AddonID = 'plugin.video.addoname'
Addon = xbmcaddon.Addon(AddonID)

class run():
    def __init__(self):
        pass

    def StartDownload(self, url="", ref=""):
        if self.cDownload:
            d = xbmcgui.Dialog().yesno("", "Stop Downloading and start this one?")
            if d:
                #Addon.setSetting("cDownload", "False")
                self.cDownload = False
                xbmc.sleep(6000)
                #Addon.setSetting("cDownload", "True")
                self.cDownload = True
                self.Download(url, ref)
        else:
            Addon.setSetting("cDownload", "True")
            self.Download(url, ref)

    def StopDownload(self):
        d = xbmcgui.Dialog().yesno("", "Stop Downloading?")
        if d:
            #Addon.setSetting("cDownload", "False")
            self.cDownload = False

    def Download(self, url="", ref=""):
        if not url: return
        Path = xbmc.translatePath( xbmcaddon.Addon().getAddonInfo('path') )
        file = os.path.join( Path, "video.mp4")
        req = Request(url)
        if ref:
            req.add_header('referer', ref)
        req.add_header('Content-Type', 'video/mp4')
        sizechunk = 10 * 1024 * 1024
        totalsize = 0
        resp = urlopen(req)
        length = re.compile('ength\: ?(\d+)').findall(str(resp.headers))
        prog=0
        progress = xbmcgui.DialogProgressBG()
        progress.create('Loading... '+length[0])
        with open(file, 'wb') as f:
            while True and self.cDownload:
                progtotal = int( 100*totalsize/(int(length[0])) )
                progress.update(progtotal, "")
                prog+=1
                chunk = resp.read(sizechunk)
                if not chunk:
                    #Addon.setSetting("cDownload", "False")
                    self.cDownload = False
                    break
                f.write(chunk)
                totalsize+=sizechunk
        progress.close()

r = run()
r.StartDownload()
Reply
#9
(2021-05-18, 00:32)M89SE Wrote:
(2021-05-17, 20:47)Rick987 Wrote:
(2021-05-17, 08:58)M89SE Wrote: You don't need to use addon.setSetting / addon.getSetting, just use self to jump between functions.

self.cDownload = False

self.cDownload = True
Im not sure how to use that, i guess i need to make a class, never tried that
I guess it should be something like this, you also need to add the request func to the class.

python:
AddonID = 'plugin.video.addoname'
Addon = xbmcaddon.Addon(AddonID)

class run():
    def __init__(self):
        pass

    def StartDownload(self, url="", ref=""):
        if self.cDownload:
            d = xbmcgui.Dialog().yesno("", "Stop Downloading and start this one?")
            if d:
                #Addon.setSetting("cDownload", "False")
                self.cDownload = False
                xbmc.sleep(6000)
                #Addon.setSetting("cDownload", "True")
                self.cDownload = True
                self.Download(url, ref)
        else:
            Addon.setSetting("cDownload", "True")
            self.Download(url, ref)

    def StopDownload(self):
        d = xbmcgui.Dialog().yesno("", "Stop Downloading?")
        if d:
            #Addon.setSetting("cDownload", "False")
            self.cDownload = False

    def Download(self, url="", ref=""):
        if not url: return
        Path = xbmc.translatePath( xbmcaddon.Addon().getAddonInfo('path') )
        file = os.path.join( Path, "video.mp4")
        req = Request(url)
        if ref:
            req.add_header('referer', ref)
        req.add_header('Content-Type', 'video/mp4')
        sizechunk = 10 * 1024 * 1024
        totalsize = 0
        resp = urlopen(req)
        length = re.compile('ength\: ?(\d+)').findall(str(resp.headers))
        prog=0
        progress = xbmcgui.DialogProgressBG()
        progress.create('Loading... '+length[0])
        with open(file, 'wb') as f:
            while True and self.cDownload:
                progtotal = int( 100*totalsize/(int(length[0])) )
                progress.update(progtotal, "")
                prog+=1
                chunk = resp.read(sizechunk)
                if not chunk:
                    #Addon.setSetting("cDownload", "False")
                    self.cDownload = False
                    break
                f.write(chunk)
                totalsize+=sizechunk
        progress.close()

r = run()
r.StartDownload()

I understand a bit more about classes now, i could make it work, but I still dont know how to stop, when i call r.StopDownload() it says the vairable r doenst exists, dunno what im doing wrong, but anyway id like to thank you for all the help

python:

if mode == 0:
    r = run()
    r.StartDownload()
elif mode == 1
    r.StopDownload()
Reply
#10
try this:

python:
r = run()

if mode == 0:
    r.StartDownload()
elif mode == 1
    r.StopDownload()
Reply
#11
(2021-05-18, 20:42)M89SE Wrote: try this:

python:
r = run()

if mode == 0:
    r.StartDownload()
elif mode == 1
    r.StopDownload()

I tried this too, seems like every time you run anything in the addon the variable r is different and receives a new class, maybe im doing something wrong. thx anyway
Reply
#12
oh sry the class is already running, try this:

python:
if mode == 0:
     self.StartDownload()
elif mode == 1
     self.StopDownload()
Reply
#13
(2021-05-19, 19:56)M89SE Wrote: oh sry the class is already running, try this:

python:
if mode == 0:
     self.StartDownload()
elif mode == 1
     self.StopDownload()

u dont need to be sorry. I tried this and it didnt work, it says self is not defined
Reply
#14
self must be defined in __init__ , try this, but you need to adjust to your function because I haven't seen the whole code:

python:
class run():
    def __init__(self):
        self.cDownload = True
        self.StartDownload = self.StartDownload()
        self.StopDownload = self.StopDownload()
        self.Download = self.Download()
        

    def onInit(self):
        mode = 0
        if mode == 0:
            self.StartDownload()
        elif mode == 1:
            self.StopDownload()

    def StartDownload(self, url="", ref=""):
        if self.cDownload:
            d = xbmcgui.Dialog().yesno("", "Stop Downloading and start this one?")
            if d:
                #Addon.setSetting("cDownload", "False")
                self.cDownload = False
                xbmc.sleep(6000)
                #Addon.setSetting("cDownload", "True")
                self.cDownload = True
                self.Download(url, ref)
        else:
            Addon.setSetting("cDownload", "True")
            self.Download(url, ref)

    def StopDownload(self):
        d = xbmcgui.Dialog().yesno("", "Stop Downloading?")
        if d:
            #Addon.setSetting("cDownload", "False")
            self.cDownload = False

    def Download(self, url="", ref=""):
        if not url: return
        Path = xbmc.translatePath( xbmcaddon.Addon().getAddonInfo('path') )
        file = os.path.join( Path, "video.mp4")
        req = Request(url)
        if ref:
            req.add_header('referer', ref)
        req.add_header('Content-Type', 'video/mp4')
        sizechunk = 10 * 1024 * 1024
        totalsize = 0
        resp = urlopen(req)
        length = re.compile('ength\: ?(\d+)').findall(str(resp.headers))
        prog=0
        progress = xbmcgui.DialogProgressBG()
        progress.create('Loading... '+length[0])
        with open(file, 'wb') as f:
            while True and self.cDownload:
                progtotal = int( 100*totalsize/(int(length[0])) )
                progress.update(progtotal, "")
                prog+=1
                chunk = resp.read(sizechunk)
                if not chunk:
                    #Addon.setSetting("cDownload", "False")
                    self.cDownload = False
                    break
                f.write(chunk)
                totalsize+=sizechunk
        progress.close()


run()
Reply
#15
(2021-05-22, 00:59)M89SE Wrote: self must be defined in __init__ , try this, but you need to adjust to your function because I haven't seen the whole code:

python:
class run():
    def __init__(self):
        self.cDownload = True
        self.StartDownload = self.StartDownload()
        self.StopDownload = self.StopDownload()
        self.Download = self.Download()
        

    def onInit(self):
        mode = 0
        if mode == 0:
            self.StartDownload()
        elif mode == 1:
            self.StopDownload()

    def StartDownload(self, url="", ref=""):
        if self.cDownload:
            d = xbmcgui.Dialog().yesno("", "Stop Downloading and start this one?")
            if d:
                #Addon.setSetting("cDownload", "False")
                self.cDownload = False
                xbmc.sleep(6000)
                #Addon.setSetting("cDownload", "True")
                self.cDownload = True
                self.Download(url, ref)
        else:
            Addon.setSetting("cDownload", "True")
            self.Download(url, ref)

    def StopDownload(self):
        d = xbmcgui.Dialog().yesno("", "Stop Downloading?")
        if d:
            #Addon.setSetting("cDownload", "False")
            self.cDownload = False

    def Download(self, url="", ref=""):
        if not url: return
        Path = xbmc.translatePath( xbmcaddon.Addon().getAddonInfo('path') )
        file = os.path.join( Path, "video.mp4")
        req = Request(url)
        if ref:
            req.add_header('referer', ref)
        req.add_header('Content-Type', 'video/mp4')
        sizechunk = 10 * 1024 * 1024
        totalsize = 0
        resp = urlopen(req)
        length = re.compile('ength\: ?(\d+)').findall(str(resp.headers))
        prog=0
        progress = xbmcgui.DialogProgressBG()
        progress.create('Loading... '+length[0])
        with open(file, 'wb') as f:
            while True and self.cDownload:
                progtotal = int( 100*totalsize/(int(length[0])) )
                progress.update(progtotal, "")
                prog+=1
                chunk = resp.read(sizechunk)
                if not chunk:
                    #Addon.setSetting("cDownload", "False")
                    self.cDownload = False
                    break
                f.write(chunk)
                totalsize+=sizechunk
        progress.close()


run()

Im gonna try this, thanks again
Reply

Logout Mark Read Team Forum Stats Members Help
Help with a download script0