Skip to content

Commit

Permalink
Added new AllDebrid requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
aymene69 committed May 1, 2024
1 parent c6a03ae commit 1abd111
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
36 changes: 18 additions & 18 deletions source/debrid/alldebrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,41 @@ def __init__(self, config):
super().__init__(config)
self.base_url = "https://api.alldebrid.com/v4/"

def add_magnet(self, magnet):
url = f"{self.base_url}magnet/upload?agent=jackett&apikey={self.config['debridKey']}&magnet={magnet}"
def add_magnet(self, magnet, ip):
url = f"{self.base_url}magnet/upload?agent=jackett&apikey={self.config['debridKey']}&magnet={magnet}&ip={ip}"
return self.get_json_response(url)

def add_torrent(self, torrent_file):
url = f"{self.base_url}magnet/upload/file?agent=jackett&apikey={self.config['debridKey']}"
def add_torrent(self, torrent_file, ip):
url = f"{self.base_url}magnet/upload/file?agent=jackett&apikey={self.config['debridKey']}&ip={ip}"
files = {"files[0]": (str(uuid.uuid4()) + ".torrent", torrent_file, 'application/x-bittorrent')}
return self.get_json_response(url, method='post', files=files)

def check_magnet_status(self, id):
url = f"{self.base_url}magnet/status?agent=jackett&apikey={self.config['debridKey']}&id={id}"
def check_magnet_status(self, id, ip):
url = f"{self.base_url}magnet/status?agent=jackett&apikey={self.config['debridKey']}&id={id}&ip={ip}"
return self.get_json_response(url)

def unrestrict_link(self, link):
url = f"{self.base_url}link/unlock?agent=jackett&apikey={self.config['debridKey']}&link={link}"
def unrestrict_link(self, link, ip):
url = f"{self.base_url}link/unlock?agent=jackett&apikey={self.config['debridKey']}&link={link}&ip={ip}"
return self.get_json_response(url)

def get_stream_link(self, query_string):
def get_stream_link(self, query_string, ip):
query = json.loads(query_string)

magnet = query['magnet']
stream_type = query['type']
torrent_download = unquote(query["torrent_download"]) if query["torrent_download"] is not None else None

torrent_id = self.__add_magnet_or_torrent(magnet, torrent_download)
torrent_id = self.__add_magnet_or_torrent(magnet, torrent_download, ip)
logger.info(f"Torrent ID: {torrent_id}")

if not self.wait_for_ready_status(
lambda: self.check_magnet_status(torrent_id)["data"]["magnets"]["status"] == "Ready"):
lambda: self.check_magnet_status(torrent_id, ip)["data"]["magnets"]["status"] == "Ready"):
logger.error("Torrent not ready, caching in progress.")
return NO_CACHE_VIDEO_URL
logger.info("Torrent is ready.")

logger.info(f"Getting data for torrent id: {torrent_id}")
data = self.check_magnet_status(torrent_id)["data"]
data = self.check_magnet_status(torrent_id, ip)["data"]
logger.info(f"Retrieved data for torrent id")

link = NO_CACHE_VIDEO_URL
Expand Down Expand Up @@ -87,7 +87,7 @@ def get_stream_link(self, query_string):

logger.info(f"Alldebrid link: {link}")

unlocked_link_data = self.unrestrict_link(link)
unlocked_link_data = self.unrestrict_link(link, ip)

if not unlocked_link_data:
logger.error("Failed to unlock link.")
Expand All @@ -97,19 +97,19 @@ def get_stream_link(self, query_string):

return unlocked_link_data["data"]["link"]

def get_availability_bulk(self, hashes_or_magnets):
def get_availability_bulk(self, hashes_or_magnets, ip):
if len(hashes_or_magnets) == 0:
logger.info("No hashes to be sent to All-Debrid.")
return dict()

url = f"{self.base_url}magnet/instant?agent=jackett&apikey={self.config['debridKey']}&magnets[]={'&magnets[]='.join(hashes_or_magnets)}"
url = f"{self.base_url}magnet/instant?agent=jackett&apikey={self.config['debridKey']}&magnets[]={'&magnets[]='.join(hashes_or_magnets)}&ip={ip}"
return self.get_json_response(url)

def __add_magnet_or_torrent(self, magnet, torrent_download=None):
def __add_magnet_or_torrent(self, magnet, torrent_download=None, ip=None):
torrent_id = ""
if torrent_download is None:
logger.info(f"Adding magnet to AllDebrid")
magnet_response = self.add_magnet(magnet)
magnet_response = self.add_magnet(magnet, ip)
logger.info(f"AllDebrid add magnet response: {magnet_response}")

if not magnet_response or "status" not in magnet_response or magnet_response["status"] != "success":
Expand All @@ -122,7 +122,7 @@ def __add_magnet_or_torrent(self, magnet, torrent_download=None):
logger.info(f"Torrent file downloaded from Jackett")

logger.info(f"Adding torrent file to AllDebrid")
upload_response = self.add_torrent(torrent_file)
upload_response = self.add_torrent(torrent_file, ip)
logger.info(f"AllDebrid add torrent file response: {upload_response}")

if not upload_response or "status" not in upload_response or upload_response["status"] != "success":
Expand Down
12 changes: 7 additions & 5 deletions source/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ async def root():
@app.get("/configure")
@app.get("/{config}/configure")
async def configure(request: Request):
print(request.client.host)
return templates.TemplateResponse(
"index.html",
{"request": request, "isCommunityVersion": COMMUNITY_VERSION},
Expand Down Expand Up @@ -118,7 +119,7 @@ async def get_manifest():


@app.get("/{config}/stream/{stream_type}/{stream_id}")
async def get_results(config: str, stream_type: str, stream_id: str):
async def get_results(config: str, stream_type: str, stream_id: str, request: Request):
start = time.time()
stream_id = stream_id.replace(".json", "")

Expand Down Expand Up @@ -177,7 +178,8 @@ async def get_results(config: str, stream_type: str, stream_id: str):
if config['debrid']:
logger.debug("Checking availability")
hashes = torrent_smart_container.get_hashes()
result = debrid_service.get_availability_bulk(hashes)
ip = request.client.host
result = debrid_service.get_availability_bulk(hashes, ip)
torrent_smart_container.update_availability(result, type(debrid_service))
logger.debug("Checked availability (results: " + str(len(result.items())) + ")")

Expand All @@ -199,7 +201,7 @@ async def get_results(config: str, stream_type: str, stream_id: str):


@app.get("/playback/{config}/{query}")
async def get_playback(config: str, query: str):
async def get_playback(config: str, query: str, request: Request):
try:
if not query:
raise HTTPException(status_code=400, detail="Query required.")
Expand All @@ -208,9 +210,9 @@ async def get_playback(config: str, query: str):
query = decodeb64(query)
logger.info(query)
logger.info("Decoded query")

ip = request.client.host
debrid_service = get_debrid_service(config)
link = debrid_service.get_stream_link(query)
link = debrid_service.get_stream_link(query, ip)

logger.info("Got link: " + link)
return RedirectResponse(url=link, status_code=status.HTTP_301_MOVED_PERMANENTLY)
Expand Down

0 comments on commit 1abd111

Please sign in to comment.