def filter_commons_images(image_titles): if not len(image_titles): return image_titles try: config = {"wiki_country": "commons"} titles = "|".join(image_titles) image_categories = WikipediaCategories(config=config).get(titles) except DSResourceException as exc: image_categories = exc.resource return [ image["title"].replace(" ", "_") for page_id, image in image_categories.get_wikipedia_json().items() if int(page_id) < 0 ]
def filter_free_images(image_titles): if not len(image_titles): return image_titles try: config = {"wiki_show_categories": "hidden"} titles = "|".join(image_titles) image_categories = WikipediaCategories(config=config).get(titles) except DSResourceException as exc: image_categories = exc.resource non_free_images = set() for page_id, page_content in image_categories.get_wikipedia_json().items(): free_image = any([ category for category in page_content.get("categories", []) if "Category:All free media" == category["title"] ]) if not free_image: non_free_images.add(page_content["title"].replace(" ", "_")) return list(non_free_images)