-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e851372
commit e39f148
Showing
4 changed files
with
107 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import asyncio | ||
from functions.torboxFunctions import getDownloads | ||
import logging | ||
from library.tinfoil import errorMessage | ||
from fastapi.responses import JSONResponse | ||
|
||
ACCEPTABLE_SWITCH_FILES = [".nsp", ".nsz", ".xci", ".xcz"] | ||
|
||
|
||
async def generateIndex(base_url: str): | ||
""" | ||
Generates a JSON index from your TorBox files. Matches only switch compatible files and returns a well-formatted JSON index for Tinfoil. | ||
Requires: | ||
- base_url: where the server is hosted, so it can create proper URLs. | ||
Returns: | ||
- dict: the generated index for Tinfoil to use. | ||
""" | ||
|
||
success_message = "Welcome to your self-hosted TorBox Tinfoil Server! You are now able to directly download your files from TorBox to your switch.\n\n" | ||
files = [] | ||
try: | ||
# runs all requests in parallel for faster responses | ||
torrents, usenet_downloads, web_downloads = await asyncio.gather(getDownloads("torrents"), getDownloads("usenet"), getDownloads("webdl")) | ||
|
||
file_list = torrents + usenet_downloads + web_downloads | ||
|
||
|
||
except Exception as e: | ||
logging.error(f"There was an error generating the index. Error: {str(e)}") | ||
return JSONResponse( | ||
status_code=500, | ||
content=errorMessage(f"There was an error generating the index. Error: {str(e)}", error_code="UNKOWN_ERROR") | ||
) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import httpx | ||
from library.torbox import TORBOX_API_KEY, TORBOX_API_URL | ||
import logging | ||
import traceback | ||
|
||
async def getDownloads(type: str): | ||
""" | ||
Gets a download type list from TorBox. | ||
Requires: | ||
- type: the download type to be retrieved, either "torrents", "usenet", or "webdl" | ||
Returns: | ||
- file_list: a list containing all of the files retrieved from the TorBox API. | ||
""" | ||
|
||
if type not in ["torrents", "usenet", "webdl"]: | ||
logging.error("Please provide a type of either 'torrents', 'usenet' or 'webdl'.") | ||
return [] | ||
|
||
try: | ||
async with httpx.AsyncClient() as client: | ||
response = await client.get( | ||
url=f"{TORBOX_API_URL}/{type}/mylist", | ||
headers={ | ||
"Authorization": f"Bearer {TORBOX_API_KEY}", | ||
"User-Agent": "TorBox SelfHosted Tinfoil Server/1.0.0" | ||
} | ||
) | ||
if response.status_code != httpx.codes.OK: | ||
logging.error(f"Unable to retrieve TorBox {type} downloads. Response Code: {response.status_code}. Response: {response.json()}") | ||
return [] | ||
files = [] | ||
json = response.json() | ||
for download in json.get("data", {}): | ||
# only get downloads that are completely ready for download | ||
if not download.get("download_present", False): | ||
continue | ||
type = "download" | ||
id = download.get("id", None) | ||
for file in download.get("files", []): | ||
if not file.get("s3_path", None): | ||
continue | ||
try: | ||
files.append({ | ||
"type": type, | ||
"id": id, | ||
"file_id": file.get("id", 0), | ||
"name": file.get("s3_path", None).split("/")[-1], # gets only the filename of the file | ||
"size": file.get("size", 0) | ||
}) | ||
except Exception: | ||
logging.error(f"There was an error trying to add {type} download file to file list. Error: {str(e)}") | ||
continue | ||
return files | ||
except Exception as e: | ||
traceback.print_exc() | ||
logging.error(f"There was an error getting {type} downloads from TorBox. Error: {str(e)}") | ||
return [] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters