from utils import s3 import utils.Google.Docs.read_sheet as gdoc def get_keywords(df, which='Release'): ''' Searches gDoc dataframe for keywords for ArtistName or ReleaseName. Returns line-delimted string of list of keywords. Called only from get_filters() ''' keys = df[ df[config.gdoc_col].isin(['Both', which]) ]['Keywords'].tolist() if keys: return '|'.join(keys) else: return def get_filters(sheet): ''' Breaks keywords and catagory into a dictionary containing which field to search for which keywords. ''' df_filter = gdoc.sheet_2_df(id=config.sheet_id, sheet=sheet, range=config.sheet_range) filter_dict = dict() df_filter[config.gdoc_col] = df_filter[config.gdoc_col].str.strip() df_filter['Keywords'] = df_filter['Keywords'].str.lstrip().str.rstrip() # search for Release and Both filter_dict['RELEASENAME'] = get_keywords(df_filter, which='Release') # search for Artist and Both filter_dict['ARTISTNAME'] = get_keywords(df_filter, which='Artist') for key in ['RELEASENAME', 'ARTISTNAME']: if not filter_dict[key]: filter_dict.pop(key, None) return filter_dict def find_keywords(df, keywords): ''' Takes the dictionary from get_filters() and the entire dataset. Searches the releasename and the artistname for keywords. True is returned if either row gets a hit. ''' release_keys = keywords.get('RELEASENAME') artist_keys = keywords.get('ARTISTNAME') if release_keys and artist_keys: hits = df['RELEASENAME'].str.contains(release_keys, case=False) \ | df['ARTISTNAME'].str.contains(artist_keys, case=False) elif release_keys and not artist_keys: hits = df['RELEASENAME'].str.contains(release_keys, case=False) elif artist_keys and not release_keys: hits = df['ARTISTNAME'].str.contains(artist_keys, case=False) return hits