Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 73845

Python script to download file from a published URL (public weblink)

$
0
0

Does anyone have any advice on the most effective way to download a file from a published public weblink. Google format example: drive.google.com/open?id=111111MMSvCyAhXXXXXXXXXXrPz-YYYYYYy although specifically we're trying to get this working with Intermedia's SecuriSync product.

These are public URL's so no login is required but typically it'll either open the file directly or download in Browser so they're not direct file downloads per se.

Code we've been trying:

DownTo='%userprofile%/downloads'from URL='https://sharesync.serverdata.net/uk/s/file?public_share=hVv4gg20M0K4vT3WgoVTx500000464'
import ctypes
class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)

import subprocess        
with disable_file_system_redirection(): 
        import urllib
import os

def downloadFile(DownTo, fromURL):
    try:
        fileName = fromURL.split('/')[-1]
        DownTo = os.path.join(DownTo, fileName)
        with open(DownTo, 'wb') as f:
            f.write(urllib.urlopen(fromURL).read())
        if os.path.isfile(DownTo):
            return '{} - {}KB'.format(DownTo, os.path.getsize(DownTo)/1000)
    except:
        return 'Please Check URL or Download Path!'

if __name__=='__main__':
    print downloadFile(DownTo, fromURL )here

We've also been trying some other variations but finding this method is not handling larger file sizes well (example file is around 800kb).

import os
import urllib

url = "http://web.archive.org/web/20100105072145/http://cmrr.ucsd.edu/people/Hughes"
dest_dir = os.path.expandvars('%userprofile%/Desktop')

try:
    os.makedirs(dest_dir)
except OSError:
    pass

urllib.urlretrieve(url, os.path.join(dest_dir, 'HDDEraseWeb.zip'))

Viewing all articles
Browse latest Browse all 73845

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>