import asyncio
from sqlor.dbpools import DBPools
from appPublic.jsonConfig import getConfig
from ahserver.filestorage import FileStorage
from imageinfo import get_image_info

async def main():
	db = DBPools()
	async with db.sqlorContext('mediadb') as sor:
		sql = "select a.* from media a left join mediainfo b on a.id = b.id where b.id is null and a.mtype = 'image'"
		recs = await sor.sqlExe(sql, {})
		fs = FileStorage()
		for r in recs:
			fp = fs.realPath(r.mlocation)
			print(fp, '...')
			lat, lon, ts = get_image_info(fp)
			info_status = '0'
			if ts:
				info_status = '1'
			ns = {
				"id":r.id,
				"info_status":info_status,
				"latitude":lat,
				"longitude":lon,
				"media_timestamp":ts
			}
			await sor.C('mediainfo', ns)

if __name__ == '__main__':
	config = getConfig('..', {'workdir':'..'})
	DBPools(config.databases)
	asyncio.new_event_loop().run_until_complete(main())

