@@ -1833,8 +1833,7 @@ def swisstransfer(link):
18331833 r"https://www\.swisstransfer\.com/d/([\w-]+)(?::(\w+))?" , link
18341834 )
18351835 if not matched_link :
1836- print ("Invalid SwissTransfer link format." )
1837- return None
1836+ raise DirectDownloadLinkException (f"Invalid SwissTransfer link format ERROR: { link } " )
18381837
18391838 transfer_id , password = matched_link .groups ()
18401839 password = password or ""
@@ -1846,24 +1845,19 @@ def encode_password(password):
18461845
18471846 def getfile (transfer_id , password ):
18481847 url = f"https://www.swisstransfer.com/api/links/{ transfer_id } "
1849- if password :
1850- headers = {
1851- "User-Agent" : "Mozilla/5.0" ,
1852- "Authorization" : encode_password (password ),
1853- }
1854- else :
1855- headers = {
1856- "User-Agent" : "Mozilla/5.0" ,
1857- "Content-Type" : "application/json" ,
1858- }
1848+ headers = {
1849+ "User-Agent" : "Mozilla/5.0" ,
1850+ "Authorization" : encode_password (password ) if password else "" ,
1851+ "Content-Type" : "application/json" if not password else ""
1852+ }
18591853 response = get (url , headers = headers )
18601854
18611855 if response .status_code == 200 :
18621856 try :
18631857 return response .json (), headers
18641858 except ValueError :
1865- return None , None
1866- return None , headers
1859+ raise DirectDownloadLinkException ( f"ERROR: Error parsing JSON response { response . text } " )
1860+ raise DirectDownloadLinkException ( f"ERROR: Error fetching file details { response . status_code } , { response . text } " )
18671861
18681862 def gettoken (password , containerUUID , fileUUID ):
18691863 url = "https://www.swisstransfer.com/api/generateDownloadToken"
@@ -1882,9 +1876,8 @@ def gettoken(password, containerUUID, fileUUID):
18821876 if response .status_code == 200 :
18831877 return response .text .strip ().replace ('"' , "" )
18841878 raise DirectDownloadLinkException (
1885- f"Error generating download token: { response .status_code } , { response .text } "
1879+ f"ERROR: Error generating download token { response .status_code } , { response .text } "
18861880 )
1887- return None
18881881
18891882 data , headers = getfile (transfer_id , password )
18901883 if not data :
@@ -1896,10 +1889,17 @@ def gettoken(password, containerUUID, fileUUID):
18961889 files = data ["data" ]["container" ]["files" ]
18971890 folder_name = data ["data" ]["container" ]["message" ] or "unknown"
18981891 except (KeyError , IndexError , TypeError ) as e :
1899- raise DirectDownloadLinkException (f"Error parsing file details: { e } " )
1892+ raise DirectDownloadLinkException (f"ERROR: Error parsing file details { e } " )
19001893
19011894 total_size = sum (file ["fileSizeInBytes" ] for file in files )
19021895
1896+ if len (files ) == 1 : # Only one download link
1897+ file = files [0 ]
1898+ file_uuid = file ["UUID" ]
1899+ token = gettoken (password , container_uuid , file_uuid )
1900+ download_url = f"https://{ download_host } /api/download/{ transfer_id } /{ file_uuid } ?token={ token } "
1901+ return download_url , "User-Agent:Mozilla/5.0"
1902+
19031903 contents = []
19041904 for file in files :
19051905 file_uuid = file ["UUID" ]
0 commit comments