def fetch_metadata(cfg, metadata, urls):
    FLICKR_PUBLIC = get_secret('flickr_api_key')
    FLICKR_SECRET = get_secret('flickr_api_secret')
    flickr = FlickrAPI(FLICKR_PUBLIC, FLICKR_SECRET, format='parsed-json')
    license = '1,2,3,4,5,6,7,8,9,10'
    extras = 'description,license,date_upload,date_taken,original_format,'
    extras += 'last_update,geo,tags, machine_tags, o_dims, media,'
    extras += 'url_m,url_n,url_z,url_c,url_l,url_o'
    inserted_ids = []
    for key in cfg['cities']:
        count = 0
        dl_limit = cfg['cities'][key]['download_limit']
        if ((dl_limit != (- 1)) and (dl_limit > 1000)):
            boxes = get_usable_bounding_boxes(list(cfg['cities'][key]['bounding_boxes']), cfg)
        else:
            boxes = list(cfg['cities'][key]['bounding_boxes'])
        city_urls = urls[key]
        if (not (key in metadata)):
            metadata[key] = {}
            metadata[key]['image_count'] = 0
            metadata[key]['images'] = []
        total = 0
        for bbox in tqdm(boxes, desc=key):
            temp = list(map(str, bbox))
            bbox_str = ','.join(temp)
            time.sleep(cfg['time_delay'])
            total_pages = 0
            try:
                city_pics = flickr.photos.search(privacy_filter=PRIVACY_FILTER, bbox=bbox_str, content_type=CONTENT_TYPE, has_geo=HAS_GEO, geo_context=GEO_CTX, license=license, extras=extras, per_page=cfg['page_size'])
                total_pages = city_pics['photos']['pages']
                total += int(city_pics['photos']['total'])
            except FlickrError as err:
                print(f'Error retrieving intitial page for bounding box {bbox}')
                print(f'{err}')
            for p in range(1, total_pages):
                try:
                    time.sleep(cfg['time_delay'])
                    city_pics = flickr.photos.search(privacy_filter=PRIVACY_FILTER, bbox=bbox_str, content_type=CONTENT_TYPE, has_geo=HAS_GEO, geo_context=GEO_CTX, license=license, extras=extras, per_page=cfg['page_size'], page=p)
                    for ph in city_pics['photos']['photo']:
                        if ((dl_limit != (- 1)) and (count > dl_limit)):
                            break
                        if ((cfg['url_field'] in ph) and (not (ph[cfg['url_field']] in city_urls))):
                            metadata[key]['images'].append(ph)
                            city_urls.add(ph[cfg['url_field']])
                            metadata[key]['image_count'] += 1
                            count += 1
                except FlickrError as err:
                    print(f'Error retrieving page {p} for bounding box {bbox}')
                    print(f'{err}')
    return (metadata, urls)
def fetch_metadata(cfg, metadata, urls):
    FLICKR_PUBLIC = get_secret('flickr_api_key')
    FLICKR_SECRET = get_secret('flickr_api_secret')
    flickr = FlickrAPI(FLICKR_PUBLIC, FLICKR_SECRET, format='parsed-json')
    license = '1,2,3,4,5,6,7,8,9,10'
    extras = 'description,license,date_upload,date_taken,original_format,'
    extras += 'last_update,geo,tags, machine_tags, o_dims, media,'
    extras += 'url_m,url_n,url_z,url_c,url_l,url_o'
    inserted_ids = []
    for key in cfg['cities']:
        count = 0
        dl_limit = cfg['cities'][key]['download_limit']
        if ((dl_limit != (- 1)) and (dl_limit > 1000)):
            boxes = get_usable_bounding_boxes(list(cfg['cities'][key]['bounding_boxes']), cfg)
        else:
            boxes = list(cfg['cities'][key]['bounding_boxes'])
        city_urls = urls[key]
        if (not (key in metadata)):
            metadata[key] = {}
            metadata[key]['image_count'] = 0
            metadata[key]['images'] = []
        total = 0
        for bbox in tqdm(boxes, desc=key):
            temp = list(map(str, bbox))
            bbox_str = ','.join(temp)
            time.sleep(cfg['time_delay'])
            total_pages = 0
            try:
                city_pics = flickr.photos.search(privacy_filter=PRIVACY_FILTER, bbox=bbox_str, content_type=CONTENT_TYPE, has_geo=HAS_GEO, geo_context=GEO_CTX, license=license, extras=extras, per_page=cfg['page_size'])
                total_pages = city_pics['photos']['pages']
                total += int(city_pics['photos']['total'])
            except FlickrError as err:
                print(f'Error retrieving intitial page for bounding box {bbox}')
                print(f'{err}')
            for p in range(1, total_pages):
                try:
                    time.sleep(cfg['time_delay'])
                    city_pics = flickr.photos.search(privacy_filter=PRIVACY_FILTER, bbox=bbox_str, content_type=CONTENT_TYPE, has_geo=HAS_GEO, geo_context=GEO_CTX, license=license, extras=extras, per_page=cfg['page_size'], page=p)
                    for ph in city_pics['photos']['photo']:
                        if ((dl_limit != (- 1)) and (count > dl_limit)):
                            break
                        if ((cfg['url_field'] in ph) and (not (ph[cfg['url_field']] in city_urls))):
                            metadata[key]['images'].append(ph)
                            city_urls.add(ph[cfg['url_field']])
                            metadata[key]['image_count'] += 1
                            count += 1
                except FlickrError as err:
                    print(f'Error retrieving page {p} for bounding box {bbox}')
                    print(f'{err}')
    return (metadata, urls)