-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxbox7zftp.py
139 lines (107 loc) · 3.4 KB
/
xbox7zftp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# pip3 install pyunpack patool tqdm
# usage: xbox7zftp.py game.7z
# Tested with XBMC4Gamers and Windows 11 on HDD ready pack, 2022-08-12
# xbox settings, change these if you need to !!!!
xbox_ip = '10.0.0.44'
xbox_user = 'xbox'
xbox_password = 'xbox'
xbox_path = '/F/games/'
#################################################
#################################################
from ftplib import FTP
from tqdm import tqdm
from pyunpack import Archive
import tempfile, shutil, os, random, sys
bs = 128000
files_transfered = 0
# ftp upload function from https://stackoverflow.com/a/27299745
def uploadThis(path):
global files_transfered, pbar
files = os.listdir(path)
os.chdir(path)
for f in files:
if os.path.isfile(f):
with open(f,'rb') as fh:
myFTP.storbinary('STOR %s' % f, fh, blocksize=bs, callback=blockTransfered)
files_transfered += 1
pbar.set_description(str(files_transfered) + "/" + str(folder_files) + " files")
elif os.path.isdir(f):
myFTP.mkd(f)
myFTP.cwd(f)
uploadThis(f)
myFTP.cwd('..')
os.chdir('..')
def blockTransfered(block):
# update bar with how many blocks we transfered
pbar.update(len(block))
def folderStats(path):
size = 0
filecount = 0
for path, dirs, files in os.walk(path):
for f in files:
filecount += 1
fp = os.path.join(path, f)
size += os.path.getsize(fp)
return size, filecount
# get input file
try:
infile = sys.argv[1]
except IndexError:
print ("usage:", sys.argv[0], "game.7z")
print ("Edit the script to change ip, username, password, etc...")
sys.exit(1)
if not os.path.isfile(infile):
print("Error, not a file:", infile)
sys.exit(1)
print("Destination:", xbox_ip, xbox_path)
print("Game:", infile)
print()
# Test xbox connection
print("Testing FTP connection to Xbox...", end=" ", flush=True)
try:
myFTP = FTP(xbox_ip, xbox_user, xbox_password, timeout=3)
myFTP.getwelcome()
print("ok")
except Exception as e:
print()
print("FTP error:", e, "Exiting...")
sys.exit(1)
# create temp folder to store extracted files in
temp_folder = tempfile.mkdtemp()
# extract
print ("Unzipping game to temp folder...", end=" ", flush=True)
Archive(infile).extractall(temp_folder)
print("ok")
# get folder size and file count
folder_size, folder_files = folderStats(temp_folder)
# cd on server (xbox) to games folder
myFTP.cwd(xbox_path)
# if game is not in its own folder, make a folder for it
if os.path.isfile(os.path.join(temp_folder, "default.xbe")):
print("default.xbe detected in root of temp folder. We need to make a folder for the game.")
game_name = input("Enter name for game folder: ")
myFTP.mkd(game_name)
myFTP.cwd(game_name)
print ("Starting FTP transfer")
pbar = tqdm(total=folder_size, unit="B", unit_scale=True, unit_divisor=1000)
try:
uploadThis(temp_folder)
except Exception as e:
print("Error occurred during FTP transfer!")
print("Error message:", e)
myFTP.quit()
input("Press any key to exit")
sys.exit(1)
pbar.close()
myFTP.quit()
print ("FTP complete")
# make sure all files were transfered
if files_transfered < folder_files:
print("Error! Not all files were transfered.")
print("This is probably due to your OS purging the temp folder")
sys.exit(1)
# remove temp folder
print ("Deleting temp folder...", end=" ", flush=True)
shutil.rmtree(temp_folder)
print("ok")
print ("All done :)")