-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphoto-receipt.py
More file actions
executable file
·64 lines (54 loc) · 1.85 KB
/
photo-receipt.py
File metadata and controls
executable file
·64 lines (54 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env -S uv --quiet run --no-project --script --
# https://peps.python.org/pep-0723/
# https://github.com/astral-sh/uv
# /// script
# requires-python = ">=3.14,<4"
# dependencies = [
# "timezonefinder >=3.0.1",
# "python-dateutil >=2.7.2",
# ]
# ///
# Python Standard Library
import asyncio, subprocess, json, re, datetime
import timezonefinder
import dateutil.tz
async def main(*, args, prog, loop=None):
tzfinder = timezonefinder.TimezoneFinder()
for photo_path in args:
p = await asyncio.create_subprocess_exec(
"exiftool", "-json", "-groupnames", "--printConv",
photo_path,
stdout=subprocess.PIPE,
)
stdout, stderr = await p.communicate()
raw_data = json.loads(stdout.decode("utf-8"))
for entry in raw_data:
if entry["SourceFile"] != photo_path:
continue
latitude = entry.get("Composite:GPSLatitude", 0)
longitude = entry.get("Composite:GPSLongitude", 0)
m = re.match(r"(?P<year>\d{4}):(?P<month>\d{2}):(?P<day>\d{2}) (?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})", entry["EXIF:DateTimeOriginal"])
tz = tzfinder.timezone_at(lat=latitude, lng=longitude)
dt = datetime.datetime(
**dict((k, int(v)) for k, v in m.groupdict().items()),
tzinfo=dateutil.tz.gettz(tz),
)
result = "Transaction Date: {datetime:%Y-%m-%d %H:%M:%S %z}\nLocation: {latitude:.6f},{longitude:.6f}\nPayee: ?\nAccount: ?\nAmount: ?\n".format(
latitude=latitude,
longitude=longitude,
datetime=dt,
)
with open(photo_path + ".txt", "x") as fo:
fo.write(result)
def _smain(*, argv):
try:
if sys.platform == "win32":
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
else:
loop = asyncio.get_event_loop()
loop.run_until_complete(main(args=argv[1:], prog=argv[0], loop=loop))
except KeyboardInterrupt:
print(file=sys.stderr)
if __name__ == "__main__":
sys.exit(_smain(argv=sys.argv))