def process_iptc_codes(item, provider): """ Ensures that the higher level IPTC codes are present by inserting them if missing, for example if given 15039001 (Formula One) make sure that 15039000 (motor racing) and 15000000 (sport) are there as well :param item: A story item :return: A story item with possible expanded subjects """ try: def iptc_already_exists(code): for entry in item['subject']: if 'qcode' in entry and code == entry['qcode']: return True return False for subject in item['subject']: if 'qcode' in subject and len(subject['qcode']) == 8: top_qcode = subject['qcode'][:2] + '000000' if not iptc_already_exists(top_qcode): item['subject'].append({ 'qcode': top_qcode, 'name': subject_codes[top_qcode] }) mid_qcode = subject['qcode'][:5] + '000' if not iptc_already_exists(mid_qcode): item['subject'].append({ 'qcode': mid_qcode, 'name': subject_codes[mid_qcode] }) except Exception as ex: raise ProviderError.iptcError(ex, provider)
def process_iptc_codes(item, provider): """ Ensures that the higher level IPTC codes are present by inserting them if missing, for example if given 15039001 (Formula One) make sure that 15039000 (motor racing) and 15000000 (sport) are there as well :param item: A story item :return: A story item with possible expanded subjects """ try: def iptc_already_exists(code): for entry in item['subject']: if 'qcode' in entry and code == entry['qcode']: return True return False for subject in item['subject']: if 'qcode' in subject and len(subject['qcode']) == 8: top_qcode = subject['qcode'][:2] + '000000' if not iptc_already_exists(top_qcode): item['subject'].append({'qcode': top_qcode, 'name': subject_codes[top_qcode]}) mid_qcode = subject['qcode'][:5] + '000' if not iptc_already_exists(mid_qcode): item['subject'].append({'qcode': mid_qcode, 'name': subject_codes[mid_qcode]}) except Exception as ex: raise ProviderError.iptcError(ex, provider)
def process_iptc_codes(item, provider): """Ensures that the higher level IPTC codes are present by inserting them if missing. For example if given 15039001 (Formula One) make sure that 15039000 (motor racing) and 15000000 (sport) are there as well. :param item: A story item :return: A story item with possible expanded subjects """ try: def iptc_already_exists(code): for entry in item["subject"]: if "qcode" in entry and code == entry["qcode"]: return True return False for subject in item["subject"]: if "qcode" in subject and len( subject["qcode"]) == 8 and subject["qcode"].isdigit(): top_qcode = subject["qcode"][:2] + "000000" if not iptc_already_exists(top_qcode): try: item["subject"].append({ "qcode": top_qcode, "name": subject_codes[top_qcode] }) except KeyError: logger.warning( "missing qcode in subject_codes: {qcode}".format( qcode=top_qcode)) continue mid_qcode = subject["qcode"][:5] + "000" if not iptc_already_exists(mid_qcode): try: item["subject"].append({ "qcode": mid_qcode, "name": subject_codes[mid_qcode] }) except KeyError: logger.warning( "missing qcode in subject_codes: {qcode}".format( qcode=mid_qcode)) continue except Exception as ex: raise ProviderError.iptcError(ex, provider)