def pattern_search(auth_info: dict, query: str = '', page: int = 1, page_size: int = 100, **kwargs) -> dict: """ Search and return pattern data. Returns data on the patterns themselves as well as pattern source data :param auth_info: auth for ravelry API :param query: String with search term if wanted - use like search box on site in combo with any filters passed in via kwargs :param page: page to pull from search data :param page_size: Size of records per page :param kwargs: Any filters to add to results - can use any from ravelry site :return: dict with 2 DataFrames containing the pattern data and pattern source data """ query_data = {} query_data.update({'query': query}) query_data.update({'page': page}) query_data.update({'page_size': page_size}) # Add any other passed in arguments for the search for key, value in kwargs.items(): if isinstance(value, list): value = ravelry_playground.serialized_list(value, '|') query_data.update({key: value}) result = ravelry_playground.ravelry_get_data(auth_info, 'patterns/search', data=query_data) return result.get('patterns')
def get_yarns(auth_info: dict, data: dict) -> dict: """ Get yarn related info for specific set of yarn IDs indicated in data input :param auth_info: auth for ravelry API :param data: data to use for api pull, in this case yarn IDs :return: Dict containing 3 dataframes with info on the yarn, fiber content for the yarn, and yarn photo info """ yarns = ravelry_playground.ravelry_get_data( auth_info, 'yarns', data=data ).get('yarns') yarn_info = [] yarn_fiber_content = [] yarn_photos = [] for yarn_id, yarn_data in yarns.items(): yarn = yarns.get(yarn_id) df_yarn = pd.json_normalize(yarn) df_yarn = df_yarn.rename(columns={'id': 'yarn_id'}) df_yarn = df_yarn.drop(['yarn_fibers', 'photos'], axis=1) yarn_info.append(df_yarn) df_fibers = pd.json_normalize( data=yarn, record_path='yarn_fibers' ) df_fibers['yarn_id'] = yarn_id df_fibers['used_fiber'] = np.arange(len(df_fibers)) df_fibers['total_used_fibers'] = len(df_fibers) df_fibers = df_fibers.rename(columns={'id': 'fiber_id'}) yarn_fiber_content.append(df_fibers) df_photos = pd.json_normalize( data=yarn, record_path='photos' ) df_photos['yarn_id'] = yarn_id df_photos['total_photos'] = len(df_photos) df_photos = df_photos.rename( columns={'id': 'photo_id', 'sort_order': 'photo_sort_order'}) yarn_photos.append(df_photos) df_yarn_info = pd.concat(yarn_info) df_yarn_fiber_info = pd.concat(yarn_fiber_content) df_yarn_photo_info = pd.concat(yarn_photos) yarn_data_output = { 'yarn_info': df_yarn_info, 'yarn_fiber_info': df_yarn_fiber_info, 'yarn_photo_info': df_yarn_photo_info } return yarn_data_output
def get_pattern_project_data(auth_info: dict, pattern_id: int, data: dict): """ Project related data for given pattern :param auth_info: auth for ravelry API :param pattern_id: pattern ID to get project data for :param data: :return: """ # ToDo In Progress pattern_project_info = ravelry_playground.ravelry_get_data( auth_info, f'patterns/{pattern_id}/projects', data=data) import json print(json.dumps(pattern_project_info, indent=2))
def get_pattern_data(auth_info: dict, data: dict) -> list: """ Pull pattern related data for specific pattern ids included in data input :param auth_info: auth for ravelry API :param data: dict with data to include in Ravelry request, in this case pattern ids :return: dict with pandas dataframes for various pattern related info """ pattern_details = ravelry_playground.ravelry_get_data( auth_info, 'patterns', data=data).get('patterns') pattern_data = [] for pattern_id, data in pattern_details.items(): data.update({'pattern_id': pattern_id}) pattern_data.append(data) return pattern_data
def get_num_pattern_search_results(auth_info: dict, query: str = '', page_size: int = 100, **kwargs) -> dict: query_data = {} query_data.update({'query': query}) query_data.update({'page': 1}) query_data.update({'page_size': page_size}) # Add any other passed in arguments for the search for key, value in kwargs.items(): if isinstance(value, list): value = ravelry_playground.serialized_list(value, '|') query_data.update({key: value}) result = ravelry_playground.ravelry_get_data(auth_info, 'patterns/search', data=query_data) paginator = result.get('paginator') return paginator