def crawl_country(context: Context, params, path, country): source_url = UI_URL % path context.log.debug("Crawling country: %s" % country) res = context.fetch_json(DATA_URL % path, params=params) data = res.get("result", {}).get("data", {}).get("page", {}) blocks = data.get("acf", {}).get("blocks", [{}])[0] content = blocks.get("free_form_content", []).get("content") doc = html.fromstring(content) function = None for i, el in enumerate(doc.getchildren()): text = el.text_content().strip() if el.tag == "h2": continue if el.tag == "h3": function = text continue if i == 0 and el.tag == "p": # this paragraph at the start is a note, not a person continue name = text.replace("(Acting)", "") if is_empty(name): continue context.log.debug( "Person", country=country, name=name, function=function, url=source_url, ) person = context.make("Person") person.id = context.make_slug(country, name, function) person.add("name", name) person.add("country", country) person.add("position", function) person.add("sourceUrl", source_url) person.add("topics", "role.pep") context.emit(person, target=True)
def crawl(context: Context): path = context.fetch_resource("source.xml", context.dataset.data.url) context.export_resource(path, XML, title=context.SOURCE_TITLE) doc = context.parse_resource_xml(path) for el in doc.findall(".//person"): fname = el.findtext("./fname") mname = el.findtext("./mname") lname = el.findtext("./lname") bdate = el.findtext("./birthdate") iin = el.findtext("./iin") name = h.make_name(given_name=fname, middle_name=mname, last_name=lname) entity_id = context.make_id(name, bdate, iin) entity = make_entity(context, el, "Person", entity_id) h.apply_name(entity, given_name=fname, middle_name=mname, last_name=lname) entity.add("innCode", iin) entity.add("birthDate", h.parse_date(bdate, FORMATS, bdate)) context.emit(entity, target=True) for el in doc.findall(".//org"): name = el.findtext(".//org_name") entity_id = context.make_id(el.findtext("./num"), name) entity = make_entity(context, el, "Organization", entity_id) for tag in (".//org_name", ".//org_name_en"): names = el.findtext(tag) if names is None: continue names = names.split("; ") entity.add("name", names) context.emit(entity, target=True)
def crawl_entity(context: Context, data): # context.pprint(data) nature = data.pop("Nature") schema = SCHEMATA.get(nature) if schema is None: context.log.error("Unknown entity type", nature=nature) return entity = context.make(schema) entity.id = context.make_slug(data.pop("IdRegistre")) sanction = h.make_sanction(context, entity) for detail in data.pop("RegistreDetail"): field = detail.pop("TypeChamp") for value in detail.pop("Valeur"): apply_prop(context, entity, sanction, field, value) name = data.pop("Nom") h.apply_name( entity, first_name=entity.first("firstName", quiet=True), tail_name=name, quiet=True, ) entity.add("topics", "sanction") context.emit(entity, target=True)
def crawl_legal(context: Context) -> None: data = json_resource(context, LEGAL_URL, "legal") for row in data: entity = context.make("Organization") entity.id = context.make_slug("org", row.pop("ukaz_id"), row.pop("index")) entity.add("name", row.pop("name_ukr", None)) entity.add("name", row.pop("name_original", None)) for alias in multi_split(row.pop("name_alternative", None), [";", "/"]): entity.add("alias", alias) entity.add("notes", row.pop("additional", None)) ipn = row.pop("ipn", "") or "" entity.add("taxNumber", ipn.replace("ІПН", "")) odrn = row.pop("odrn_edrpou", "") or "" odrn = odrn.replace("ОДРН", "") odrn = odrn.replace("ЄДРПОУ", "") entity.add("registrationNumber", odrn) handle_address(context, entity, row.pop("place", None)) handle_address(context, entity, row.pop("place_alternative", None)) handle_sanction(context, entity, row) # context.pprint(row) context.emit(entity, target=True)
def crawl(context: Context): path = context.fetch_resource("source.json", context.dataset.data.url) context.export_resource(path, JSON, title=context.SOURCE_TITLE) with open(path, "r") as fh: data = json.load(fh) for record in data: bank = context.make("Company") charter_no = record.pop("CharterNumber") bank_name = record.pop("BankName") bank.id = context.make_slug(charter_no, bank_name) bank.add("name", bank_name) bank.add("registrationNumber", charter_no) bank.add("country", "us") bank.add("topics", "fin.bank") if bank.id is not None: context.emit(bank) company_name = record.pop("CompanyName") first_name = record.pop("FirstName") last_name = record.pop("LastName") if company_name: entity = context.make("Company") entity.id = context.make_id(charter_no, bank_name, company_name) entity.add("name", company_name) else: entity = context.make("Person") entity.id = context.make_id(charter_no, bank_name, first_name, last_name) h.apply_name(entity, first_name=first_name, last_name=last_name) entity.add("country", "us") entity.add("topics", "crime.fin") addr = h.make_address( context, city=record.pop("CityName"), state=record.pop("StateName"), country_code="us", ) record.pop("StateAbbreviation") h.apply_address(context, entity, addr) sanction = h.make_sanction(context, entity) sanction.add("startDate", record.pop("CompleteDate", None)) sanction.add("endDate", record.pop("TerminationDate", None)) sanction.add("program", record.pop("EnforcementTypeDescription", None)) sanction.add("authorityId", record.pop("DocketNumber", None)) # context.pprint(record) context.emit(entity, target=True) context.emit(sanction)
def parse_individual(context: Context, node): person = context.make("Person") sanction = parse_common(context, person, node) person.add("title", values(node.find("./TITLE"))) person.add("position", values(node.find("./DESIGNATION"))) for alias in node.findall("./INDIVIDUAL_ALIAS"): parse_alias(person, alias) for addr in node.findall("./INDIVIDUAL_ADDRESS"): h.apply_address(context, person, parse_address(context, addr)) for doc in node.findall("./INDIVIDUAL_DOCUMENT"): passport = context.make("Passport") number = doc.findtext("./NUMBER") date = doc.findtext("./DATE_OF_ISSUE") type_ = doc.findtext("./TYPE_OF_DOCUMENT") if number is None and date is None and type_ is None: continue passport.id = context.make_id(person.id, number, date, type_) passport.add("holder", person) passport.add("passportNumber", number) passport.add("startDate", date) passport.add("type", type_) passport.add("type", doc.findtext("./TYPE_OF_DOCUMENT2")) passport.add("summary", doc.findtext("./NOTE")) country = doc.findtext("./COUNTRY_OF_ISSUE") country = country or doc.findtext("./ISSUING_COUNTRY") passport.add("country", country) context.emit(passport) for nat in node.findall("./NATIONALITY/VALUE"): person.add("nationality", nat.text) for dob in node.findall("./INDIVIDUAL_DATE_OF_BIRTH"): date = dob.findtext("./DATE") or dob.findtext("./YEAR") person.add("birthDate", date) for pob in node.findall("./INDIVIDUAL_PLACE_OF_BIRTH"): address = parse_address(context, pob) if address is not None: person.add("birthPlace", address.get("full")) person.add("country", address.get("country")) context.emit(person, target=True) context.emit(sanction)
def parse_relation(context: Context, el, parties): type_id = el.get("RelationTypeID") type_ = ref_value("RelationType", el.get("RelationTypeID")) from_id = context.make_slug(el.get("From-ProfileID")) from_party = parties.get(from_id) if from_party is None: context.log.warn("Missing relation 'from' party", entity_id=from_id, type=type_) return to_id = context.make_slug(el.get("To-ProfileID")) to_party = parties.get(to_id) if to_party is None: context.log.warn("Missing relation 'to' party", entity_id=to_id, type=type_) return # if type_id == "15003": # print( # "REL", # from_party.caption, # from_party.schema.name, # type_, # to_party.caption, # to_party.schema.name, # ) relation = lookup("relations", type_id) if relation is None: context.log.warn( "Unknown relation type", type_id=type_id, type_value=type_, from_party=from_party, to_party=to_party, ) return entity = context.make(relation.schema) from_range = entity.schema.get(relation.from_prop).range to_range = entity.schema.get(relation.to_prop).range # HACK: Looks like OFAC just has some link in a direction that makes no # semantic sense, so we're flipping them here. if disjoint_schema(from_party, from_range) or disjoint_schema(to_party, to_range): # context.log.warning( # "Flipped relation", # from_party=from_party, # to_party=to_party, # schema=entity.schema, # type=type_, # ) from_party, to_party = to_party, from_party add_schema(from_party, from_range) add_schema(to_party, to_range) context.emit(from_party, target=True) context.emit(to_party, target=True) entity.id = context.make_id("Relation", from_party.id, to_party.id, el.get("ID")) entity.add(relation.from_prop, from_party) entity.add(relation.to_prop, to_party) entity.add(relation.description_prop, type_) entity.add("summary", el.findtext("./Comment")) context.emit(entity) context.log.debug("Relation", from_=from_party, type=type_, to=to_party)
def parse_entry(context: Context, target, programs, places, updated_at): entity = context.make("LegalEntity") node = target.find("./entity") if node is None: node = target.find("./individual") entity = context.make("Person") if node is None: node = target.find("./object") object_type = node.get("object-type") if object_type != "vessel": context.log.warning("Unknown target type", target=target, object_type=object_type) entity = context.make("Vessel") entity.id = context.make_slug(target.get("ssid")) entity.add("gender", node.get("sex"), quiet=True) for other in node.findall("./other-information"): value = other.text.strip() if entity.schema.is_a("Vessel") and value.lower().startswith("imo"): _, imo_num = value.split(":", 1) entity.add("imoNumber", imo_num) else: entity.add("notes", h.clean_note(value)) sanction = h.make_sanction(context, entity) dates = set() for mod in target.findall("./modification"): dates.add(mod.get("publication-date")) sanction.add("listingDate", mod.get("publication-date")) sanction.add("startDate", mod.get("effective-date")) dates_ = [d for d in dates if d is not None] if len(dates_): entity.add("createdAt", min(dates_)) entity.add("modifiedAt", max(dates_)) ssid = target.get("sanctions-set-id") sanction.add("program", programs.get(ssid)) for justification in node.findall("./justification"): # TODO: should this go into sanction:reason? entity.add("notes", h.clean_note(justification.text)) for relation in node.findall("./relation"): rel_type = relation.get("relation-type") target_id = context.make_slug(relation.get("target-id")) res = context.lookup("relations", rel_type) if res is None: context.log.warn( "Unknown relationship type", type=rel_type, source=entity, target=target_id, ) continue rel = context.make(res.schema) rel.id = context.make_slug(relation.get("ssid")) rel.add(res.source, entity.id) rel.add(res.target, target_id) rel.add(res.text, rel_type) # rel_target = context.make(rel.schema.get(res.target).range) # rel_target.id = target_id # context.emit(rel_target) entity.add_schema(rel.schema.get(res.source).range) context.emit(rel) for identity in node.findall("./identity"): parse_identity(context, entity, identity, places) entity.add("topics", "sanction") context.emit(entity, target=True) context.emit(sanction)
def crawl_item(context: Context, listing): links = listing.get("links", {}) url = urljoin(API_URL, links.get("self")) data = http_get(context, url, cache_days=14) person = context.make("Person") _, officer_id = url.rsplit("/", 1) person.id = context.make_slug(officer_id) person.add("name", listing.get("title")) person.add("notes", listing.get("description")) person.add("topics", "crime") source_url = urljoin(WEB_URL, links.get("self")) person.add("sourceUrl", source_url) last_name = data.pop("surname", None) person.add("lastName", last_name) forename = data.pop("forename", None) person.add("firstName", forename) other_forenames = data.pop("other_forenames", None) person.add("middleName", other_forenames) person.add("title", data.pop("title", None)) nationality = data.pop("nationality", None) if nationality is not None: person.add("nationality", nationality.split(",")) person.add("birthDate", data.pop("date_of_birth", None)) person.add("topics", "crime") address = listing.get("address", {}) address = h.make_address( context, full=listing.get("address_snippet"), street=address.get("address_line_1"), street2=address.get("premises"), city=address.get("locality"), postal_code=address.get("postal_code"), region=address.get("region"), # country_code=person.first("nationality"), ) h.apply_address(context, person, address) for disqual in data.pop("disqualifications", []): case_id = disqual.get("case_identifier") sanction = h.make_sanction(context, person, key=case_id) sanction.add("recordId", case_id) sanction.add("startDate", disqual.get("disqualified_from")) sanction.add("endDate", disqual.get("disqualified_until")) sanction.add("listingDate", disqual.get("undertaken_on")) for key, value in disqual.get("reason", {}).items(): value = value.replace("-", " ") reason = f"{key}: {value}" sanction.add("reason", reason) sanction.add("country", "gb") context.emit(sanction) address = disqual.get("address", {}) address = h.make_address( context, full=listing.get("address_snippet"), street=address.get("address_line_1"), street2=address.get("premises"), city=address.get("locality"), postal_code=address.get("postal_code"), region=address.get("region"), # country_code=person.first("nationality"), ) for company_name in disqual.get("company_names", []): company = context.make("Company") company.id = context.make_slug("named", company_name) company.add("name", company_name) company.add("jurisdiction", "gb") context.emit(company) h.apply_address(context, company, address) directorship = context.make("Directorship") directorship.id = context.make_id(person.id, company.id) directorship.add("director", person) directorship.add("organization", company) context.emit(directorship) context.emit(person, target=True)
def crawl_persons(context: Context): for data in fetch(context, "personas"): entity = crawl_common(context, data, "personas", "Person") entity.add("title", values(data.pop("TITLE", None))) entity.add("nationality", values(data.pop("NATIONALITY", None))) entity.add("position", values(data.pop("DESIGNATION", None))) entity.add("gender", data.pop("GENDER", None)) entity.add("birthDate", data.pop("DATE_OF_BIRTH", None)) entity.add("birthDate", data.pop("YEAR", None)) entity.add("birthPlace", data.pop("CITY_OF_BIRTH", None)) entity.add("country", data.pop("COUNTRY_OF_BIRTH", None)) for dob in data.pop("INDIVIDUAL_DATE_OF_BIRTH", []): date = parse_date(dob.pop("DATE", None)) entity.add("birthDate", date) date = parse_date(dob.pop("TYPE_OF_DATE", None)) entity.add("birthDate", date) entity.add("birthDate", dob.pop("YEAR", None)) entity.add("birthDate", dob.pop("FROM_YEAR", None)) entity.add("birthDate", dob.pop("TO_YEAR", None)) h.audit_data(dob, ignore=["NOTE"]) for doc in data.pop("INDIVIDUAL_DOCUMENT", []): type_ = doc.pop("TYPE_OF_DOCUMENT", None) number = doc.pop("NUMBER", None) schema = context.lookup_value("doc_types", type_) if schema is None: context.log.warning("Unknown document type", type=type_) continue passport = context.make(schema) passport.id = context.make_id("ID", entity.id, number) passport.add("holder", entity) passport.add("type", type_) passport.add("number", number) passport.add("type", doc.pop("TYPE_OF_DOCUMENT2", None)) passport.add("startDate", parse_date(doc.pop("DATE_OF_ISSUE", None))) passport.add("country", doc.pop("ISSUING_COUNTRY", None)) passport.add("country", doc.pop("COUNTRY_OF_ISSUE", None)) passport.add("summary", doc.pop("NOTE", None)) context.emit(passport) h.audit_data(doc, ignore=["CITY_OF_ISSUE"]) for addr in data.pop("INDIVIDUAL_ADDRESS", []): address = parse_address(context, addr) h.apply_address(context, entity, address) for addr in data.pop("INDIVIDUAL_PLACE_OF_BIRTH", []): address = parse_address(context, addr) if address is not None: entity.add("birthPlace", address.get("full")) entity.add("country", address.get("country")) for alias in data.pop("INDIVIDUAL_ALIAS", []): entity.add("birthDate", alias.pop("DATE_OF_BIRTH", None)) entity.add("birthDate", alias.pop("YEAR", None)) entity.add("birthPlace", alias.pop("CITY_OF_BIRTH", None)) entity.add("country", alias.pop("COUNTRY_OF_BIRTH", None)) parse_alias(entity, alias) h.audit_data(data, ["VERSIONNUM"]) context.emit(entity, target=True)
def parse_row(context: Context, row): group_type = row.pop("GroupTypeDescription") schema = TYPES.get(group_type) if schema is None: context.log.error("Unknown group type", group_type=group_type) return entity = context.make(schema) entity.id = context.make_slug(row.pop("GroupID")) sanction = h.make_sanction(context, entity) sanction.add("program", row.pop("RegimeName")) sanction.add("authority", row.pop("ListingType", None)) listed_date = h.parse_date(row.pop("DateListed"), FORMATS) sanction.add("listingDate", listed_date) designated_date = h.parse_date(row.pop("DateDesignated"), FORMATS) sanction.add("startDate", designated_date) entity.add("createdAt", listed_date) if not entity.has("createdAt"): entity.add("createdAt", designated_date) sanction.add("authorityId", row.pop("UKSanctionsListRef", None)) sanction.add("unscId", row.pop("UNRef", None)) sanction.add("status", row.pop("GroupStatus", None)) sanction.add("reason", row.pop("UKStatementOfReasons", None)) last_updated = h.parse_date(row.pop("LastUpdated"), FORMATS) sanction.add("modifiedAt", last_updated) entity.add("modifiedAt", last_updated) # TODO: derive topics and schema from this?? entity_type = row.pop("Entity_Type", None) entity.add_cast("LegalEntity", "legalForm", entity_type) reg_number = row.pop("Entity_BusinessRegNumber", None) entity.add_cast("LegalEntity", "registrationNumber", reg_number) row.pop("Ship_Length", None) entity.add_cast("Vessel", "flag", row.pop("Ship_Flag", None)) flags = split_new(row.pop("Ship_PreviousFlags", None)) entity.add_cast("Vessel", "pastFlags", flags) entity.add_cast("Vessel", "type", row.pop("Ship_Type", None)) entity.add_cast("Vessel", "tonnage", row.pop("Ship_Tonnage", None)) entity.add_cast("Vessel", "buildDate", row.pop("Ship_YearBuilt", None)) entity.add_cast("Vessel", "imoNumber", row.pop("Ship_IMONumber", None)) ship_owner = row.pop("Ship_CurrentOwners", None) if ship_owner is not None: owner = context.make("LegalEntity") owner.id = context.make_slug("named", ship_owner) owner.add("name", ship_owner) context.emit(owner) ownership = context.make("Ownership") ownership.id = context.make_id(entity.id, "owns", owner.id) ownership.add("owner", owner) ownership.add("asset", entity) context.emit(ownership) countries = parse_countries(row.pop("Country", None)) entity.add("country", countries) title = split_items(row.pop("Title", None)) entity.add("title", title, quiet=True) pobs = split_items(row.pop("Individual_TownOfBirth", None)) entity.add_cast("Person", "birthPlace", pobs) dob = h.parse_date(row.pop("Individual_DateOfBirth", None), FORMATS) entity.add_cast("Person", "birthDate", dob) cob = parse_countries(row.pop("Individual_CountryOfBirth", None)) entity.add_cast("Person", "country", cob) nationalities = parse_countries(row.pop("Individual_Nationality", None)) entity.add_cast("Person", "nationality", nationalities) positions = split_items(row.pop("Individual_Position", None)) entity.add_cast("Person", "position", positions) entity.add_cast("Person", "gender", row.pop("Individual_Gender", None)) name_type = row.pop("AliasType", None) name_prop = NAME_TYPES.get(name_type) if name_prop is None: context.log.warning("Unknown name type", type=name_type) return name_quality = row.pop("AliasQuality", None) is_weak = WEAK_QUALITY.get(name_quality) if is_weak is None: context.log.warning("Unknown name quality", quality=name_quality) return h.apply_name( entity, name1=row.pop("name1", None), name2=row.pop("name2", None), name3=row.pop("name3", None), name4=row.pop("name4", None), name5=row.pop("name5", None), tail_name=row.pop("Name6", None), name_prop=name_prop, is_weak=is_weak, quiet=True, ) entity.add("alias", row.pop("NameNonLatinScript", None)) full_address = join_text( row.pop("Address1", None), row.pop("Address2", None), row.pop("Address3", None), row.pop("Address4", None), row.pop("Address5", None), row.pop("Address6", None), sep=", ", ) address = h.make_address( context, full=full_address, postal_code=row.pop("PostCode", None), country=first(countries), ) h.apply_address(context, entity, address) passport_number = row.pop("Individual_PassportNumber", None) passport_numbers = split_items(passport_number) entity.add_cast("Person", "passportNumber", passport_numbers) passport_detail = row.pop("Individual_PassportDetails", None) # passport_details = split_items(passport_detail) # TODO: where do I stuff this? ni_number = row.pop("Individual_NINumber", None) ni_numbers = split_items(ni_number) entity.add_cast("Person", "idNumber", ni_numbers) ni_detail = row.pop("Individual_NIDetails", None) # ni_details = split_items(ni_detail) # TODO: where do I stuff this? for phone in split_new(row.pop("PhoneNumber", None)): entity.add_cast("LegalEntity", "phone", phone) for email in split_new(row.pop("EmailAddress", None)): entity.add_cast("LegalEntity", "email", email) for website in split_new(row.pop("Website", None)): entity.add_cast("LegalEntity", "website", website) for name in parse_companies(context, row.pop("Entity_ParentCompany", None)): parent = context.make("Organization") parent.id = context.make_slug("named", name) parent.add("name", name) context.emit(parent) ownership = context.make("Ownership") ownership.id = context.make_id(entity.id, "owns", parent.id) ownership.add("owner", parent) ownership.add("asset", entity) context.emit(ownership) for name in parse_companies(context, row.pop("Entity_Subsidiaries", None)): subsidiary = context.make("Company") subsidiary.id = context.make_slug("named", name) subsidiary.add("name", name) context.emit(subsidiary) ownership = context.make("Ownership") ownership.id = context.make_id(entity.id, "owns", subsidiary.id) ownership.add("owner", entity) ownership.add("asset", subsidiary) context.emit(ownership) grp_status = row.pop("GrpStatus", None) if grp_status != "A": context.log.warning("Unknown GrpStatus", value=grp_status) entity.add("notes", h.clean_note(row.pop("OtherInformation", None))) h.audit_data(row, ignore=["NonLatinScriptLanguage", "NonLatinScriptType"]) entity.add("topics", "sanction") context.emit(entity, target=True) context.emit(sanction)
def emit_row(context: Context, sheet: str, section: str, row: Dict[str, List[str]]): schema = context.lookup_value("schema", section) if schema is None: context.log.warning("No schema for section", section=section, sheet=sheet) return entity = context.make(schema) entity.id = context.make_id(*row.get("name_english"), *row.get("name_japanese")) if entity.id is None: # context.pprint((sheet, row)) return entity.add("name", parse_names(row.pop("name_english"))) if not entity.has("name"): entity.add("name", parse_names(row.pop("name_japanese"))) else: entity.add("alias", parse_names(row.pop("name_japanese"))) entity.add("alias", parse_names(row.pop("alias", []))) entity.add("alias", parse_names(row.pop("known_alias", []))) entity.add("weakAlias", parse_names(row.pop("weak_alias", []))) entity.add("weakAlias", parse_names(row.pop("nickname", []))) entity.add("previousName", parse_names(row.pop("past_alias", []))) entity.add("previousName", parse_names(row.pop("old_name", []))) entity.add_cast("Person", "position", row.pop("position", [])) birth_date = parse_date(row.pop("birth_date", [])) entity.add_cast("Person", "birthDate", birth_date) entity.add_cast("Person", "birthPlace", row.pop("birth_place", [])) entity.add_cast("Person", "passportNumber", row.pop("passport_number", [])) entity.add("idNumber", row.pop("id_number", [])) entity.add("idNumber", row.pop("identification_number", [])) entity.add("notes", row.pop("other_information", [])) entity.add("notes", row.pop("details", [])) entity.add("phone", row.pop("phone", [])) entity.add("phone", row.pop("fax", [])) for address_full in row.pop("address", []): address = h.make_address(context, full=address_full) h.apply_address(context, entity, address) for address_full in row.pop("where", []): address = h.make_address(context, full=address_full) h.apply_address(context, entity, address) title = row.pop("title", []) if entity.schema.is_a("Person"): entity.add("title", title) else: entity.add("notes", title) entity.add("country", row.pop("citizenship", [])) entity.add("country", row.pop("activity_area", [])) sanction = h.make_sanction(context, entity) sanction.add("program", section) sanction.add("reason", row.pop("root_nomination", None)) sanction.add("reason", row.pop("reason_res1483", None)) sanction.add("recordId", row.pop("notification_number", None)) sanction.add("startDate", parse_date(row.pop("notification_date", []))) sanction.add("startDate", parse_date(row.pop("designated_date", []))) sanction.add("listingDate", parse_date(row.pop("publication_date", []))) row.pop("designated_un", None) # if len(row): # context.pprint(row) entity.add("topics", "sanction") context.emit(entity, target=True) context.emit(sanction)
def crawl_person(context: Context, name, url): context.log.debug("Crawling member", name=name, url=url) doc = context.fetch_html(url) _, person_id = url.rsplit("/", 1) person = context.make("Person") person.id = context.make_slug(person_id) person.add("sourceUrl", url) person.add("name", name) person.add("topics", "role.pep") last_name, first_name = name.split(", ", 1) person.add("firstName", first_name) person.add("lastName", last_name) address = {} details = doc.find('.//div[@class="regular-details"]') for row in details.findall('.//ul[@class="no-bullet"]/li'): children = row.getchildren() title = children[0] title_text = collapse_spaces(stringify(title.text_content())) title_text = title_text or title.get("class") value = collapse_spaces(title.tail) if title_text in ("Full name:", "Address:", "Declaration of interests"): # ignore these. continue if title_text == "Emails:": emails = [e.text for e in row.findall(".//a")] person.add("email", emails) continue if "glyphicon-phone" in title_text: person.add("phone", value.split(",")) continue if "fa-fax" in title_text: # TODO: yeah, no # person.add("phone", value) continue if title_text in ("Web sites:", "list-inline"): sites = [e.get("href") for e in row.findall(".//a")] person.add("website", sites) continue if title_text == "Represented Country:": person.add("country", value) continue if title_text == "Languages:": # TODO: missing in FtM # person.add("languages", value.split(',')) continue if "Regions since:" in title_text: date = h.parse_date(value, FORMATS) person.add("createdAt", date) continue if "Date of birth:" in title_text: person.add("birthDate", h.parse_date(value, FORMATS)) continue if "Commissions:" in title_text: for com in row.findall(".//li"): text = collapse_spaces(com.text_content()) sep = "Mandate - " if sep in text: _, text = text.split(sep, 1) person.add("sector", text) continue if "Areas of interest:" in title_text: for area in row.findall(".//li"): person.add("keywords", area.text_content()) continue if title.tag == "i" and value is None: person.add("position", title_text) continue if title_text in ("Country:"): person.add("country", value) if title_text in ("Street:", "Postal code:", "City:", "Country:"): address[title_text.replace(":", "")] = value continue if title_text == "Political group:": group = context.make("Organization") group.add("name", value) slug = value if "(" in slug: _, slug = slug.rsplit("(", 1) slug = slugify(slug, sep="-") group.id = f"eu-cor-group-{slug}" context.emit(group) member = context.make("Membership") member.id = context.make_id("Membership", person.id, group.id) member.add("member", person) member.add("organization", group) context.emit(member) continue address = h.make_address( context, street=address.get("Street"), city=address.get("City"), postal_code=address.get("Posal code"), country=address.get("Country"), ) h.apply_address(context, person, address) context.emit(person, target=True)
def crawl_person(context: Context, data: Dict[str, Any]): is_pep = data.pop("is_pep", False) entity = context.make("Person", target=is_pep) wikidata_id = clean_wdid(data.pop("wikidata_id", None)) entity.id = person_id(context, data.pop("id"), wikidata_id) entity.add("sourceUrl", data.pop("url_en", None)) data.pop("url_ru", None) entity.add("modifiedAt", data.pop("last_change", None)) entity.add("wikidataId", wikidata_id) entity.add("name", data.pop("full_name_en", None)) entity.add("name", data.pop("full_name_ru", None)) entity.add("alias", data.pop("inversed_full_name_en", None)) entity.add("alias", data.pop("inversed_full_name_ru", None)) entity.add("alias", data.pop("also_known_as_en", None)) entity.add("alias", data.pop("also_known_as_ru", None)) entity.add("alias", split_names(data.pop("names", []))) entity.add("birthDate", parse_date(data.pop("date_of_birth", None))) entity.add("deathDate", parse_date(data.pop("termination_date_human", None))) entity.add("birthPlace", data.pop("city_of_birth_ru", None)) entity.add("birthPlace", data.pop("city_of_birth_en", None)) entity.add("innCode", data.pop("inn", None)) entity.add("firstName", data.pop("first_name_en", None)) entity.add("firstName", data.pop("first_name_ru", None)) entity.add("fatherName", data.pop("patronymic_en", None)) entity.add("fatherName", data.pop("patronymic_ru", None)) entity.add("lastName", data.pop("last_name_en", None)) entity.add("lastName", data.pop("last_name_ru", None)) for suffix in ("", "_en", "_ru"): role = data.pop(f"last_job_title{suffix}", None) org = data.pop(f"last_workplace{suffix}", None) if org is None or not len(org.strip()): continue position = org if role is not None and len(role.strip()): position = f"{org} ({role})" entity.add("position", position) for country_data in data.pop("related_countries", []): rel_type = country_data.pop("relationship_type") country_name = country_data.pop("to_country_en", None) country_name = country_name or country_data.pop("to_country_ru") # print(country_name) res = context.lookup("country_links", rel_type) if res is None: context.log.warn( "Unknown country link", rel_type=rel_type, entity=entity, country=country_name, ) continue if res.prop is not None: entity.add(res.prop, country_name) # h.audit_data(country_data) for rel_data in data.pop("related_persons", []): other_pep = rel_data.pop("is_pep", False) other_wdid = clean_wdid(rel_data.pop("person_wikidata_id")) other = context.make("Person", target=other_pep) other.id = person_id(context, rel_data.pop("person_id"), other_wdid) other.add("name", rel_data.pop("person_en", None)) other.add("name", rel_data.pop("person_ru", None)) other.add("wikidataId", other_wdid) rel_type = rel_data.pop("relationship_type_en", None) rel_type_ru = rel_data.pop("relationship_type_ru", None) rel_type = rel_type or rel_type_ru res = context.lookup("person_relations", rel_type) if res is None: context.log.warn( "Unknown person/person relation type", rel_type=rel_type, entity=entity, other=other, ) continue # print("LINK", (entity.id, other.id)) id_a, id_b = sorted((entity.id, other.id)) rel = context.make(res.schema) id_a_short = short_id(context, id_a) id_b_short = short_id(context, id_b) rel.id = context.make_slug(id_a_short, res.schema, id_b_short) rel.add(res.from_prop, id_a) rel.add(res.to_prop, id_b) rel.add(res.desc_prop, rel_type) rel.add("modifiedAt", parse_date(rel_data.pop("date_confirmed"))) rel.add("startDate", parse_date(rel_data.pop("date_established"))) rel.add("endDate", parse_date(rel_data.pop("date_finished"))) # h.audit_data(rel_data) context.emit(other, target=other_pep) context.emit(rel) data.pop("type_of_official_ru", None) person_type = data.pop("type_of_official_en", None) person_topic = context.lookup_value("person_type", person_type) if person_topic is None: context.log.warn("Unknown type of official", type=person_type) entity.add("topics", person_topic) if is_pep: entity.add("topics", "role.pep") entity.add("status", person_type) data.pop("died", None) data.pop("tags", None) data.pop("reason_of_termination_en", None) data.pop("reason_of_termination_ru", None) # TODO: store images data.pop("photo", None) data.pop("related_companies", None) data.pop("declarations", None) # h.audit_data(data) context.emit(entity, target=is_pep)
def crawl_company(context: Context, data: Dict[str, Any]): entity = context.make("Organization") entity.id = company_id(context, data.pop("id")) entity.add("sourceUrl", data.pop("url_en", None)) data.pop("url_ru", None) entity.add("name", data.pop("name_en", None)) entity.add("name", data.pop("name_ru", None)) entity.add("name", data.pop("name_suggest_output_ru", None)) entity.add("alias", data.pop("also_known_as", None)) entity.add("alias", data.pop("short_name_en", None)) entity.add("alias", data.pop("short_name_ru", None)) entity.add("incorporationDate", parse_date(data.pop("founded", None))) entity.add("dissolutionDate", parse_date(data.pop("closed", None))) entity.add("status", data.pop("status_en", data.pop("status_ru", None))) entity.add("status", data.pop("status", None)) entity.add_cast("Company", "ogrnCode", data.pop("ogrn_code", None)) entity.add("registrationNumber", data.pop("edrpou", None)) for country_data in data.pop("related_countries", []): rel_type = country_data.pop("relationship_type") country_name = country_data.pop("to_country_en", None) country_name = country_name or country_data.pop("to_country_ru") # print(country_name) res = context.lookup("country_links", rel_type) if res is None: context.log.warn( "Unknown country link", rel_type=rel_type, entity=entity, country=country_name, ) continue if res.prop is not None: entity.add(res.prop, country_name) # h.audit_data(country_data) for rel_data in data.pop("related_persons", []): other_wdid = clean_wdid(rel_data.pop("person_wikidata_id")) other_id = person_id(context, rel_data.pop("person_id"), other_wdid) rel_type = rel_data.pop("relationship_type_en", None) rel_type_ru = rel_data.pop("relationship_type_ru", None) rel_type = rel_type or rel_type_ru res = context.lookup("person_relations", rel_type) if res is None: context.log.info( "Unknown company/person relation type", rel_type=rel_type, entity=entity, other=other_id, ) continue if res.schema is None: continue if res.schema == "Organization" and res.from_prop == "asset": entity.schema = model.get("Company") rel = context.make(res.schema) id_a_short = short_id(context, entity.id) id_b_short = short_id(context, other_id) rel.id = context.make_slug(id_a_short, res.schema, id_b_short) rel.add(res.from_prop, entity.id) rel.add(res.to_prop, other_id) rel.add(res.desc_prop, rel_type) rel.add("modifiedAt", parse_date(rel_data.pop("date_confirmed"))) rel.add("startDate", parse_date(rel_data.pop("date_established"))) rel.add("endDate", parse_date(rel_data.pop("date_finished"))) context.emit(rel) for rel_data in data.pop("related_companies", []): # pprint(rel_data) # other_id = company_id(context, rel_data.pop("company_id")) # rel_type = rel_data.pop("relationship_type_en", None) # rel_type_ru = rel_data.pop("relationship_type_ru", None) # rel_type = rel_type or rel_type_ru # res = context.lookup("company_relations", rel_type) # if res is None: # context.log.warn( # "Unknown company/company relation type", # rel_type=rel_type, # entity=entity, # other=other_id, # ) # continue # if res.schema is None: # continue # if res.schema == "Organization" and res.from_prop == "asset": # entity.schema = model.get("Company") # rel = context.make(res.schema) # id_a_short = short_id(context, entity.id) # id_b_short = short_id(context, other_id) # rel.id = context.make_slug(id_a_short, res.schema, id_b_short) # rel.add(res.from_prop, entity.id) # rel.add(res.to_prop, other_id) # rel.add(res.desc_prop, rel_type) # rel.add("modifiedAt", parse_date(rel_data.pop("date_confirmed"))) # rel.add("startDate", parse_date(rel_data.pop("date_established"))) # rel.add("endDate", parse_date(rel_data.pop("date_finished"))) # context.emit(rel) pass address = h.make_address( context, street=data.pop("street", None), city=data.pop("city", None), ) h.apply_address(context, entity, address) if data.pop("state_company", False): entity.add("topics", "gov.soe") ignore = [ "wiki", "bank_name", "other_founders", "other_owners", "other_managers", "other_recipient", ] h.audit_data(data, ignore=ignore) # print(entity.to_dict()) context.emit(entity)
def parse_entry(context: Context, entry): entity = context.make("LegalEntity") if entry.findtext("./type-entry") == "2": entity = context.make("Person") entry_id = entry.findtext("number-entry") entity.id = context.make_slug(entry_id) sanction = h.make_sanction(context, entity) sanction.add("program", entry.findtext("./program-entry")) date_entry = entry.findtext("./date-entry") if date_entry: date = datetime.strptime(date_entry, "%Y%m%d") entity.add("createdAt", date.date()) sanction.add("listingDate", date.date()) sanction.add("startDate", date.date()) for aka in entry.findall("./aka-list"): h.apply_name( entity, name1=aka.findtext("./aka-name1"), name2=aka.findtext("./aka-name2"), name3=aka.findtext("./aka-name3"), tail_name=aka.findtext("./aka-name4"), alias=aka.findtext("type-aka") != "N", is_weak=aka.findtext("./quality-aka") == "2", quiet=True, ) for node in entry.findall("./title-list"): entity.add("title", node.text, quiet=True) for doc in entry.findall("./document-list"): reg = doc.findtext("./document-reg") number = doc.findtext("./document-id") country = doc.findtext("./document-country") passport = context.make("Passport") passport.id = context.make_id("Passport", entity.id, reg, number, country) passport.add("holder", entity) passport.add("passportNumber", number) passport.add("summary", reg) passport.add("country", country) context.emit(passport) for doc in entry.findall("./id-number-list"): entity.add("idNumber", doc.text) for node in entry.findall("./address-list"): address = h.make_address(context, full=node.findtext("./address")) h.apply_address(context, entity, address) for pob in entry.findall("./place-of-birth-list"): entity.add_cast("Person", "birthPlace", pob.text) for dob in entry.findall("./date-of-birth-list"): date = parse_date(dob.text) entity.add_cast("Person", "birthDate", date) for nat in entry.findall("./nationality-list"): for country in multi_split(nat.text, [";", ","]): country = remove_bracketed(country) entity.add("nationality", country, quiet=True) entity.add("topics", "sanction") context.emit(entity, target=True) context.emit(sanction)
def parse_entry(context: Context, entry: Element): subject_type = entry.find("./subjectType") schema = context.lookup_value( "subject_type", subject_type.get("code"), dataset="eu_fsf", ) if schema is None: context.log.warning("Unknown subject type", type=subject_type) return entity = context.make(schema) eu_ref = entry.get("euReferenceNumber") if eu_ref is not None: entity.id = context.make_slug(eu_ref, dataset="eu_fsf") else: entity.id = context.make_slug("logical", entry.get("logicalId")) entity.add("notes", h.clean_note(entry.findtext("./remark"))) entity.add("topics", "sanction") parse_sanctions(context, entity, entry) for name in entry.findall("./nameAlias"): is_weak = not as_bool(name.get("strong")) h.apply_name( entity, full=name.get("wholeName"), first_name=name.get("firstName"), middle_name=name.get("middleName"), last_name=name.get("lastName"), is_weak=is_weak, quiet=True, ) entity.add("title", name.get("title"), quiet=True) entity.add("position", name.get("function"), quiet=True) entity.add("gender", name.get("gender"), quiet=True) for node in entry.findall("./identification"): type = node.get("identificationTypeCode") schema = "Passport" if type == "passport" else "Identification" passport = context.make(schema) passport.id = context.make_id("ID", entity.id, node.get("logicalId")) passport.add("holder", entity) passport.add("authority", node.get("issuedBy")) passport.add("type", node.get("identificationTypeDescription")) passport.add("number", node.get("number")) passport.add("number", node.get("latinNumber")) passport.add("startDate", node.get("issueDate")) passport.add("startDate", node.get("issueDate")) passport.add("country", parse_country(node)) passport.add("country", node.get("countryDescription")) for remark in node.findall("./remark"): passport.add("summary", remark.text) context.emit(passport) for node in entry.findall("./address"): address = parse_address(context, node) h.apply_address(context, entity, address) for child in node.getchildren(): if child.tag in ("regulationSummary"): continue elif child.tag == "remark": entity.add("notes", child.text) elif child.tag == "contactInfo": prop = context.lookup_value( "contact_info", child.get("key"), dataset="eu_fsf", ) if prop is None: context.log.warning("Unknown contact info", node=child) else: entity.add(prop, child.get("value")) else: context.log.warning("Unknown address component", node=child) for birth in entry.findall("./birthdate"): partialBirth = parse_parts(birth.get("year"), birth.get("month"), birth.get("day")) entity.add("birthDate", birth.get("birthdate")) entity.add("birthDate", partialBirth) address = parse_address(context, birth) if address is not None: entity.add("birthPlace", address.get("full")) entity.add("country", address.get("country")) for node in entry.findall("./citizenship"): entity.add("nationality", parse_country(node), quiet=True) entity.add("nationality", node.get("countryDescription"), quiet=True) context.emit(entity, target=True)
def crawl_row(context: Context, data: Dict[str, str]): entity = context.make("LegalEntity") ind_id = data.pop("INDIVIDUAL_Id", data.pop("IndividualID")) entity.id = context.make_slug(ind_id) assert entity.id, data entity.add("notes", h.clean_note(data.pop("COMMENTS", None))) entity.add("notes", h.clean_note(data.pop("Comments", None))) entity.add("notes", h.clean_note(data.pop("NOTE", None))) entity.add("notes", h.clean_note(data.pop("NOTE1", None))) entity.add("notes", h.clean_note(data.pop("NOTE2", None))) entity.add("notes", h.clean_note(data.pop("NOTE3", None))) entity.add_cast("Person", "nationality", data.pop("NATIONALITY", None)) entity.add_cast("Person", "nationality", data.pop("Nationality", None)) entity.add_cast("Person", "title", data.pop("TITLE", None)) entity.add_cast("Person", "title", data.pop("Title", None)) entity.add_cast("Person", "position", data.pop("DESIGNATION", None)) entity.add_cast("Person", "position", data.pop("Designation", None)) entity.add_cast("Person", "birthPlace", data.pop("PLACEOFBIRTH", None)) entity.add_cast("Person", "birthPlace", data.pop("IndividualPlaceOfBirth", None)) entity.add_cast("Person", "birthPlace", data.pop("CITY_OF_BIRTH", None)) entity.add_cast("Person", "birthDate", data.pop("YEAR", None)) entity.add_cast("Person", "gender", data.pop("GENDER", None)) entity.add_cast("Person", "birthDate", parse_date(data.pop("DATE", None))) entity.add_cast("Person", "birthDate", parse_date(data.pop("DATE_OF_BIRTH", None))) dob = parse_date(data.pop("IndividualDateOfBirth", None)) entity.add_cast("Person", "birthDate", dob) data.pop("BIRTHPLACE_x0020_CITY", None) data.pop("BIRTHPLACE_x0020_STATE_PROVINCE", None) entity.add("country", data.pop("BIRTHPLACE_x0020_COUNTRY", None)) entity.add("country", data.pop("COUNTRY_OF_BIRTH", None)) entity.add_cast("Person", "birthPlace", data.pop("BIRTHPLACE_x0020_NOTE", None)) h.apply_name( entity, full=data.pop("FullName", None), given_name=data.pop("FIRST_NAME", None), second_name=data.pop("SECOND_NAME", None), name3=data.pop("THIRD_NAME", None), name4=data.pop("FOURTH_NAME", None), quiet=True, ) alias = data.pop("NAME_ORIGINAL_SCRIPT", None) if alias is not None and "?" not in alias: entity.add("alias", alias) entity.add("alias", data.pop("SORT_KEY", None)) data.pop("IndividualAlias", None) entity.add_cast("Person", "passportNumber", data.pop("PASSPORT", None)) entity.add_cast("Person", "passportNumber", data.pop("IndividualDocument", None)) data.pop("DATE_OF_ISSUE", None) data.pop("CITY_OF_ISSUE", None) entity.add("country", data.pop("COUNTRY_OF_ISSUE", None)) entity.add_cast("Person", "idNumber", data.pop("IDNUMBER", None)) address = h.make_address( context, # remarks=data.pop("NOTE"), full=data.pop("IndividualAddress", None), street=data.pop("STREET", None), city=data.pop("CITY", None), region=data.pop("STATE_PROVINCE", None), postal_code=data.pop("ZIP_CODE", None), country=data.pop("COUNTRY", None), ) h.apply_address(context, entity, address) sanction = h.make_sanction(context, entity) inserted_at = parse_date(data.pop("DateInserted", None)) listed_on = data.pop("ListedON", data.pop("ListedOn", None)) listed_at = parse_date(listed_on) entity.add("createdAt", inserted_at or listed_at) sanction.add("listingDate", listed_at or inserted_at) sanction.add("startDate", data.pop("FROM_YEAR", None)) sanction.add("endDate", data.pop("TO_YEAR", None)) sanction.add("program", data.pop("UN_LIST_TYPE", None)) sanction.add("unscId", data.pop("REFERENCE_NUMBER", None)) sanction.add("unscId", data.pop("ReferenceNumber", None)) sanction.add("authority", data.pop("SUBMITTED_BY", None)) entity.add("topics", "sanction") h.audit_data(data, ignore=["VERSIONNUM", "TYPE_OF_DATE", "ApplicationStatus"]) context.emit(entity, target=True) context.emit(sanction)
def crawl_organizations(context: Context): path = context.fetch_resource("organizations.xlsx", ORG_URL) context.export_resource(path, XLSX, title=context.SOURCE_TITLE) seq_ids = {} links = [] for record in excel_records(path): seq_id = record.pop("internal_seq_id", None) name_en = record.pop("organization_name_english", None) name_he = record.pop("organization_name_hebrew", None) entity = context.make("Organization") entity.id = context.make_id(name_en, name_he) if entity.id is None: continue if seq_id is not None: seq_ids[seq_id] = entity.id entity.add("name", name_en) entity.add("name", name_he) entity.add("topics", "crime.terror") entity.add("notes", lang_pick(record, "comments")) entity.add("notes", record.pop("column_42", None)) entity.add("email", record.pop("email", None)) entity.add("country", record.pop("country_hebrew", None)) entity.add("country", record.pop("country_english", None)) entity.add("registrationNumber", record.pop("corporation_id", None)) entity.add("legalForm", lang_pick(record, "corporation_type")) entity.add("jurisdiction", lang_pick(record, "location_of_formation")) date = parse_date(record.pop("date_of_corporation", None)) entity.add("incorporationDate", date) for field in list(record.keys()): if field.startswith("organization_name_"): entity.add("alias", record.pop(field, None)) if field.startswith("telephone"): entity.add("phone", record.pop(field, None)) if field.startswith("website"): entity.add("website", record.pop(field, None)) entity.add("phone", record.pop("column_70", None)) entity.add("website", record.pop("column_73", None)) sanction = h.make_sanction(context, entity) sanction.add("recordId", seq_id) sanction.add("recordId", record.pop("seq_num_in_other_countries", None)) sanction.add("program", record.pop("designation_type", None)) sanction.add("reason", lang_pick(record, "designation_justification")) sanction.add("authority", lang_pick(record, "designated_by")) sanction.add("publisher", record.pop("public_records_references", None)) lang_pick(record, "designated_by_abroad") record.pop("date_designated_in_other_countries", None) linked = record.pop("linked_to_internal_seq_id", "") for link in linked.split(";"): links.append((max(link, seq_id), min(link, seq_id))) street = lang_pick(record, "street") city = lang_pick(record, "city_village") if street or city: address = h.make_address( context, street=street, city=city, country_code=entity.first("country") ) h.apply_address(context, entity, address) for field in ( "date_of_temporary_designation", "date_of_permenant_designation", "date_designation_in_west_bank", ): sanction.add("startDate", parse_date(record.pop(field, None))) context.emit(entity, target=True) context.emit(sanction) if len(record): context.pprint(record) for (subject, object) in links: subject_id = seq_ids.get(subject) object_id = seq_ids.get(object) if subject_id is None or object_id is None: continue link = context.make("UnknownLink") link.id = context.make_id(subject_id, object_id) link.add("subject", subject_id) link.add("object", object_id) context.emit(link)
def crawl(context: Context): path = context.fetch_resource("source.html", context.dataset.data.url) context.export_resource(path, HTML, title=context.SOURCE_TITLE) with open(path, "r") as fh: doc = html.parse(fh) tables = doc.findall(".//table") assert len(tables) == 1 rows = tables[0].findall(".//tr") for row in rows[2:]: cells = [ collapse_spaces(c.text_content()) for c in row.findall("./td") ] index = cells[0] body = cells[1] decision = cells[2] un_id = cells[3] listing_date = cells[4] entity = context.make("Thing") entity.id = context.make_slug(index, un_id) entity.add("notes", cells[5]) sanction = h.make_sanction(context, entity) sanction.add("listingDate", clean_date(listing_date)) sanction.add("program", decision) sanction.add("recordId", un_id) body, gender = maybe_rsplit(body, "пол:") entity.add_cast("Person", "gender", h.clean_gender(gender)) body, gender = maybe_rsplit(body, "Пол:") entity.add_cast("Person", "gender", h.clean_gender(gender)) body, location = maybe_rsplit(body, "местонахождение:") entity.add_cast("LegalEntity", "country", location) body, imo_num = maybe_rsplit(body, "Присвоенный ИМО номер компании:") body, imo_num = maybe_rsplit(body, "Номер ИМО:") body, emails = maybe_rsplit(body, "Адрес эл. почты:") for email in letter_split(emails): entity.add_cast("LegalEntity", "email", email) body, fax = maybe_rsplit(body, "Номер факса:") body, fax = maybe_rsplit(body, "Факс:") body, phones = maybe_rsplit(body, "Номера телефонов:") for phone in letter_split(phones): entity.add_cast("LegalEntity", "phone", phone) body, phones = maybe_rsplit(body, "Тел.:") for phone in letter_split(phones): entity.add_cast("LegalEntity", "phone", phone) body, swift = maybe_rsplit(body, "СВИФТ-код:") entity.add_cast("LegalEntity", "swiftBic", swift) body, swift = maybe_rsplit(body, "СВИФТ/БИК-код:") entity.add_cast("LegalEntity", "swiftBic", swift) body, other_info = maybe_rsplit(body, "Прочая информация:") entity.add_cast("Thing", "notes", other_info) body, listing_date = maybe_rsplit(body, "Дата внесения в перечень:") body, addresses = maybe_rsplit(body, "Адрес:") for address in letter_split(addresses): country = address if ", " in country: country = address.rsplit(", ", 1) code = registry.country.clean(country, fuzzy=True) obj = h.make_address(context, full=address, country_code=code) h.apply_address(context, entity, obj) entity.add("country", code) body, national_ids = maybe_rsplit( body, "Национальный идентификационный номер:") for national_id in letter_split(national_ids): entity.add_cast("LegalEntity", "idNumber", national_id) body, passport_nos = maybe_rsplit(body, "Паспорт №:") for passport_no in letter_split(passport_nos): entity.add_cast("Person", "passportNumber", passport_no) body, citizenship = maybe_rsplit(body, "Гражданство:") entity.add_cast("Person", "nationality", citizenship) aka = "На основании менее достоверных источников также известен как:" body, aka = maybe_rsplit(body, aka) entity.add("alias", letter_split(aka)) strong_aka = "На основании достоверных источников также известен как:" body, strong_aka = maybe_rsplit(body, strong_aka) entity.add("alias", letter_split(strong_aka)) body, rik_no = maybe_rsplit(body, "Р.И.К.:") body, birth_place = maybe_rsplit(body, "Место рождения:") entity.add_cast("Person", "birthPlace", birth_place) body, birth_dates = maybe_rsplit(body, "Дата рождения:") for birth_date in letter_split(birth_dates): entity.add_cast("Person", "birthDate", clean_date(birth_date)) body, position = maybe_rsplit(body, "Должность:") entity.add_cast("Person", "position", position) body, job = maybe_rsplit(body, "Обращение:") entity.add_cast("Person", "position", job) body, aliases = maybe_rsplit(body, "Другие названия:") entity.add("alias", letter_split(aliases)) body, aliases = maybe_rsplit(body, "Вымышленные названия:") entity.add("alias", letter_split(aliases)) names = body.split(", ") entity.add("name", names) # context.pprint(names) if entity.schema.name == "Thing": entity.schema = model.get("LegalEntity") context.emit(entity, target=True) context.emit(sanction)
def crawl(context: Context): for page in count(1): url = str(context.dataset.data.url) url = url.replace("pPageNumber=1", "pPageNumber=%s" % page) headers = { "Accept": "application/json", "Referer": "https://www.iadb.org/en/transparency/sanctioned-firms-and-individuals", } res = context.http.get(url, headers=headers) ids = [] for row in res.json(): for field, value in list(row.items()): if value == "N/A": row[field] = "" row_id = row.pop("id") ids.append(row_id) entity_type = row.pop("entity") schema = context.lookup_value("types", entity_type) if schema is None: context.log.warning("Unknown entity type", entity=entity_type) continue entity = context.make(schema) entity.id = context.make_slug(row_id) entity.add("name", row.pop("firmName")) entity.add("topics", "debarment") entity.add("alias", row.pop("additionalName")) entity.add("notes", row.pop("title")) entity.add("notes", row.pop("additionalTitle")) entity.add("country", parse_countries(row.pop("country"))) nat = "nationality" if schema == "Company": nat = "jurisdiction" entity.add(nat, parse_countries(row.pop("nationality"))) affiliated = row.pop("affiliatedWithEntityId") if len(affiliated): link = context.make("UnknownLink") link.id = context.make_id(row_id, affiliated) link.add("subject", entity.id) link.add("object", context.make_slug(affiliated)) context.emit(link) sanction = h.make_sanction(context, entity) sanction.add("status", row.pop("statusName")) sanction.add("reason", row.pop("grounds")) sanction.add("authority", row.pop("source")) sanction.add("authority", row.pop("idBinstSource")) sanction.add("program", row.pop("idBinstType")) sanction.add("startDate", h.parse_date(row.pop("datefrom"), FORMATS)) sanction.add("endDate", h.parse_date(row.pop("dateto"), FORMATS)) # context.pprint(row) context.emit(sanction) context.emit(entity, target=True) if min(ids) == 1: return
def parse_result(context: Context, result): type_ = result.pop("type", None) schema = context.lookup_value("type", type_) if schema is None: context.log.error("Unknown result type", type=type_) return entity = context.make(schema) entity.id = context.make_slug(result.pop("id")) entity_number = result.pop("entity_number", None) if entity_number is not None: assert int(entity_number) entity.id = context.make_slug(entity_number, dataset="us_ofac_sdn") name = result.pop("name", None) name = name.replace("and any successor, sub-unit, or subsidiary thereof", "") entity.add("name", name) for alias in ensure_list(result.pop("alt_names", "")): entity.add("alias", alias.split("; ")) entity.add("notes", result.pop("remarks", None)) entity.add("country", result.pop("country", None)) if entity.schema.is_a("Person"): entity.add("position", result.pop("title", None)) entity.add("nationality", result.pop("nationalities", None)) entity.add("nationality", result.pop("citizenships", None)) for dob in result.pop("dates_of_birth", []): entity.add("birthDate", h.parse_date(dob, FORMATS)) entity.add("birthPlace", result.pop("places_of_birth", None)) elif entity.schema.is_a("Vessel"): entity.add("flag", result.pop("vessel_flag", None)) entity.add("callSign", result.pop("call_sign", None)) entity.add("type", result.pop("vessel_type", None)) grt = result.pop("gross_registered_tonnage", None) entity.add("grossRegisteredTonnage", grt) gt = result.pop("gross_tonnage", None) entity.add("tonnage", gt) # TODO: make adjacent owner entity result.pop("vessel_owner", None) assert result.pop("title", None) is None assert not len(result.pop("nationalities", [])) assert not len(result.pop("citizenships", [])) assert not len(result.pop("dates_of_birth", [])) assert not len(result.pop("places_of_birth", [])) for address in result.pop("addresses", []): obj = h.make_address( context, street=address.get("address"), city=address.get("city"), postal_code=address.get("postal_code"), region=address.get("state"), country=address.get("country"), ) h.apply_address(context, entity, obj) for ident in result.pop("ids", []): country = ident.pop("country") entity.add("country", country) h.apply_feature( context, entity, ident.pop("type"), ident.pop("number"), country=country, date_formats=FORMATS, start_date=ident.pop("issue_date", None), end_date=ident.pop("expiration_date", None), ) sanction = context.make("Sanction") sanction.id = context.make_id(entity.id, "Sanction") sanction.add("entity", entity) sanction.add("program", result.pop("programs", [])) sanction.add("provisions", result.pop("license_policy", [])) sanction.add("reason", result.pop("license_requirement", [])) sanction.add("authorityId", result.pop("federal_register_notice", None)) sanction.add("startDate", result.pop("start_date", None)) sanction.add("endDate", result.pop("end_date", None)) sanction.add("country", "us") sanction.add("authority", result.pop("source", None)) # TODO: deref source_url = deref_url(context, result.pop("source_information_url")) sanction.add("sourceUrl", source_url) result.pop("source_list_url") context.emit(sanction) context.emit(entity, target=True) h.audit_data(result, ignore=["standard_order"])