<s> '''<STR_LIT>''' <EOL> from dbmodels import IssueRef <EOL> import db <EOL> import dbutils <EOL> from matchscore import MatchScore <EOL> import imagehash <EOL> import utils <EOL> __MATCH_THRESHOLD = <NUM_LIT> <EOL> def find_series_ref ( book , config ) : <EOL> '''<STR_LIT>''' <EOL> def are_the_same ( hash1 , hash2 ) : <EOL> x = imagehash . similarity ( hash1 , hash2 ) <EOL> return x > __MATCH_THRESHOLD <EOL> retval = None <EOL> series_ref = __find_best_series ( book , config ) <EOL> if series_ref : <EOL> matches = False <EOL> hash_local = __get_local_hash ( book ) <EOL> if hash_local : <EOL> ref = db . query_issue_ref ( series_ref , book . issue_num_s ) if book . issue_num_s else series_ref <EOL> ref = series_ref if not ref else ref <EOL> hash_remote = __get_remote_hash ( ref ) <EOL> matches = are_the_same ( hash_local , hash_remote ) <EOL> if not matches and type ( ref ) == IssueRef : <EOL> issue = db . query_issue ( ref , True ) <EOL> if issue : <EOL> for ref in issue . image_urls_sl : <EOL> hash_remote = __get_remote_hash ( ref ) <EOL> matches = are_the_same ( hash_local , hash_remote ) <EOL> if matches : break <EOL> retval = series_ref if matches else None <EOL> return retval ; <EOL> def __find_best_series ( book , config ) : <EOL> '''<STR_LIT>''' <EOL> series_refs = db . query_series_refs ( book . series_s , <EOL> config . ignored_searchterms_sl ) <EOL> series_refs = dbutils . filter_series_refs ( <EOL> series_refs , <EOL> config . ignored_publishers_sl , <EOL> config . ignored_before_year_n , <EOL> config . ignored_after_year_n , <EOL> config . never_ignore_threshold_n ) <EOL> primary = None <EOL> secondary = None <EOL> tertiary = None <EOL> if len ( series_refs ) > <NUM_LIT:0> : <EOL> mscore = MatchScore ( ) <EOL> def find_best_score ( refs ) : <EOL> return reduce ( lambda x , y : x if mscore . compute_n ( book , x ) <EOL> >= mscore . compute_n ( book , y ) else y , refs ) if refs else None <EOL> primary = find_best_score ( series_refs ) <EOL> if primary : <EOL> series_refs . remove ( primary ) <EOL> secondary = find_best_score ( series_refs ) <EOL> if secondary : <EOL> series_refs . remove ( secondary ) <EOL> tertiary = find_best_score ( series_refs ) <EOL> is_first_issue = ( lambda i : not i or ( utils . is_number ( i ) and float ( i ) == <NUM_LIT:1.0> ) ) ( book . issue_num_s ) <EOL> if is_first_issue and primary and secondary : <EOL> too_similar = False <EOL> SIMILARITY_THRESHOLD = __MATCH_THRESHOLD - <NUM_LIT> <EOL> hash1 = __get_remote_hash ( primary ) <EOL> hash2 = __get_remote_hash ( secondary ) <EOL> if imagehash . similarity ( hash1 , hash2 ) > SIMILARITY_THRESHOLD : <EOL> too_similar = True <EOL> elif tertiary : <EOL> hash3 = __get_remote_hash ( tertiary ) <EOL> if imagehash . similarity ( hash1 , hash3 ) > SIMILARITY_THRESHOLD : <EOL> too_similar = True <EOL> primary = None if too_similar else primary <EOL> return primary <EOL> def __get_local_hash ( book ) : <EOL> '''<STR_LIT>''' <EOL> hash = None <EOL> try : <EOL> image = book . create_image_of_page ( <NUM_LIT:0> ) if book else None ; <EOL> if image : <EOL> image = utils . strip_back_cover ( image ) <EOL> hash = imagehash . hash ( image ) <EOL> finally : <EOL> if "<STR_LIT:image>" in locals ( ) and image : image . Dispose ( ) <EOL> return hash <EOL> def __get_remote_hash ( ref ) : <EOL> '''<STR_LIT>''' <EOL> hash = None <EOL> try : <EOL> image = db . query_image ( ref ) if ref else None <EOL> if image : <EOL> image = utils . strip_back_cover ( image ) <EOL> hash = imagehash . hash ( image ) <EOL> finally : <EOL> if "<STR_LIT:image>" in locals ( ) and image : image . Dispose ( ) <EOL> return hash </s>
