@@ -738,50 +738,69 @@ def delete_votes(self, vote_ids: List[int]):
738738
739739 def get_pair_for_voting (self , album_id : int = 1 ) -> Tuple [Optional [tuple ], Optional [tuple ]]:
740740 """
741- Get two media items for voting: one least voted and one random.
742-
743- Returns:
744- Tuple of (least voted media, random media), where each is a tuple of
745- (id, path, rating, votes) or None if not enough media items exist
741+ Get two media items for voting with adaptive rating difference handling.
746742 """
747- # Get current reliability
748743 media_count = self .get_total_media_count (album_id )
749744 total_votes = self .get_total_votes (album_id )
750745 reliability = ReliabilityCalculator .calculate_reliability (media_count , total_votes )
751746
752-
747+ # Get least voted item
753748 self .cursor .execute ("""
754- SELECT id, path, rating, votes
755- FROM media
756- WHERE album_id = ?
757- ORDER BY votes ASC, RANDOM()
758- LIMIT 1
759- """ , (album_id ,))
749+ SELECT id, path, rating, votes
750+ FROM media
751+ WHERE album_id = ?
752+ ORDER BY votes ASC, RANDOM()
753+ LIMIT 1
754+ """ , (album_id ,))
760755 least_voted = self .cursor .fetchone ()
761756
762757 if not least_voted :
763758 return None , None
764759
760+ # Adaptive rating difference logic
765761 if reliability >= 85 :
762+ # Try to find closest match first, then gradually expand search
766763 self .cursor .execute ("""
767- SELECT id, path, rating, votes
768- FROM media
769- WHERE id != ?
770- AND album_id = ?
771- AND ABS(rating - ?) <= 100
772- ORDER BY RANDOM()
773- LIMIT 1
774- """ , (least_voted [0 ], album_id , least_voted [2 ]))
764+ SELECT id, path, rating, votes
765+ FROM media
766+ WHERE id != ?
767+ AND album_id = ?
768+ ORDER BY
769+ CASE
770+ WHEN ABS(rating - ?) <= 100 THEN 0
771+ WHEN ABS(rating - ?) <= 200 THEN 1
772+ ELSE 2
773+ END,
774+ ABS(rating - ?) ASC,
775+ RANDOM()
776+ LIMIT 1
777+ """ , (least_voted [0 ], album_id , least_voted [2 ], least_voted [2 ], least_voted [2 ]))
775778 else :
779+ # Random selection for early stages
776780 self .cursor .execute ("""
777- SELECT id, path, rating, votes
778- FROM media
779- WHERE id != ? AND album_id = ?
780- ORDER BY RANDOM()
781- LIMIT 1
782- """ , (least_voted [0 ], album_id ))
781+ SELECT id, path, rating, votes
782+ FROM media
783+ WHERE id != ?
784+ AND album_id = ?
785+ ORDER BY RANDOM()
786+ LIMIT 1
787+ """ , (least_voted [0 ], album_id ))
788+
789+ second_item = self .cursor .fetchone ()
790+
791+ # Fallback if no matches found (should never happen with at least 2 items)
792+ if not second_item :
793+ self .cursor .execute ("""
794+ SELECT id, path, rating, votes
795+ FROM media
796+ WHERE id != ?
797+ AND album_id = ?
798+ ORDER BY RANDOM()
799+ LIMIT 1
800+ """ , (least_voted [0 ], album_id ))
801+ second_item = self .cursor .fetchone ()
783802
784- return least_voted , self . cursor . fetchone ()
803+ return least_voted , second_item
785804
786805 def close (self ):
787806 """Close the database connection."""
0 commit comments