#!/usr/bin/env python3
from urllib import request
import ssl, json, os, sys
host = "192.168.1.999" # Your Device's IP address
download_to = "app000" # Location to save the downloaded data
cookie = "CSRF-Token=sVFaf9o0li4E1NBE72BIBZQk+HlhpxS4; WMID=7366892322403631730047470568773952721700401324520418066364616518" # The headers sent from the browser
ignore_cert = ssl.create_default_context()
ignore_cert.check_hostname = False
ignore_cert.verify_mode = ssl.CERT_NONE
handler_https = request.HTTPSHandler( context = ignore_cert )
opener = request.build_opener( handler_https )
opener.addheaders = [( "Cookie", cookie )]
params0 = {
"knownfolderid": "LocalAppData"
, "packagefullname": "<app_package_id>"
}
uri_fs = "https://%(host)s/api/filesystem/apps/%(uri)s"
def fs_command( cmd, params ):
p = { **params0, **params }
qstr = cmd + "?" + "&".join( "%s=%s" % x for x in p.items() )
return uri_fs % { "host": host, "uri": qstr }
def open_dir( path ):
params = {}
if path:
params[ "path" ] = path
req = opener.open( fs_command( "files", params ) )
data = json.loads( req.read() )
return data
def download( item ):
_dir = os.path.join( download_to, item[ "SubPath" ] ).replace( "\\", os.path.sep )
os.makedirs( _dir, exist_ok = True )
req = opener.open( fs_command( "file", { "filename": item[ "Id" ], "path": item[ "SubPath" ] } ) )
with open( os.path.join( _dir, item[ "Name" ] ), "wb" ) as f:
f.write( req.read() )
def download_all( data ):
for item in data[ "Items" ]:
f_id, subpath, entry_type = item[ "Id" ], item[ "SubPath" ], item[ "Type" ]
if entry_type == 32: # Entry is file
print( " -> " + f_id )
download( item )
elif entry_type == 16: # Entry is folder
print( "Path: " + subpath )
ndata = open_dir( subpath )
download_all( ndata )
data = open_dir( None )
download_all( data )