-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathrestsdk_public.py
114 lines (106 loc) · 4.62 KB
/
restsdk_public.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
##Intended for python3.6 on linux, probably won't work on Windows
##This software is distributed without any warranty. It will probably brick your computer.
#DO NOT ADD SLASHES TO END OF DIRECTORIES
db='/restsdk/data/db/index.db' #where the file DB is stored example: /data/db/index.db
filedir='/restsdk/data/files' #where the files are stored example: /restsdk/data/files
dumpdir='/location/to/dump/files/to' #where you want the new files dumped example:/EXTERNAL/FILES
#NOTHING AFTER THIS LINE NEEDS TO BE EDITED
skipnames=[filedir] #remove these strings from the final file/path name. Don't edit this.
import sqlite3
import pprint
import copy
import os
from shutil import copyfile
def findNextParent(fileID):
#finds the next parent db item in a chain
for key,value in fileDIC.items():
if key==fileID:
return value['Parent']
def hasAnotherParent(fileID):
#checks to see if a db item has another parent
if fileDIC[fileID]['Parent']!=None:
return True
else:
return False
def findTree(fileID,name,parent):
#turn a file ID into an original path
path=fileDIC[parent]['Name']+"/"+name
while hasAnotherParent(fileID)==True:
fileID=findNextParent(fileID)
path=fileDIC[fileID]['Name']+'/'+path
return path
def idToPath2(fileID):
#turn a file ID into an original path
value=fileDIC[fileID]
if value['Parent']!=None:
#print("Found file " + value['Name'] + 'searching for parents')
#print('Totalpath is ' + path)
path=findTree(fileID,value['Name'],value['Parent'])
else:
#print("Found file " + value['Name'] + 'no parent search needed')
path=fileDIC[fileID]['Name']
return path
def filenameToID(filename):
#turn a filename from filesystem into a db id | Some (newer?) versions of MyCloud Home have changed their DB entries so that contentID no longer is the filename
for keys,values in fileDIC.items():
if values['contentID']==filename:
#print('Found filename ' + filename + ' in DBkey ' + str(keys) +' with name ' + values['Name'])
return str(keys)
elif values['Name']==filename:
#print('Found filename ' + filename + ' in DBkey ' + str(keys) +' with name ' + values['Name'])
return str(keys)
#print('Unable to find filename' + filename)
return None
def getRootDirs():
#quick function to find annoying "auth folder" name for filtering purposes
for keys,values in fileDIC.items():
if 'auth' in values['Name'] and '|' in values['Name']:
return str(values['Name'])
#open the sqlite database
print('Opening database...',end="/r")
try:
con = sqlite3.connect(db)
except:
print('Error opening database at ' + db)
quit()
print('Querying database...',end="/r")
cur = con.cursor()
cur.execute("SELECT id,name,parentID,mimeType,contentID FROM files")
files = cur.fetchall()
#SQlite has a table named "FILES", the filename in the file structure is found in ContentID, with the parent directory being called ParentID
fileDIC={}
for file in files:
fileID=file[0]
fileName=file[1]
fileParent=file[2]
mimeType=file[3]
contentID=file[4]
fileDIC[fileID]={'Name':fileName,'Parent':fileParent,'contentID':contentID,'Type':mimeType,'fileContentID':''}
skipnames.append(getRootDirs()) #remove obnoxious root dir names
for root,dirs,files in os.walk(filedir): #find all files in original directory structure
for file in files:
filename=str(file)
print('FOUND FILE ' + filename + ' SEARCHING......',end="\r")
fileID=filenameToID(str(file))
fullpath=None
if fileID!=None:
fullpath=idToPath2(fileID)
if fullpath!=None:
#print('FILE RESOLVED TO ' + fullpath)
for paths in skipnames:
newpath=fullpath.replace(paths,'')
newpath=dumpdir+newpath
newpath=newpath.replace('|', '-')
fullpath=str(os.path.join(root,file))
#print('Copying ' + fullpath + ' to ' + newpath,end="\r")
print('Copying ' + newpath)
try:
os.makedirs(os.path.dirname(newpath), exist_ok=True)
copyfile(fullpath,newpath)
except:
print('Error copying file ' + fullpath + ' to ' + newpath)
print("Did this script help you recover your data? Save you a few hundred bucks? Or make you some money recovering somebody else's data?")
print("Consider sending us some bitcoin/crypto as a way of saying thanks!")
print("Bitcoin: 1DqSLNR8kTgwq5rvveUFDSbYQnJp9D5gfR")
print("ETH: 0x9e765052283Ce6521E40069Ac52ffA5B277bD8AB")
print("Zcash: t1RetUQktuUBL2kbX72taERb6QcuAiDsvC4")