gofilepy
Gofilepy - Unofficial Python wrapper for Gofile API
A true wrapper for Gofile's REST API.
Installation
Install Gofilepy with pip
pip install gofilepy-api
Documentation
Usage/Examples (Free Users)
from gofilepy import GofileClient
client = GofileClient()
#Free users can this function
file = client.upload(file=open("./test.txt", "rb"))
print(file.name)
print(file.page_link) #View and download file at this link
Usage/Examples (Premium Users)
from gofilepy import GofileClient
from gofilepy.options import FileOption, FolderOption
from gofilepy.exceptions import GofileAPIAuthenticationError
client = GofileClient(token="") #Get token from gofile.io.
print(client.account.email)
print(client.account.tier)
root_folder_id = client.account.root_id
root = client.get(root_folder_id)
child = client.create_folder("NEW_FOLDER", parent_id=root.content_id)
child.set_option(FolderOption.DESCRIPTION, "New folder created with gofilepy") #More options available https://gofile.io/api
child.set_option(FolderOption.TAGS, ["example", "gofilepy"])
# Registering changes to local variable
child.content_id in root.children_ids # = false because it hasn't been updated
root.reload() #Gets any new changes/updates to the folder
child.content_id in root.children_ids # = true after root folder has been reloaded
# Copying content (files & folders)
child.copy_to(child.parent_id) #Duplicates folder in same directory
root.reload() #Now root.children_ids has another id
#uploading & downloading files
f = child.upload("./test.txt") #uploads file to newly created "child" folder
f.set_option(FileOption.HAS_DIRECT_LINK, True) #Must be set to true to download using gofilepy
print(f.direct_link) #"None"
f.reload() #now f.direct_link is updated
path = f.download("./") #downloads file to local dir
print(path) #function returns full path of downloaded file
#Deleting content
child.delete() #Deletes folder
f.delete() #Deletes file
Links
1""" 2.. include:: /home/m0bb1n/Programs/gofilepy_dev/gofilepy/README.md 3""" 4__pdoc__ = { 5 "gofile": False 6} 7 8__all__ = [ 9 "GofileClient", 10 "GofileFolder", 11 "GofileFile", 12 "GofileContent", 13 "GofileAccount", 14 "options", 15 "exceptions" 16] 17 18from .gofile import GofileClient, GofileFolder, GofileFile, GofileContent, GofileAccount
13class GofileClient (object): 14 server = None 15 _BASE_DOMAIN = 'gofile.io' 16 _API_SUBDOMAIN = 'api' 17 _BASE_API_URL = 'https://'+_API_SUBDOMAIN+'.'+_BASE_DOMAIN 18 19 _API_ROUTE_GET_SERVER_URL = _BASE_API_URL + '/getServer' 20 _API_ROUTE_GET_ACCOUNT_URL = _BASE_API_URL + "/getAccountDetails" 21 _API_ROUTE_GET_CONTENT_URL = _BASE_API_URL + "/getContent" 22 23 _API_ROUTE_DELETE_CONTENT_URL = _BASE_API_URL + "/deleteContent" 24 _API_ROUTE_COPY_CONTENT_URL = _BASE_API_URL + "/copyContent" 25 _API_ROUTE_CREATE_FOLDER_URL = _BASE_API_URL + "/createFolder" 26 _API_ROUTE_SET_OPTION_URL = _BASE_API_URL + "/setOption" 27 28 _API_ROUTE_DOWNLOAD_PATH = "download" 29 _API_ROUTE_UPLOAD_CONTENT_PATH = "uploadFile" 30 31 _API_STORE_FORMAT = "https://{}.{}/{}" 32 33 def __init__(self, token: str = None, get_account: bool = True, verbose: bool = False): 34 self.token = token 35 self.server = GofileClient.get_best_server() 36 self.verbose = verbose 37 38 if get_account and token: 39 self.get_account() 40 41 @staticmethod 42 def handle_response(resp: requests.Response): 43 code = resp.status_code 44 data = resp.json() 45 got = None 46 api_status = '' 47 48 if data: 49 api_status = data.get('status') 50 got = data.get('data') 51 52 53 if api_status != 'ok' or code != 200: 54 raise GofileAPIException.__init_from_resp__(resp) 55 return got 56 57 def get_best_upload_url(self): 58 return self._API_STORE_FORMAT.format(self.server, self._BASE_DOMAIN, self._API_ROUTE_UPLOAD_CONTENT_PATH) 59 60 61 @staticmethod 62 def get_best_server(throw_if_not_200=False): 63 resp = requests.get(GofileClient._API_ROUTE_GET_SERVER_URL) 64 return GofileClient.handle_response(resp)['server'] 65 66 67 def _download_file_from_direct_link(self, direct_link, out_dir="./"): 68 fn = direct_link.rsplit('/', 1)[1] 69 resp = requests.get(direct_link, stream=True, allow_redirects=None) 70 if resp.status_code != 200: 71 raise GofileAPIException("Could not download file", code=resp.status_code) 72 73 out_path = os.path.join(out_dir, fn) 74 with open(out_path, 'wb') as f: 75 for chunk in resp.iter_content(chunk_size=1024): 76 if chunk: 77 f.write(chunk) 78 return out_path 79 80 81 def _get_token(self, token): 82 if not token: 83 token = self.token 84 if not self.token: 85 token = "" 86 return token 87 88 def upload(self, path: str=None, file: BufferedReader=None, parent_id: str=None, token: str=None) -> GofileFile: 89 if not file and not path: 90 raise ValueError("GofileClient.upload() requires a BufferedReader or file path") 91 92 upload_url = self.get_best_upload_url() 93 token = self._get_token(token) 94 95 data = {} 96 if parent_id: 97 data["folderId"] = parent_id 98 if token: 99 data["token"] = token 100 101 files = {} 102 if file: 103 files["file"] = file 104 elif path: 105 files["file"] = open(path, "rb") 106 107 resp = requests.post(upload_url, files=files, data=data) 108 got = GofileClient.handle_response(resp) 109 110 #Needed because json returned from API has different key values at this endpoint 111 got["id"] = got.get("fileId", None) 112 got["name"] = got.get("fileName", None) 113 114 return GofileFile._load_from_dict(got, client=self) 115 116 117 def _get_content_raw_resp(self, content_id: str, token: str = None): 118 token = self._get_token(token) 119 req_url = self._API_ROUTE_GET_CONTENT_URL + "?contentId="+content_id 120 121 if token: 122 req_url += "&token="+token 123 124 resp = requests.get(req_url) 125 data = GofileClient.handle_response(resp) 126 return resp, data 127 128 129 def get(self, content_id: str, token: str = None): 130 resp,data = self._get_content_raw_resp(content_id, token=token) 131 return GofileContent.__init_from_resp__(resp, client=self) 132 133 def get_folder(self, *args, **kwargs): 134 """Retrieves folder using content_id""" 135 return self.get(*args, **kwargs) 136 137 def delete(self, *content_ids: str, token: str = None): 138 """Calls Gofile API to delete provided content_ids.""" 139 token = self._get_token(token) 140 data = {"contentsId": ",".join(content_ids), "token": token} 141 resp = requests.delete(GofileClient._API_ROUTE_DELETE_CONTENT_URL, data=data) 142 got = GofileClient.handle_response(resp) 143 144 145 def _get_account_raw_resp(self, token: str = None): 146 token = self._get_token(token) 147 url = GofileClient._API_ROUTE_GET_ACCOUNT_URL + "?token=" + token 148 resp = requests.get(url) 149 data = GofileClient.handle_response(resp) 150 return resp, data 151 152 153 def get_account(self, token: str = None) -> GofileAccount: 154 """If token is provided returns specified account, otherwise token defaults to self.token. 155 \If token is default self.account is updated""" 156 token = self._get_token(token) 157 resp, data = self._get_account_raw_resp(token=token) 158 account = GofileAccount._load_from_dict(data) 159 account._client = self 160 account._raw = data 161 162 if account.token == self.token: 163 self.account = account #update client's copy of account 164 165 return account 166 167 def set_content_option(self, content_id: str, option: str, value, token: str = None): 168 """Sets content option like 'description', 'public', etc (more at gofile.io/api). Note that folder and file content have different options""" 169 token = self._get_token(token) 170 171 value = ContentOption._process_option_value(option, value) #checks file types and formats for api 172 173 data = { 174 "contentId": content_id, 175 "token": token, 176 "option": option, 177 "value": value 178 } 179 180 resp = requests.put(GofileClient._API_ROUTE_SET_OPTION_URL, data=data) 181 got = GofileClient.handle_response(resp) 182 183 184 def copy_content(self, *content_ids: str, parent_id: str = None, token: str = None): 185 """Copy provided content_ids to destination folder's content_id. Currently returns None because api doesn't return any information. Will have to query parent folder""" 186 if not parent_id: 187 raise ValueError("Must pass a parent folder id: parent_id=None") 188 189 token = self._get_token(token) 190 data = { 191 "contentsId": ",".join(content_ids), 192 "folderIdDest": parent_id, 193 "token": token 194 } 195 196 resp = requests.put(GofileClient._API_ROUTE_COPY_CONTENT_URL, data=data) 197 got = GofileClient.handle_response(resp) 198 199 """ 200 As of right now /copyContent does not return information about newly created content 201 For this reason copy_content will just return None 202 203 return GofileContent.__init_from_resp__(resp, client=self) 204 """ 205 206 def create_folder(self, name: str, parent_id: str, token: str = None): 207 """Creates folder in specified parent folder's content_id""" 208 token = self._get_token(token) 209 210 data = { 211 "parentFolderId": parent_id, 212 "folderName": name 213 } 214 215 data["token"] = token 216 217 resp = requests.put(GofileClient._API_ROUTE_CREATE_FOLDER_URL, data=data) 218 got = GofileClient.handle_response(resp) 219 220 return GofileContent.__init_from_resp__(resp, client=self)
41 @staticmethod 42 def handle_response(resp: requests.Response): 43 code = resp.status_code 44 data = resp.json() 45 got = None 46 api_status = '' 47 48 if data: 49 api_status = data.get('status') 50 got = data.get('data') 51 52 53 if api_status != 'ok' or code != 200: 54 raise GofileAPIException.__init_from_resp__(resp) 55 return got
88 def upload(self, path: str=None, file: BufferedReader=None, parent_id: str=None, token: str=None) -> GofileFile: 89 if not file and not path: 90 raise ValueError("GofileClient.upload() requires a BufferedReader or file path") 91 92 upload_url = self.get_best_upload_url() 93 token = self._get_token(token) 94 95 data = {} 96 if parent_id: 97 data["folderId"] = parent_id 98 if token: 99 data["token"] = token 100 101 files = {} 102 if file: 103 files["file"] = file 104 elif path: 105 files["file"] = open(path, "rb") 106 107 resp = requests.post(upload_url, files=files, data=data) 108 got = GofileClient.handle_response(resp) 109 110 #Needed because json returned from API has different key values at this endpoint 111 got["id"] = got.get("fileId", None) 112 got["name"] = got.get("fileName", None) 113 114 return GofileFile._load_from_dict(got, client=self)
133 def get_folder(self, *args, **kwargs): 134 """Retrieves folder using content_id""" 135 return self.get(*args, **kwargs)
Retrieves folder using content_id
137 def delete(self, *content_ids: str, token: str = None): 138 """Calls Gofile API to delete provided content_ids.""" 139 token = self._get_token(token) 140 data = {"contentsId": ",".join(content_ids), "token": token} 141 resp = requests.delete(GofileClient._API_ROUTE_DELETE_CONTENT_URL, data=data) 142 got = GofileClient.handle_response(resp)
Calls Gofile API to delete provided content_ids.
153 def get_account(self, token: str = None) -> GofileAccount: 154 """If token is provided returns specified account, otherwise token defaults to self.token. 155 \If token is default self.account is updated""" 156 token = self._get_token(token) 157 resp, data = self._get_account_raw_resp(token=token) 158 account = GofileAccount._load_from_dict(data) 159 account._client = self 160 account._raw = data 161 162 if account.token == self.token: 163 self.account = account #update client's copy of account 164 165 return account
If token is provided returns specified account, otherwise token defaults to self.token. \If token is default self.account is updated
167 def set_content_option(self, content_id: str, option: str, value, token: str = None): 168 """Sets content option like 'description', 'public', etc (more at gofile.io/api). Note that folder and file content have different options""" 169 token = self._get_token(token) 170 171 value = ContentOption._process_option_value(option, value) #checks file types and formats for api 172 173 data = { 174 "contentId": content_id, 175 "token": token, 176 "option": option, 177 "value": value 178 } 179 180 resp = requests.put(GofileClient._API_ROUTE_SET_OPTION_URL, data=data) 181 got = GofileClient.handle_response(resp)
Sets content option like 'description', 'public', etc (more at gofile.io/api). Note that folder and file content have different options
184 def copy_content(self, *content_ids: str, parent_id: str = None, token: str = None): 185 """Copy provided content_ids to destination folder's content_id. Currently returns None because api doesn't return any information. Will have to query parent folder""" 186 if not parent_id: 187 raise ValueError("Must pass a parent folder id: parent_id=None") 188 189 token = self._get_token(token) 190 data = { 191 "contentsId": ",".join(content_ids), 192 "folderIdDest": parent_id, 193 "token": token 194 } 195 196 resp = requests.put(GofileClient._API_ROUTE_COPY_CONTENT_URL, data=data) 197 got = GofileClient.handle_response(resp) 198 199 """ 200 As of right now /copyContent does not return information about newly created content 201 For this reason copy_content will just return None 202 203 return GofileContent.__init_from_resp__(resp, client=self) 204 """
Copy provided content_ids to destination folder's content_id. Currently returns None because api doesn't return any information. Will have to query parent folder
206 def create_folder(self, name: str, parent_id: str, token: str = None): 207 """Creates folder in specified parent folder's content_id""" 208 token = self._get_token(token) 209 210 data = { 211 "parentFolderId": parent_id, 212 "folderName": name 213 } 214 215 data["token"] = token 216 217 resp = requests.put(GofileClient._API_ROUTE_CREATE_FOLDER_URL, data=data) 218 got = GofileClient.handle_response(resp) 219 220 return GofileContent.__init_from_resp__(resp, client=self)
Creates folder in specified parent folder's content_id
463class GofileFolder (GofileContent): 464 children: list[GofileContent] 465 """Children of folder, either GofileFolder or GofileFile type""" 466 children_ids: list[str] 467 """List of childrens' content ids""" 468 time_created: int 469 """Time folder was created""" 470 total_size: int 471 """Total size of folders' contents (in bytes)""" 472 is_public: bool 473 """Is folder publicly accessible""" 474 is_owner: bool 475 """If caller is folder owner""" 476 is_root: bool 477 """If folder is root folder""" 478 has_password: bool 479 """If folder is password protected""" 480 description: str 481 """A user set description using set_option""" 482 tags: list 483 """User set tags using set_option""" 484 code: str 485 """Folder shortcode to access from the browser""" 486 487 def __init__(self, name: str, content_id: str, parent_id: str, client: GofileClient = None): 488 super().__init__(content_id, parent_id, _type="folder", client=client) 489 self.name = name 490 self.children = [] 491 self.children_ids = [] 492 self.total_size = None 493 self.total_download_cnt = None 494 self.time_created = None 495 self.tags = [] 496 self.is_public = None 497 self.is_owner = None 498 self.is_root = None 499 self.description = None 500 self.has_password = None 501 self.code = None 502 503 def __init_children_from_contents__(self, data: dict) -> None: 504 children = [] 505 506 self.children_ids = data.get("childs") 507 contents = data.get("contents", {}) 508 509 if contents.keys(): 510 for content in contents.values(): 511 child = GofileContent.__init_from_resp__({"data": content}, client=self._client) 512 children.append(child) 513 514 elif self.children_ids: 515 for child_id in self.children_ids: 516 child = GofileContent(child_id, parent_id=data["id"], client=self._client) 517 children.append(child) 518 return children 519 520 521 def _override_from_dict(self, data: dict) -> None: 522 self.content_id = data.get("id", self.content_id) 523 self.parent_id = data.get("parentFolder", self.parent_id) 524 self.name = data.get("name", self.name) 525 self.time_created = data.get("createTime", self.time_created) 526 self.is_public = data.get("public", self.is_public) 527 self.is_owner = data.get("isOwner", self.is_owner) 528 self.is_root = data.get("isRoot", False) 529 self.has_password = data.get("password", self.has_password) 530 self.description = data.get("description", self.description) 531 self.code = data.get("code", self.code) 532 self.total_size = data.get("totalSize", self.total_size) 533 self.total_download_cnt = data.get("totalDownloadCount", self.total_download_cnt) 534 self.children_ids = data.get("childs") 535 self.children = self.__init_children_from_contents__(data) 536 537 self.tags = data.get("tags", "").split(",") 538 if self.tags[0] == "": 539 self.tags = [] 540 541 542 543 self._raw = data 544 545 546 @staticmethod 547 def _load_from_dict(data: dict, client: GofileClient = None) -> GofileFolder: 548 parent_id = data.get("parentFolder", None) 549 folder = GofileFolder(data["name"], data["id"], parent_id, client=client) 550 folder._override_from_dict(data) 551 folder._raw = data 552 553 return folder 554 555 def upload(self, path: str = None, file: BufferedReader = None) -> GofileFile: 556 return self._client.upload(file=file, path=path, parent_id=self.content_id)
487 def __init__(self, name: str, content_id: str, parent_id: str, client: GofileClient = None): 488 super().__init__(content_id, parent_id, _type="folder", client=client) 489 self.name = name 490 self.children = [] 491 self.children_ids = [] 492 self.total_size = None 493 self.total_download_cnt = None 494 self.time_created = None 495 self.tags = [] 496 self.is_public = None 497 self.is_owner = None 498 self.is_root = None 499 self.description = None 500 self.has_password = None 501 self.code = None
Inherited Members
394class GofileFile (GofileContent): 395 time_created: int 396 """Time that file was uploaded""" 397 size: int 398 """Size of file (in bytes)""" 399 download_cnt: int 400 """Amount of times the file has been downloaded""" 401 mimetype: str 402 """Mimetype of file""" 403 server: str 404 """subdomain server from where file will be downloaded (Premium)""" 405 direct_link: str 406 """Direct link to download file (Premium)""" 407 page_link: str 408 """Page link to view and get download url for file""" 409 md5: str 410 """Hash function of file for verification""" 411 412 def __init__(self, content_id: str, parent_id: str, client: GofileClient = None): 413 super().__init__(content_id, parent_id, _type="file", client=client) 414 self.name = None 415 self.time_created = None 416 self.size = None 417 self.download_cnt = None 418 self.mimetype = None 419 self.server = None 420 self.direct_link = None 421 self.page_link = None 422 self.md5 = None 423 424 def _override_from_dict(self, data: dict) -> None: 425 self.content_id = data.get("id", self.content_id) 426 self.parent_id = data.get("parentFolder", self.parent_id) 427 self.name = data.get("name", self.name) 428 self.time_created = data.get("createTime", self.time_created) 429 self.size = data.get("size", self.size) 430 self.download_cnt = data.get("downloadCount", self.download_cnt) 431 self.mimetype = data.get("mimetype", self.mimetype) 432 self.md5 = data.get("md5", self.md5) 433 self.server = data.get("serverChoosen", None) 434 link = data.get("link") 435 if link: 436 self.direct_link = self._get_direct_link_from_link(link) 437 self.page_link = data.get("downloadPage", self.page_link) 438 439 self._raw = data 440 441 @staticmethod 442 def _get_direct_link_from_link (link): 443 idx = link.find("download/") + len("download/")-1 444 return link[:idx] + "/direct/" + link[idx+1:] 445 446 @staticmethod 447 def _load_from_dict(data: dict, client: GofileClient = None): 448 file = GofileFile(data["id"], data["parentFolder"], client=client) 449 file._override_from_dict(data) 450 file._raw = data 451 return file 452 453 def download(self, out_dir: str = "./") -> str: 454 """Downloads file to passed dir (default is working directory). Note: The option directLink 455 \needs to be True (Premium)""" 456 457 if self.direct_link: 458 return self._client._download_file_from_direct_link(self.direct_link, out_dir=out_dir) 459 460 else: 461 raise Exception("Direct link needed - set option directLink=True (only for premium users)")
412 def __init__(self, content_id: str, parent_id: str, client: GofileClient = None): 413 super().__init__(content_id, parent_id, _type="file", client=client) 414 self.name = None 415 self.time_created = None 416 self.size = None 417 self.download_cnt = None 418 self.mimetype = None 419 self.server = None 420 self.direct_link = None 421 self.page_link = None 422 self.md5 = None
453 def download(self, out_dir: str = "./") -> str: 454 """Downloads file to passed dir (default is working directory). Note: The option directLink 455 \needs to be True (Premium)""" 456 457 if self.direct_link: 458 return self._client._download_file_from_direct_link(self.direct_link, out_dir=out_dir) 459 460 else: 461 raise Exception("Direct link needed - set option directLink=True (only for premium users)")
Downloads file to passed dir (default is working directory). Note: The option directLink
eeds to be True (Premium)
Inherited Members
274class GofileContent (object): 275 name: str 276 """Name of content""" 277 content_id: str 278 """Gofile API's unique content id""" 279 parent_id: str 280 """Content_id of parent folder""" 281 _type: str 282 """GofileContent subtypes, either 'file', 'folder' or 'unknown'.""" 283 is_file_type: bool 284 """If GofileContent is a file""" 285 is_folder_type: bool 286 """If GofileContent is a folder""" 287 is_unknown_type: bool 288 """If GofileContent is unknown (call reload() to update)""" 289 is_deleted: bool 290 """If content is deleted (will only register if called by it's own method delete())""" 291 292 def __init__(self, content_id: str, parent_id: str, _type: str = None, client: GofileClient = None): 293 self.content_id = content_id 294 self.parent_id = parent_id 295 self._type = _type 296 self._client = client 297 self._raw = {} 298 299 self.is_file_type = _type == "file" 300 self.is_folder_type = _type == "folder" 301 self.is_unknown_type = _type == None 302 self.name = None 303 self.time_created = None 304 self.is_deleted = False 305 306 def __repr__ (self) -> str: 307 _type = self._type 308 if not _type: 309 _type = "Unknown" 310 311 return "<Gofile {}: content_id={} name={}>".format(_type.upper(), self.content_id, self.name) 312 313 def delete (self) -> None: 314 """Deletes itself. When called successfully is_deleted = True""" 315 guest_token = self._raw.get("guestToken") 316 self._client.delete(self.content_id, token=guest_token) 317 self.is_deleted = True 318 319 def copy_to (self, dest_id: str) -> None: 320 """Copies itself to destination folder's content_id""" 321 self._client.copy_content(self.content_id, parent_id=dest_id) 322 323 def copy (self, dest_id: str) -> None: 324 self.copy_to(dest_id) 325 326 def set_option(self, option: str, value, reload: bool = True) -> None: 327 """Sets content option. Full option list available at m0bb1n.github.io/gofilepy/gofilepy/options.html""" 328 self._client.set_content_option(self.content_id, option, value) 329 if reload: 330 self.reload() #reload to get up to date information 331 332 def reload (self): 333 """Reloads any new updates to content. If is_unknown_type must call reload() before fully usable""" 334 if self.is_folder_type or (self.is_unknown_type and self.parent_id == None): 335 resp, data = self._client._get_content_raw_resp(self.content_id) 336 337 if self.is_unknown_type: 338 content = GofileContent.__init_from_resp__(resp, client=self._client) 339 340 elif self.is_folder_type: 341 self._override_from_dict(data) 342 return self 343 344 elif (self.is_unknown_type or self.is_file_type) and self.parent_id: 345 resp, data = self._client._get_content_raw_resp(self.parent_id) 346 content_data = data["contents"].get(self.content_id, None) 347 348 if content_data: 349 if self.is_unknown_type: 350 #re-init instance as sub class (GofileFile or GofileFolder) of GofileContent 351 match content_data['type']: 352 case "folder": 353 self.__class__ = GofileFolder 354 self.__init__( 355 content_data["name"], content_data["id"], 356 content_data["parentFolder"], client=self._client 357 ) 358 359 case "file": 360 self.__class__ = GofileFile 361 self.__init__( 362 content_data["id"], content_data["parentFolder"], 363 client=self._client 364 ) 365 366 case _: 367 raise TypeError("Type '{}' is not a valid option".format(content_data['type'])) 368 369 self._override_from_dict(content_data) 370 371 return self 372 373 else: 374 raise NotImplemented 375 376 @staticmethod 377 def __init_from_resp__ (resp: requests.Response, _type: str = None, client: GofileClient = None): 378 if type(resp) == requests.models.Response: 379 resp = resp.json() 380 381 got = resp["data"] 382 _type = got.get("type", _type) 383 content = None 384 385 if _type == "file": 386 content = GofileFile._load_from_dict(got, client=client) 387 elif _type == "folder": 388 content = GofileFolder._load_from_dict(got, client=client) 389 else: 390 raise NotImplemented 391 392 return content
292 def __init__(self, content_id: str, parent_id: str, _type: str = None, client: GofileClient = None): 293 self.content_id = content_id 294 self.parent_id = parent_id 295 self._type = _type 296 self._client = client 297 self._raw = {} 298 299 self.is_file_type = _type == "file" 300 self.is_folder_type = _type == "folder" 301 self.is_unknown_type = _type == None 302 self.name = None 303 self.time_created = None 304 self.is_deleted = False
313 def delete (self) -> None: 314 """Deletes itself. When called successfully is_deleted = True""" 315 guest_token = self._raw.get("guestToken") 316 self._client.delete(self.content_id, token=guest_token) 317 self.is_deleted = True
Deletes itself. When called successfully is_deleted = True
319 def copy_to (self, dest_id: str) -> None: 320 """Copies itself to destination folder's content_id""" 321 self._client.copy_content(self.content_id, parent_id=dest_id)
Copies itself to destination folder's content_id
326 def set_option(self, option: str, value, reload: bool = True) -> None: 327 """Sets content option. Full option list available at m0bb1n.github.io/gofilepy/gofilepy/options.html""" 328 self._client.set_content_option(self.content_id, option, value) 329 if reload: 330 self.reload() #reload to get up to date information
Sets content option. Full option list available at m0bb1n.github.io/gofilepy/gofilepy/options.html
332 def reload (self): 333 """Reloads any new updates to content. If is_unknown_type must call reload() before fully usable""" 334 if self.is_folder_type or (self.is_unknown_type and self.parent_id == None): 335 resp, data = self._client._get_content_raw_resp(self.content_id) 336 337 if self.is_unknown_type: 338 content = GofileContent.__init_from_resp__(resp, client=self._client) 339 340 elif self.is_folder_type: 341 self._override_from_dict(data) 342 return self 343 344 elif (self.is_unknown_type or self.is_file_type) and self.parent_id: 345 resp, data = self._client._get_content_raw_resp(self.parent_id) 346 content_data = data["contents"].get(self.content_id, None) 347 348 if content_data: 349 if self.is_unknown_type: 350 #re-init instance as sub class (GofileFile or GofileFolder) of GofileContent 351 match content_data['type']: 352 case "folder": 353 self.__class__ = GofileFolder 354 self.__init__( 355 content_data["name"], content_data["id"], 356 content_data["parentFolder"], client=self._client 357 ) 358 359 case "file": 360 self.__class__ = GofileFile 361 self.__init__( 362 content_data["id"], content_data["parentFolder"], 363 client=self._client 364 ) 365 366 case _: 367 raise TypeError("Type '{}' is not a valid option".format(content_data['type'])) 368 369 self._override_from_dict(content_data) 370 371 return self 372 373 else: 374 raise NotImplemented
Reloads any new updates to content. If is_unknown_type must call reload() before fully usable
222class GofileAccount (object): 223 token: str 224 """Token of account, found on Gofile.io""" 225 email: str 226 """Email address of account""" 227 tier: str 228 """Tier of GofileAccount either Standard or Premium""" 229 root_id: str 230 """Root folder's content_id of Gofile account""" 231 folder_cnt: int 232 """Number of children folders""" 233 file_cnt: int 234 """Number of children files""" 235 total_size: int 236 """Total size of Accounts' contents""" 237 total_download_cnt: int 238 """Total download count of Accounts' contents""" 239 240 def __init__(self, token: str = None): 241 self.token = token 242 self.email = None 243 self.tier = None 244 self.root_id = None 245 self.folder_cnt = None 246 self.file_cnt = None 247 self.total_size = None 248 self.total_download_cnt = None 249 self._raw = {} 250 251 def _override_from_dict(self, data: dict) -> None: 252 self.token = data.get("token", self.token) 253 self.email = data.get("email", self.email) 254 self.tier = data.get("tier", self.tier) 255 self.root_id = data.get("rootFolder", self.root_id) 256 self.folder_cnt = data.get("foldersCount", self.folder_cnt) 257 self.file_cnt = data.get("filesCount", self.file_cnt) 258 self.total_size = data.get("totalSize", self.total_size) 259 self.total_download_cnt = data.get("totalDownloadCount", self.total_download_cnt) 260 261 self._raw = data 262 263 @staticmethod 264 def _load_from_dict(data: dict): 265 account = GofileAccount(data["token"]) 266 account._override_from_dict(data) 267 return account 268 269 def reload(self): 270 resp, data = self._client._get_account_raw_resp(token=self.token) 271 self._override_from_dict(data) 272 self._raw = data