def test_python_xmp_validate_change_list(graph): with graph.open_metadata() as xmp: assert 'dc:creator' in xmp xmp['dc:creator'] = ['Dobby', 'Kreacher'] assert str(xmp) if not XMPMeta: pytest.skip(msg='needs libxmp') xmpmeta = XMPMeta(xmp_str=str(xmp)) DC = XMP_NS_DC assert xmpmeta.does_array_item_exist(DC, 'creator', 'Dobby') assert xmpmeta.does_array_item_exist(DC, 'creator', 'Kreacher')
def test_does_array_item_exist(self): """Tests XMPMeta method does_array_item_exist. Issue #03""" filename = pkg_resources.resource_filename(__name__, "samples/test1.xmp") with open(filename, 'r') as fptr: strbuffer = fptr.read() xmp = XMPMeta() xmp.parse_from_str(strbuffer) xmp.set_array_item(NS_DC, "creator", 2, "foo") xmp.append_array_item(NS_DC, "creator", "bar") self.assertTrue(xmp.does_array_item_exist(NS_DC, "creator", "foo")) self.assertFalse(xmp.does_array_item_exist(NS_DC, "creator", "blah"))
def test_python_xmp_validate_add(trivial): with trivial.open_metadata() as xmp: xmp['dc:creator'] = ['Bob', 'Doug'] xmp['dc:title'] = 'Title' xmp['dc:publisher'] = {'Mackenzie'} xmp_str = str(xmp).replace('\n', '') assert '<rdf:Seq><rdf:li>Bob</rdf:li><rdf:li>Doug</rdf:li>' in xmp_str assert '<rdf:Bag><rdf:li>Mackenzie</rdf:li>' in xmp_str xmpmeta = XMPMeta(xmp_str=str(xmp)) DC = XMP_NS_DC assert xmpmeta.does_array_item_exist(DC, 'creator', 'Bob') assert xmpmeta.does_array_item_exist(DC, 'creator', 'Doug') assert xmpmeta.get_localized_text(DC, 'title', None, 'x-default') == 'Title' assert xmpmeta.does_array_item_exist(DC, 'publisher', 'Mackenzie')
def worker(filepath): # initialize saucenao saucenao_core = SauceNao( directory='directory', databases=9, # 999 by default, 5 for pixiv, 9 for booru. minimum_similarity=65, combine_api_types=False, api_key=saucenao_api_key, exclude_categories='', move_to_categories=False, use_author_as_category=False, output_type=SauceNao.API_JSON_TYPE, start_file='', log_level=logging.ERROR, title_minimum_similarity=90) # search image on saucenao try: result = search(saucenao_core, filepath) except requests.exceptions.ConnectionError: print("Failed to connect saucenao!") return -1 except saucenao.exceptions.DailyLimitReachedException: print("Saucenao daily limit reached! try 1 hour later!") return -2 if (len(result) is 0): print('Image not found on danbooru!') return 1 else: danbooru_id = result[0]['data']['danbooru_id'] print('Image Found, ID=' + str(danbooru_id)) # GET danbooru tag json try: http = urllib3.PoolManager() # disable https cert check warning urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) url = 'https://danbooru.donmai.us/posts/' + str( danbooru_id) + '.json' headers = urllib3.util.make_headers(basic_auth=danbooru_login + ':' + danbooru_api_key) r = http.request('GET', url, headers=headers) r_data = r.data if isinstance(r_data, bytes): r_data = str(r_data, 'utf-8') tags = json.loads(r_data)['tag_string'] taglist = tags.split() except requests.exceptions.ConnectionError: print('failed to GET tag data from danbooru') return -1 # Write XMP Metadata to image xmpfile = XMPFiles(file_path=filepath, open_forupdate=True) xmp = xmpfile.get_xmp() # if image has no xmp data, create one if (xmp is None): xmp = XMPMeta() # write the tags for each in taglist: # check whether XMP includes 'subject' property, # if not, create a new one if (not xmp.does_property_exist(consts.XMP_NS_DC, 'subject')): xmp.append_array_item(consts.XMP_NS_DC, 'subject', each, { 'prop_array_is_ordered': True, 'prop_value_is_array': True }) # check whether tag has been written to file if (not xmp.does_array_item_exist(consts.XMP_NS_DC, 'subject', each)): xmp.append_array_item(consts.XMP_NS_DC, 'subject', each) if (xmpfile.can_put_xmp(xmp)): xmpfile.put_xmp(xmp) xmpfile.close_file() return 0 else: print('Unable to write XMP data!') return -1