22import json
33import logging
44import argparse
5+ import os .path
56import traceback
67
78try :
@@ -289,8 +290,8 @@ def author(self) -> str:
289290 def direct_download_link (self , quality , mode ) -> str :
290291 """
291292 Returns the direct download URL for a given quality
292- :param quality:
293- :param mode:
293+ :param quality: 'best', 'half', 'worst', or a specific resolution like '720p'
294+ :param mode: The mode to filter links by (e.g., 'video')
294295 :return: str
295296 """
296297 if not self .enable_html :
@@ -313,49 +314,42 @@ def direct_download_link(self, quality, mode) -> str:
313314 available_links .append ((preference , href ))
314315 break
315316
316- # Filter and sort available links
317- available_links = [(res , link ) for res , link in available_links if res in quality_preferences ]
318- available_links .sort (key = lambda x : quality_preferences .index (x [0 ]))
317+ reversed_links = list (reversed (available_links ))
319318
320- if not available_links :
321- raise NotAvailable (f"No available links for quality '{ quality } ' and mode '{ mode } '." )
322-
323- # Select the range of qualities based on the specified quality
324319 if quality == "best" :
325- relevant_qualities = quality_preferences [:len (quality_preferences ) // 3 ]
320+ quality , url = reversed_links [0 ]
321+
326322 elif quality == "half" :
327- relevant_qualities = quality_preferences [len (quality_preferences ) // 3 : 2 * len (quality_preferences ) // 3 ]
323+ index_to_use = round (len (available_links ) / 2 )
324+ quality , url = reversed_links [index_to_use ]
325+
328326 elif quality == "worst" :
329- relevant_qualities = quality_preferences [2 * len (quality_preferences ) // 3 :]
330- else :
331- relevant_qualities = quality_preferences
327+ quality , url = reversed_links [- 1 ]
332328
333- # Return the first matching link from the relevant qualities
334- for preference in relevant_qualities :
335- for resolution , link in available_links :
336- if resolution == preference :
337- return urljoin ("https://eporner.com" , link )
329+ else :
330+ raise "No URLs available? Please report that"
338331
339- # Fallback to the lowest available quality
340- return urljoin ("https://eporner.com" , available_links [- 1 ][1 ])
332+ return urljoin ("https://eporner.com" , str (url ))
341333
342- def download (self , quality , path , callback = None , mode = Encoding .mp4_h264 ):
334+ def download (self , quality , path , callback = None , mode = Encoding .mp4_h264 , no_title = False ):
343335 if not self .enable_html :
344336 raise HTML_IS_DISABLED ("HTML content is disabled! See Documentation for more details" )
345337
346338 response_redirect_url = core .fetch (self .direct_download_link (quality , mode ),
347- allow_redirects = False )
339+ allow_redirects = True , get_response = True )
348340
349- if 'Location' in response_redirect_url .headers :
350- redirected_url = response_redirect_url .headers ['Location' ]
351- try :
352- core .legacy_download (stream = True , url = redirected_url , callback = callback , path = path )
353- return True
341+ if no_title is False :
342+ path = os .path .join (path , f"{ self .title } .mp4" )
343+
344+ try :
345+ core .legacy_download (url = str (response_redirect_url .url ), callback = callback , path = path )
346+ return True
347+
348+ except Exception :
349+ error = traceback .format_exc ()
350+ logger .error (error )
351+ return False
354352
355- except Exception :
356- error = traceback .format_exc ()
357- logger .error (error )
358- return False
359353
360354
361355class Pornstar :
@@ -530,16 +524,16 @@ def main():
530524 help = "(Optional) Specify a file with URLs (separated with new lines)" )
531525 parser .add_argument ("--output" , metavar = "Output directory" , type = str , help = "The output path (with filename)" ,
532526 required = True )
533- parser .add_argument ("--use -title" , metavar = "True,False" , type = bool ,
527+ parser .add_argument ("--no -title" , metavar = "True,False" , type = str ,
534528 help = "Whether to apply video title automatically to output path or not" , required = True )
535529
536530 args = parser .parse_args ()
531+ no_title = BaseCore ().str_to_bool (args .no_title )
537532
538533 if args .download :
539534 client = Client ()
540535 video = client .get_video (args .download , enable_html_scraping = True )
541- path = core .return_path (args = args , video = video )
542- video .download (quality = args .quality , path = path )
536+ video .download (quality = args .quality , path = args .output , no_title = no_title )
543537
544538 if args .file :
545539 videos = []
@@ -552,8 +546,7 @@ def main():
552546 videos .append (client .get_video (url , enable_html_scraping = True ))
553547
554548 for video in videos :
555- path = core .return_path (args = args , video = video )
556- video .download (quality = args .quality , path = path )
549+ video .download (quality = args .quality , path = args .output , no_title = no_title )
557550
558551
559552if __name__ == "__main__" :
0 commit comments