def create_season(series_id, label, descr, ordinal, dry): dry_str = "[DRY-RUN] " if dry else "" repoutil = RepoUtils(Site().data_repository()) print( f"{dry_str}Creating season with\n\tlabel='{label}'\n\tdescription='{descr}'" ) if not dry: season = ItemPage(repoutil.repo) season.editLabels({"en": label}, summary="Setting label") season.editDescriptions({"en": descr}, summary="Setting description") print(f"Created a new Item: {season.getID()}") print(f"{dry_str}Setting {wp.INSTANCE_OF}={wp.TELEVISION_SERIES_SEASON}") if not dry: instance_claim = repoutil.new_claim(wp.INSTANCE_OF.pid) instance_claim.setTarget( ItemPage(repoutil.repo, wp.TELEVISION_SERIES_SEASON)) season.addClaim(instance_claim, summary=f"Setting {wp.INSTANCE_OF.pid}") print( f"{dry_str}Setting {wp.PART_OF_THE_SERIES}={series_id}, with {wp.SERIES_ORDINAL.pid}={ordinal}" ) if not dry: series_claim = repoutil.new_claim(wp.PART_OF_THE_SERIES.pid) series_claim.setTarget(ItemPage(repoutil.repo, series_id)) season_ordinal = repoutil.new_claim(wp.SERIES_ORDINAL.pid) season_ordinal.setTarget(str(ordinal)) series_claim.addQualifier(season_ordinal) season.addClaim(series_claim, summary=f"Setting {wp.PART_OF_THE_SERIES.pid}")
def copy( self, src_item: ItemPage, dest_item: ItemPage, props: Iterable[wp.WikidataProperty], ): """Copy properties from the source item to the destination item Returns a tuple of (successes, failures) """ src_item.get() dest_item.get() failures = 0 successes = 0 for prop in props: if prop.pid not in src_item.claims: print(f"{prop} not found in {src_item.title()}") failures += 1 continue src_claims = src_item.claims[prop.pid] if len(src_claims) > 1: copy_multiple = click.confirm( f"There are {len(src_claims)} values for {prop}. Are you sure you want to copy all of them?" ) # copy_multiple = False if not copy_multiple: print( f"Cannot copy {prop} from {format(src_item)} to {format(dest_item)}. Only scalar properties can be copied" ) failures += 1 continue if prop.pid in dest_item.claims: print(f"{prop} already has a value in {format(dest_item)}") failures += 1 continue targets = [claim.getTarget() for claim in src_claims] for target in targets: if hasattr(target, "get"): target.get() target_str = printable_target_value(target) print( f"Copying {prop}={target_str} from {format(src_item)} to {format(dest_item)}" ) new_claim = Claim(self.repo, prop.pid) new_claim.setTarget(target) dest_item.addClaim( new_claim, summary=f"Setting {prop.pid} ({prop.name})" ) successes += 1 return (successes, failures)
def normalize_repo_url( item: ItemPage, url_normalized: str, url_raw: str, q_value: str, edit_group_hash: str, ): """ Canonicalize the github url This use the format https://github.com/[owner]/[repo] Note: This apparently only works with a bot account """ if url_raw == url_normalized: return logger.info("Normalizing {} to {}".format(url_raw, url_normalized)) source_p = Properties.source_code_repository urls = item.claims[source_p] if source_p in item.claims and len(urls) == 2: if urls[0].getTarget() == url_normalized and urls[1].getTarget( ) == url_raw: logger.info( "The old and the new url are already set, removing the old") item.removeClaims(urls[1], summary=get_sumary(edit_group_hash)) return if urls[0].getTarget() == url_raw and urls[1].getTarget( ) == url_normalized: logger.info( "The old and the new url are already set, removing the old") item.removeClaims(urls[0], summary=get_sumary(edit_group_hash)) return if source_p in item.claims and len(urls) > 1: logger.info( "Multiple source code repositories for {} not supported".format( q_value)) return if urls[0].getTarget() != url_raw: logger.error( f"The url on the object ({urls[0].getTarget()}) doesn't match the url from the sparql query ({url_raw}) for {q_value}" ) return # Editing is in this case actually remove the old value and adding the new one claim = Claim(Settings.wikidata_repo, source_p) claim.setTarget(url_normalized) claim.setSnakType("value") item.addClaim(claim, summary=get_sumary(edit_group_hash)) item.removeClaims(urls[0], summary=get_sumary(edit_group_hash)) # Add git as protocol git = ItemPage(Settings.wikidata_repo, "Q186055") get_or_create_qualifiers(claim, Properties.protocol, git, edit_group_hash)
def main(dry=False): dry_str = "" if dry: print("Running in dry-run mode, will not implement any changes") dry_str = "[DRY-RUN MODE] " repo = Site().data_repository() for movie_id, movie_label in movies_with_missing_titles(): print( f"{dry_str}Setting title='{movie_label}' for {movie_id} ( https://www.wikidata.org/wiki/{movie_id} )" ) if not dry: movie_item = ItemPage(repo, movie_id) movie_item.get() claim = Claim(repo, wp.TITLE.pid) claim.setTarget(WbMonolingualText(movie_label, "en")) movie_item.addClaim(claim)
def get_or_create_claim(item: ItemPage, p_value: str, value: Any, edit_group_hash: str) -> Tuple[Claim, bool]: """ Gets or creates a claim with `value` under the property `p_value` to `item` """ all_claims = item.claims.get(p_value, []) for claim in all_claims: if claim.target_equals(value): return claim, False claim = Claim(Settings.wikidata_repo, p_value) claim.setTarget(value) item.addClaim(claim, summary=get_sumary(edit_group_hash)) return claim, True
def create_episode(series_id, season_id, title, ordinal, dry): dry_str = "[DRY-RUN] " if dry else "" print(f"{dry_str}Creating episode with label='{title}'") if not dry: repoutil = RepoUtils(Site().data_repository()) episode = ItemPage(repoutil.repo) episode.editLabels({"en": title}, summary="Setting label") print(f"Created a new Item: {episode.getID()}") print(f"{dry_str}Setting {wp.INSTANCE_OF}={wp.TELEVISION_SERIES_EPISODE}") if not dry: instance_claim = repoutil.new_claim(wp.INSTANCE_OF.pid) instance_claim.setTarget( ItemPage(repoutil.repo, wp.TELEVISION_SERIES_EPISODE)) episode.addClaim(instance_claim, summary=f"Setting {wp.INSTANCE_OF.pid}") print(f"{dry_str}Setting {wp.PART_OF_THE_SERIES}={series_id}") if not dry: series_claim = repoutil.new_claim(wp.PART_OF_THE_SERIES.pid) series_claim.setTarget(ItemPage(repoutil.repo, series_id)) episode.addClaim(series_claim, summary=f"Setting {wp.PART_OF_THE_SERIES.pid}") print( f"{dry_str}Setting {wp.SEASON}={season_id}, with {wp.SERIES_ORDINAL}={ordinal}" ) if not dry: season_claim = repoutil.new_claim(wp.SEASON.pid) season_claim.setTarget(ItemPage(repoutil.repo, season_id)) season_ordinal = repoutil.new_claim(wp.SERIES_ORDINAL.pid) season_ordinal.setTarget(ordinal) season_claim.addQualifier(season_ordinal) episode.addClaim(season_claim, summary=f"Setting {wp.SEASON.pid}")
def create_episode(series_id, season_id, title, series_ordinal, season_ordinal, dry): """Creates a season item on WikiData Arguments --------- series_id: str The Wiki ID of the series ItemPage season_id: str The Wiki ID of the season ItemPage title: str The title of this episode. This is used to set the label. series_ordinal: int The ordinal of this episode, within the series season_ordinal: int The ordinal of this episode, within the season dry: bool Whether or not this function should run in dry-run mode. In dry-run mode, no real changes are made to WikiData, they are only logged to stdout. Returns ------- episode_id: str The Wiki ID of the episode item """ dry_str = "[DRY-RUN] " if dry else "" print(f"{dry_str}Creating episode with label='{title}'") episode = None if not dry: repoutil = RepoUtils(Site().data_repository()) season = ItemPage(repoutil.repo, season_id) season.get() # Check if season has part_of_the_series set to series_id if wp.PART_OF_THE_SERIES.pid not in season.claims: raise ValueError( f"The season {season_id} does not have a PART_OF_THE_SERIES ({wp.PART_OF_THE_SERIES.pid} property). Check the input series and season IDs for correctness." ) actual_series_id = str( season.claims[wp.PART_OF_THE_SERIES.pid][0].getTarget().getID()) if actual_series_id != series_id: raise ValueError( f"The season {season_id} has PART_OF_THE_SERIES={actual_series_id} but expected={series_id}. Check the input series and season IDs for correctness." ) episode = ItemPage(repoutil.repo) episode.editLabels({"en": title}, summary="Setting label") print(f"Created a new Item: {episode.getID()}") print(f"{dry_str}Setting {wp.INSTANCE_OF}={wp.TELEVISION_SERIES_EPISODE}") if not dry: instance_claim = repoutil.new_claim(wp.INSTANCE_OF.pid) instance_claim.setTarget( ItemPage(repoutil.repo, wp.TELEVISION_SERIES_EPISODE)) episode.addClaim(instance_claim, summary=f"Setting {wp.INSTANCE_OF.pid}") print( f"{dry_str}Setting {wp.PART_OF_THE_SERIES}={series_id}, with {wp.SERIES_ORDINAL}={series_ordinal}" ) if not dry: series_claim = repoutil.new_claim(wp.PART_OF_THE_SERIES.pid) series_claim.setTarget(ItemPage(repoutil.repo, series_id)) series_ordinal_claim = repoutil.new_claim(wp.SERIES_ORDINAL.pid) series_ordinal_claim.setTarget(series_ordinal) series_claim.addQualifier(series_ordinal_claim) episode.addClaim(series_claim, summary=f"Setting {wp.PART_OF_THE_SERIES.pid}") print( f"{dry_str}Setting {wp.SEASON}={season_id}, with {wp.SERIES_ORDINAL}={season_ordinal}" ) if not dry: season_claim = repoutil.new_claim(wp.SEASON.pid) season_claim.setTarget(ItemPage(repoutil.repo, season_id)) season_ordinal_claim = repoutil.new_claim(wp.SERIES_ORDINAL.pid) season_ordinal_claim.setTarget(season_ordinal) season_claim.addQualifier(season_ordinal_claim) episode.addClaim(season_claim, summary=f"Setting {wp.SEASON.pid}") return episode.getID() if episode is not None else "Q-1"